Background: I have to copy a file from one server, to over 100 servers in a test environment. once the file is copied, it requires to have the permissions on the file changed/verified. These are all linux servers. most of them have the same password for login, but some may not.
I need help with making a loop in my bash script. It is actually calling on Expect. The area I want to make 'better' or fix, is a few things
- the part where it expects a password. (the * section), it should quit the script after 1 failed password attempt, and dump the ip to a text file called "fail.txt"
- ideally there should be a piece that also dumps the ip to a fail.txt if there is no connection to the server.
I tried to write the password piece thinking its a loop, but I'm not really sure if that method of thinking is deal. I tried just adding another "expect Password: " thinking that if it gets the prompt a second time, to exit out, but I had a hard time with getting that to work. Thank you!
#!/bin/bash
while read ip; do
sleep 2
expect <<- DONE
set timeout 1
spawn scp yoman.txt root@$ip:/felixtemp
if above command fails, dump the IP to fail.txt, otherwise continue
expect yes/no { send yes\r }
expect Password: { send aaaaaa\r } #if this is good, continue the script from *****
else #exit the script
expect Password: { send 033\r }
expect # { send "echo 'password failed'\r" }
&& dump to a text file called fail.txt
***** expect # { send "exit\r\r" }
sleep 1
set timeout 1
spawn ssh root@$ip
sleep 2
expect yes/no { send yes\r }
sleep 2
expect Password: { send aaaaaa\r }
sleep 5
expect # { send "cd /felixtemp\r" }
expect # { send "chown informix:informix yoman.txt\r" }
expect # { send "chmod 775 yoman.txt\r" }
expect # { send "sum yoman.txt | grep 10350 && echo 'transfer good' || echo 'transfer bad'\r" }
expect # { send exit\r }
sleep 1
DONE
done < ip.txt