Linux and linux
Simple mode:
#!/usr/bin/expect -f set timeout 5 spawn ssh root@192.168.0.1expect "*assword*" send "root\r" expect "#" send "ifconfig \r" expect eof
Explanation:
Send: used to send a string to the process: receive a string from the process, for example, receive CT "* assword *"
Spawn: Start a new process interact: allow user interaction
How to Use variables:
#!/usr/bin/expect -fset port 22set user rootset host 192.168.0.12set password rootset timeout -1spawn ssh -D $port $user@$host "ifconfig"expect { "*yes/no" { send "yes\r"; exp_continue} "*assword:" { send "$password\r" } } expect "*#*" send "ifconfig > /home/cfg \r"send "exit\r"}
Explanation:
Keep CT {"* yes/no" {send "yes \ r"; exp_continue} "* assword:" {send "$ password \ r "}}
Select the mode. exp_continue indicates that the operation continues.
Get the variable by reading the configuration file:
Configuration File
192.168.0.1 root192.168.0.2 root
Automated login script
#!/usr/bin/expect -fset f [open ./ip r] while { [gets $f line ]>=0 } { set ip [lindex $line 0] set pwd [lindex $line 1]spawn ssh $ipexpect "*password:" { send "$pwd\r" }expect "#"send "ifconfig \r"send "exit\r"interact}
Explanation:
Multiple servers can be executed cyclically. This is a very useful method!
Automatic Remote file copy:
#!/usr/bin/expect -fset port 22set user rootset host 192.168.28.30set password rootset timeout -1spawn scp $host:/home/cfg ./expect {"*yes/no" { send "yes\r"; exp_continue}"*assword:" { send "$password\r" }}expect eof
Explanation:
Same principle as ssh
Run the command remotely and write the file to the local server through scp:
#!/usr/bin/expect -fset port 22set user rootset host 192.168.28.30set password rootset timeout -1spawn ssh -D $port $user@$host "ifconfig"expect {"*yes/no" { send "yes\r"; exp_continue}"*assword:" { send "$password\r" }}expect "*#*"send "ifconfig > /home/cfg \r"send "exit\r"interactspawn scp $host:/home/cfg ./expect {"*yes/no" { send "yes\r"; exp_continue}"*assword:" { send "$password\r" }}expect eof
Explanation:
Automated O & M, remote interaction from server A ssh to server B, and then execute commands on server B.
Http://www.cnblogs.com/Javame/p/4272440.html