Linux 之 expect

17 篇文章 0 订阅
5 篇文章 0 订阅

################

expect:

################

[set timeout 10]:设置超时时间,单位s,timeout -1永不超时

[ spawn ssh root@ip ]:是进入expect环境后才可以执行的expect的内部命令。主要功能是给ssh运行进程加个壳,用来传递交互指令。

[ expect "password:"]:判断上次输出结果是否包含"password:"的字符串,如果有立即返回,否则就等待一段时间,就是set timeout 10的时间。

[ send "yes\r"]:执行交互动作,与手动输入一样。

[ interact ]:保持交互状态,留在远程终端

[ $argv ]:参数数组,[ lindex $argv n],n从0开始

################


#!/usr/bin/expect				##shell类型
set ip [lindex $argv 0]                     	##接受第一个参数
set password [lindex $argv 1]			##接受第二个参数
set timeout 10                                	##设置超时时间,单位s,timeout -1为永不超时
spawn ssh root@ip                       	##发送ssh请求
expect{                                         ##返回匹配信息
"*yes/no" {send "yes\r";exp_continue}		##第一次ssh连接会提示yes/no,继续
"*password:"{send "$password\r"}		##出现密码提示,发送密码
}
interact					##交互模式,用户会停留在远程服务器上

#!/usr/bin/expect
set ip [lindex $argv 0]
set password redhat				##登录密码为redhat(已知密码的情况)
set timeout 10
spawn ssh root@ip
expect{
"*yes/no" {send "yes\r";exp_continue}
"*password:"{send "$password\r"}
}
interact

#!/usr/bin/expect
set ip [lindex $argv 0]
set password redhat
set timeout 10
spawn ssh root@ip
expect{
"*yes/no" {send "yes\r";exp_continue}
"*password:"{send "$password\r"}
}
expect "#*"					##登录后
send "pwd\r"					##执行密令pwd,查看当前路径
send "exit\r"					##退出
expect eof


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值