expect 用法

 

expect

send

send_user #打印输出

puts打印字符到屏幕

sleep 休眠 xx s

wait 等待进程执行结束

expect eof 退出expect进程

set timeout #设置等待超时时长

  expect最常用的语法(tcl语言:模式-动作) 

        单一分支模式语法: 

              expect “hi” {send “You said hi\n"}           匹配到hi后,会输出“you said hi”,并换行

        多分支模式语法: 

              expect "hi" { send "You said hi\n" } \ "hehe" { send “Hehe yourself\n" } \ "bye" { send “Good bye\n" } 

        匹配hi,hello,bye任意字符串时,执行相应输出.等同如下:

             expect { "hi" { send "You said hi\n"} "hehe" { send "Hehe yourself\n"} "bye" { send “Good bye\n"} } 

 示例:

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\n";exp_continue }         
 9        "password" { send "$password\n" } 
10     } 
11    set timeout 20
11    expect "]#" { send "useradd haha\n" }
12    expect "]#" { send "echo aaa|passwd --stdin haha\n" }
13    send "exit\n" 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

posted @ 2021-06-23 08:33  i舒  阅读(547)  评论(0编辑  收藏  举报