Reputation: 171
The following expect script work fine when $IP of Linux machine ask password after login
But on some cases some Linux machines not need password for ssh ( we can login without password) ,
so I need to change my expect script in order to support machines without password
Please advice how to fit my expect script in order to support machines with password and machines without password
* target of the following expect script is to check the hostname on remote Linux machine
expect_test=`cat << EOF
set timeout -1
spawn ssh $IP hostname
expect {
")?" { send "yes\r" ; exp_continue }
word: {send "pass123\r" }
}
expect eof
EOF`
expect -c "$expect_test"
example of exe on remote machine (with password)(name of remote machine - Linux1_machine )
IP=10.17.18.6
expect -c "$expect_test"
spawn ssh 10.17.18.6 hostname
sh: /usr/local/bin/stty: not found
This computer system, including all related equipment, networks and network devices (specifically including Internet access),is pros
yes
Password:
Linux1_machine
example when we exe the expect script on machine that not need password for login
IP=10.10.92.26
expect -c "$expect_test"
spawn ssh 10.10.92.26 hostname
sh: /usr/local/bin/stty: not found
Linux15_machine
expect: spawn id exp5 not open
while executing
"expect eof"
Upvotes: 1
Views: 2974
Reputation: 958
You have a couple of options.
If you know what to expect from the servers you don't have to login to (e.g. session goes straight to the prompt) then add an expect statement with that:
expect {
")?" { send "yes\r" ; exp_continue }
word: {send "pass123\r" }
"prompt" { do something }
}
In case you don't have consistent prompts, you could try using a Regular Expression detecting a wide range of prompts:
set prompt “(%|#|\\$) $” ;# default prompt
expect {
")?" { send "yes\r" ; exp_continue }
word: {send "pass123\r" }
-re $prompt { do something }
}
If you don't know what you will get at all (seems unlikely) then you can also add an action when Expect times out.
expect {
")?" { send "yes\r" ; exp_continue }
word: {send "pass123\r" }
timeout { do something }
}
that will allow expect to move when it doesn't receive the other expected lines.
See http://wiki.tcl.tk/11583 for specific examples of what you are trying to do. Also, the Getting Started with Expect chapter from the Oreilly book is worth a read.
Upvotes: 2