下载与安装:
yum -y install expect 或参考 http://blog.csdn.net/xiaofei0859/article/details/6047207
1,使用变量
set date [exec date "+%Y-%m-%d"]
$date
set commd [exec ls]
set ip [lindex $argv 0] #
puts "ip is $ip" #控制台输出
2,获取执行参数
set nb1 [lindex $argv 0]
set nb2 [lindex $argv 1]
puts "$argc"
puts "$argv0"
3,逻辑判断
switch
set color
switch
}
if 分支
[root@localhost expect]# cat 3.exp
#!/usr/bin/expect
set test [lindex $argv 0]
if { "$test" == "apple" } {
} else
}
for 循环结构
第一种
[root@localhost expect]# cat 5.exp
#!/usr/bin/expect
foreach number {
1
2
3
4
} {
}
第二种
[root@localhost expect]# cat 5.exp
#!/usr/bin/expect
for {set i 0} {$i<4} {incr i} {
}
while
[root@localhost expect]# cat 5.exp
#!/usr/bin/expect
set i 1
while {$i<4} {
}
4 函数定义
[root@localhost expect]# cat 6.exp
#!/usr/bin/expect
proc test {} {
}
test
5,返回条件判断
expect {
"yes/no" { send "yes\r";exp_continue}
"assword" { send "${pw}\r"}
}
6. 结尾加 [interact]
执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。如果你只是登录过去执行