Thank you for visiting this page, this page has been update in another link Expect command examples on linux
As it's man page says, expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. An interpreted language provides branching and high-level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, afterward returning control to the script. Here is a simple example, it colletcs login information on other host, it could be a host, or device which supports telnet. Same way, you can use it for ssh connection.#!/usr/bin/expect set timeout 20 set host "aaa.com" set prompt "aaa: " set user "user" set pass "mysecret" set cmd "who -l" spawn telnet $host expect "login: " send "$user\r" expect "Password: " send "$pass\r" expect "$prompt" send "$cmd\r" expect "$prompt" send "exit\r" expect "\n" #!/usr/bin/expect set timeout 9 set username [lindex $argv 0] set password [lindex $argv 1] set hostname [lindex $argv 2] log_user 0 if {[llength $argv] == 0} { send_user "Usage: scriptname username \'password\' hostname\n" exit 1 } send_user "\n#####\n# test loging to $hostname\n#####\n" spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname expect { timeout { send_user "\nFailed to get password prompt\n"; exit 1 } eof { send_user "\nSSH failure for $hostname\n"; exit 1 } "*assword" } send "$password\r" expect { timeout { send_user "\nLogin failed. Password incorrect.\n"; exit 1} "*\$*" } send_user "\nPassword is correct\n" send "exit\r" close The bad part of the script is that you have to type in server's username, password and hostname. In a large installation environment, mostly all servers has ssh key setup for admin server, so easier for admin nodes to check server's status, or something else that you want. #!/usr/bin/expect set timeout 9 set username [lindex $argv 0] set hostname [lindex $argv 1] set cmd [lindex $argv 2] log_user 0 if {[llength $argv] == 0} { send_user "Usage: scriptname username hostname\n" exit 1 } send_user "\n#####\n# test loging to $hostname\n#####\n" spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname expect { timeout { send_user "\nFailed to con nect to the host $hostname \n"; exit 1 } eof { send_user "\nSSH failure for $hostname\n"; exit 1 } "*\$*" } send "$ cmd \r" expect { timeout { send_user "\nLogin failed. Password incorrect.\n"; exit 1} "*\$*" } send_user "\nPassword is correct\n" send "exit\r" close |