首页 > 其他 > 详细

74.expect

时间:2018-06-06 23:35:59      阅读:293      评论:0      收藏:0      [点我收藏+]
分发系统-expect
yum install -y expect
  • 自动远程登录
vim 1.expect
#! /usr/bin/expect
set host "192.168.133.132"  //变量设置,类似shell的 host=xx
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}  
"password:" { send "$passwd\r" }   //返回值匹配到yes/no时,输入yes并回车,返回值匹配到passwd时输入密码并回车
}
interact   //interact退出expect,留在远程主机;什么也不写会退出expect,并退出远程主机;expect eof 延迟几秒后退出expect脚本并退出远程主机
chomod a+x 1.expect
./1.expect  //执行1.expect脚本
  • 自动远程登录后,执行命令并退出
#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.133.132
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"  //返回值匹配到"]*" ,则执行下面命令
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"  //退出终端,(退出expect,并退出远程主机)

技术分享图片

  • 传递参数($argv 0 第一个参数,依次类推)
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh $user@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
./3.expect  root  192.168.127.134  "w;top;"

技术分享图片

74.expect

原文:http://blog.51cto.com/13569831/2125739

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!