Need help with Script

Discussion in 'Programming/Scripts' started by gasparmenendez, Apr 18, 2017.

  1. gasparmenendez

    gasparmenendez New Member

    Hi folks, first of all I need to say that I'm not a programmer, but I need to do something based on programming....
    My problem is that I need to modify the DNS servers in almost 1000 nanostation equipments. I have access to them through ssh and I want to make a script to acomplish the task. I already made a file with the ip addresses of all CPE's (named client.txt). I think my script can begin like this:

    #!/bin/bash
    for host in $(cat client.txt);
    do
    ssh Administrador@$host sameforall;
    sed 's/x.x.x.x/y.y.y.y /etc/resolve.conf;
    sed 's/w.w.w.w/z.z.z.z /etc/resolve.conf;

    # here I restart network service but I don't have the command yet
    done.


    y.y.y.y and z.z.z.z are the new DNS servers and x.x.x.x w.w.w.w the old ones


    I know this is far away to be correct, but I need somebody help me. The first problem I'm going to find is that ssh prompts me to put the given servers ssh key to my known_hosts file, how can I solve this???

    Can anybody please help me??? Thanks in advance.

    BR.
     
    Last edited: Apr 18, 2017
  2. sjau

    sjau Local Meanie Moderator

    You'd probably want something like this:

    Code:
    #!/usr/bin/env bash
    
    while read i; do
        ssh Administrator@${i} << EOF
            sed 's/x.x.x.x/y.y.y.y "/etc/resolv.conf"
            sed 's/w.w.w.w/z.z.z.z "/etc/resolv.conf"
            systemctl restart networking
    EOF
    done < "client.txt"
    
     
    Last edited: Apr 19, 2017
  3. You can apparently setup password less Login Using SSH Keygen. You dont need to put password each time script runs.
    A quick google give idea regarding how to setup : Refer

    Also if you just need to update new namserver IP, why not use echo?
    like,
    Code:
    echo "nameserver 8.8.8.8" > /etc/resolve.conf
    echo "nameserver 8.8.4.4" >> /etc/resolve.conf
     
  4. florian030

    florian030 ISPConfig Developer ISPConfig Developer

    You can use sed or a simple echo. But do not use /etc/resolve.conf. Better use /etc/resolv.conf
     
  5. gasparmenendez

    gasparmenendez New Member

    thanks for your replies... I'll try the echo solution and post results.
    BR.
     
  6. gasparmenendez

    gasparmenendez New Member

    hi @24x7servermanagement !, when I try to create .ssh Directory on the remote host (You can apparently setup password less Login Using SSH Keygen) it ask me for the password, so I'm back to square one...
    In the other hand the echo solution worked fine!! Now I have to figure out how can I solve the password issue (I'm thinking in sshpass maybe). I'll post results.
     
  7. sjau

    sjau Local Meanie Moderator

    1. Create a key pair on your local machine
    Code:
    ssh-keygen -t rsa -b 4096
    
    That would create a rsa keypair with 4096 bit and by default it will be stored as ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub

    2. Copy public key to the server
    Code:
    ssh-copy-id -i .ssh/id_rsa.pub [email protected]_server
    
    If you'e not in your home folder, you have to supply the according path to the public key.

    3. On the server
    On Debian it's now default that you can't login as root directly. You may need to change the /etc/ssh/sshd_config
    and enable this option:
    Code:
    PermitRootLogin yes
    
    After editing the sshd_config, you'll need to restart/reload the sshd server.
    This is only require if you want to login as root. Step 2 may fail if it's not enabled and you try to add your public key to the server's root authenticated keys. If so, repeat step 2 after enabling root login.
    You could also add this line to the sshd_config
    Code:
    PermitRootLogin without-password
    
    With that, you can only login through a key and not with a password.

    4. On the client
    You can now login in your server without being asked for a password
    Code:
    ssh [email protected]_server
    
     

Share This Page