expect
send
send_user #打印输出
puts打印字符到屏幕
sleep 休眠 xx s
wait 等待进程执行结束
expect eof 退出expect进程
set timeout #设置等待超时时长
expect最常用的语法(tcl语言:模式-动作)
单一分支模式语法:
expect “hi” {send “You said hi "} 匹配到hi后,会输出“you said hi”,并换行
多分支模式语法:
expect "hi" { send "You said hi " } "hehe" { send “Hehe yourself " } "bye" { send “Good bye " }
匹配hi,hello,bye任意字符串时,执行相应输出.等同如下:
expect { "hi" { send "You said hi "} "hehe" { send "Hehe yourself "} "bye" { send “Good bye "} }
示例:
1 #!/usr/bin/expect 2 set ip [lindex $argv 0] 3 set user [lindex $argv 1] 4 set password [lindex $argv 2] 5 set timeout 10 6 spawn ssh $user@$ip 7 expect { timeout {exp_continue} 8 "yes/no" { send "yes ";exp_continue } 9 "password" { send "$password " } 10 } 11 set timeout 20 11 expect "]#" { send "useradd haha " } 12 expect "]#" { send "echo aaa|passwd --stdin haha " } 13 send "exit " expect eof 14 #./ssh4.exp 192.168.8.100 root aa 执行多条命令
https://www.cnblogs.com/yxh168/p/9028122.html
https://www.cnblogs.com/yxh168/p/9028122.html
https://www.cnblogs.com/yxh168/p/9028122.html