Need help with Script

Discussion in 'Programming/Scripts' started by gasparmenendez, Apr 18, 2017 at 12:16 AM.

  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 at 7:54 PM
  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 at 12:17 PM
  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.
     

Share This Page