1、ftp登录远程服务器
#!/usr/bin/expect -f
spawn ftp 192.168.250.12
expect "Name (192.168.250.12:root):" { send "root\r"}
expect "Password:" { send "cmcc\r" }
expect "ftp>" {send "binary\r"}
expect "ftp>" {send "get anaconda-ks.cfg\r"}
expect "ftp>" {send "exit\r"}
2、ssh到远程机器并交互式输入命令后退出
#!/usr/bin/expect -f
if { $argc != 2 } {
puts "args error!for example:expect ssh.expect user ip"
exit
}
set user [lindex $argv 0]
set ip [lindex $argv 1]
set passwd [lindex $argv 2]
spawn ssh -l $user $ip
expect {
"*yes/no" {send "yes\r";exp_continue}
"*password:" {send "cmcc\r";exp_continue}
"#*" {send "mkdir -p /opt/test1\r" }
}
send "exit \r"
#interact ##交互式命令,如果有此参数,脚本退出,但是窗口不退出,可以直接在远程机上输入命令
expect eof
3、匹配任意条件后输入对应的结果
#!/usr/bin/expect -f
expect {
"hi" { send "you said hi\n"}
"hello" {send "you said hello\n"}
"word" {send "you said word\n"}
}




