Skip to content
Advertisement

No return after executing passwd command using ssh in script

I have one script which reads hosts IP from /etc/hosts file, do ssh and update the passwords there for the given user. However, the script is closed after setting password for the first host and not able to set password for rest of the hosts. Please find below files-

/etc/hosts file-

172.x.x.x host1
172.x.x.x host2
172.x.x.x host3

Script I am using is as-

key=$1  
user=$2  
password=$3  
filename='/etc/hosts'  
while IFS= read -r line; do 
   IFS=' ' read -r -a array <<< $line
   ssh -i $key centos@${array[0]} "echo "$password" | sudo passwd $user --stdin"  
done <$filename

Script is simple. but it is getting closed after setting password for host1 from file. Does anyone has any idea, what I am missing?

Advertisement

Answer

By default ssh reads from stdin which is your input file.ssh consumes the rest of the file and your while loop terminates.So try using

       ssh -n 
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement