3

We've around 3000 VMs & 450 Physical servers which are Linux based servers (few of then ubuntu starting from 9.x & few of them are Susu starting 8.X & majority of them are RHEL starting from 4.x till 7.4) on all of them I need to add few hostname entries with IP details into their respective /etc/hosts files.

I've different users on each server with full sudoers access which I can use Hence I've created a CSV file with hostname, username & password format. which contains required details to log in. Filename is "hostname_logins.csv"

I need to upload a file (i.e. hostname_list to each of these servers and then update those same details in each of the servers host files.

I'll be running this script using one RHEL 6 server. (All of the other hosts are resolvable from this server & are reachable, I've confirmed it already.)

The script is working but it's asking for accepting the host key once and also asked for the password 2 times however the 3rd time it does not asked for a password it worked automatically I guess, but need to ensure it does not askes to accept the host key or passwords.:

#!/bin/bash
runing_ssh()
{
while read hostname_login user_name user_password
do ssh -vveS -ttq rishee:[email protected] "sudo -S -ttq < ./.pwtmp cp -p /etc/hosts /etc/hosts.$(date +%Y-%m-%d_%H:%M:%S).bkp && sudo -S bash -c 'cat ./hostname_list >> /etc/hosts' && rm -f ./.pwtmp ./hostname_list"
done < hostname_logins.csv
}

while read hostname_login user_name user_password
do  echo $user_password > ./.pwtmp
    cat ./.pwtmp
    scp -p ./.pwtmp ./hostname_list $user_name@$hostname_login:
    runing_ssh
done < hostname_logins.csv

I need to make this as a single script which will work on all these servers. thanks in advance.

2
  • 1
    before the while loop do IFS=, so you split up the data properly during the read command Commented Oct 18, 2017 at 10:05
  • 1
    I really recommend ansible.... Commented Oct 18, 2017 at 14:09

1 Answer 1

2
+50

You are executing the original copy from /tmp with sudo, but nothing else.

while read hostname_login user_name user_password
do  echo $myPW >.pwtmp
    scp -p ./.pwtmp ./hostname_list $user_name:$user_password@$hostname_login:
    ssh -etS $user_name:$user_password@$hostname_login "sudo -S <.pwtmp cp -p /etc/hosts /etc/hosts.bkp && sudo -S <.pwtmp cat ./hostname_list >> /etc/hosts && rm -f ./.pwtmp ./hostname_list"
done < hostname_logins.csv

I dropped the explicit send to /tmp and the cp back to your home dir, and defaulted the location (to $user_name's home dir) by not passing anything to scp after the colon. Fix that if it doesn't work for you.

I created a password file for improved security and code reuse, and sent it along with the hosts list. I added a sudo -S to each relevant command, reading from the password file.

That [bash -c ...] syntax doesn't work on my implementation, so I took it out.

Hope that helps.

Update

Added -t to ssh call. Try that.

Sign up to request clarification or add additional context in comments.

17 Comments

I tried your script but this is what the error I got
[root@node2 ~]# ./script1.sh ssh: connect to host rishee port 22: No route to host lost connection rishee:[email protected]'s password: bash: .pwtmp: No such file or directory [root@node2 ~]#
while read hostname_login user_name user_password do echo $myPW > .pwtmp scp -p ./.pwtmp ./hostname_list $user_name@$hostname_login: ssh -eS $user_name:$user_password@$hostname_login "sudo -S <.pwtmp cp -p /etc/hosts /etc/hosts.bkp && sudo -S <.pwtmp cat ./hostname_list >> /etc/hosts && rm -f ./.pwtmp ./hostname_list" done < hostname_logins.csv
[root@node2 ~]# ./script1.sh [email protected]'s password: .pwtmp 100% 1 0.0KB/s 00:00 hostname_list 100% 122 0.1KB/s 00:00 rishee:[email protected]'s password: sudo: sorry, you must have a tty to run sudo [root@node2 ~]#
pluse uno for tech support (and helpful answer). Good luck to all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.