Shell script to ssh and change the passwords

Hi All,

I am trying to create a script with the help of while and expect loop to do the following:

  1. script will pick up the servers one by one from server_list file and will do ssh to it.
  2. for each server it will change the password of user test1.
  3. script should also provide logs for each and every step

below is sample script which i have created (doesnot inlcude the password change part , since i want to first make sure script run correctly before inclusding password change commmand in to it).

cat -n test.sh
     1  #!/usr/bin/expect -f
set hosts [open server_list r]
     4  while {[gets $hosts host] >= 0}
     5  {
     6  #echo "connecting $server via ssh"
     7  spawn sh -p 56786 -l test1 $host
     8  expect "$"
     9  send "exit\r"
    10  expect eof
    11  }
    12  close $hosts
 
i am getting error as :
[root@VM172016001140 tmp]# bash -x test.sh
+ LOG=/tmp/changepassword.log
+ set hosts '[open' server_list 'r]'
test.sh: line 13: syntax error: unexpected end of file

can someone help in modifying this script to fulfill all the mentioned requirement.:slight_smile:

I'm not sure that you log snippet fits your code snippet as there's no LOG assignment in the code (or part of the code is overtyped due to e.g. <CR> chars at EOL). So I can't tell if there's e.g. an opening quote too many. Any how, in line 7, the ssh command lost one "s".