[shell] expect的一些例子

发表于:2007-05-26来源:作者:点击数: 标签:
到rpmfind可以下载到expect #---------------------------------------------- 例一:追加到文本前(利用vi) #!/usr/bin/expect set a [lindex $argv 0] set b [lindex $argv 1] spawn vi $b expect * send O send $a send 3 send : send wq send expect eo

到rpmfind可以下载到expect

#----------------------------------------------

例一:追加到文本前(利用vi)

#!/usr/bin/expect
set a [lindex $argv 0]
set b [lindex $argv 1]
spawn vi $b
expect "*"
send "O"
send "$a"
send "3"
send ":"
send "wq"
send " "
expect eof

[lindex $argv 0] 第一个变量,类似""

[lindex $argv 1] 第二个变量,类似""

#----------------------------------------------

例二:自动su

#!/usr/bin/expect
set password 123
spawn su
expect "*password:"
send "$password "
interact

#----------------------------------------------

例三:passwd tmp用户

#!/usr/bin/expect
spawn passwd tmp
set password 345
expect "*password:"
send "$password "
expect "*password:"
send "$password "
expect eof

原文转自:http://www.ltesting.net