向一个脚本传递参数,bash shell是使用$1,$2...来接收参数的,而epxect则将脚本的执行参数保存在数组$argv中,在脚本中一般将其赋值给变量:set 变量名 [lindex $argv 参数]
实例:
写一个自动登陆远程主机的脚本,通过参数传递主机名,帐户,密码
1)编写脚本
#!/usr/bin/expect
set ip [lindex $argv 0]
set username [lindex $argv 1]
set passwd [lindex $argv 2]
spawn ssh -l $username $ip
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
#expect eof
interact
注意:若登陆后便退出远程终端,则写expect eof即可。
2)执行脚本
expect autologin.sh 172.24.6.29 root redhat