Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71254
  • 博文数量: 44
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2016-09-19 16:49
文章分类

全部博文(44)

文章存档

2018年(2)

2017年(1)

2016年(41)

我的朋友

分类: LINUX

2016-04-26 09:56:59

原文地址:学习expect(一) 作者:linux_kaige

[yangkai@localhost myshell]$ cat test_dh.exp 
#!/bin/bash/expect -f
set ip 192.168.23.128
set password 123456
set timeout 3
spawn ssh root@$ip
expect {
"passw" {send "$password\r";exp_continue}
#"de password: " {send "$password\r";exp_continue}
"root@" {send "df -h /opt\r";exp_continue}
}
interact
[yangkai@localhost myshell]$ 
[yangkai@localhost myshell]$ expect test_dh.exp 
spawn ssh root@192.168.23.128
root@192.168.23.128's password: 
Last login: Mon Mar  9 23:18:26 2015 from 192.168.23.128
df -h /opt
[root@localhost ~]# df -h /opt
文件系统              容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
                       18G  3.0G   14G  18% /
df -h /opt
[root@localhost ~]# df -h /opt
文件系统              容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
                       18G  3.0G   14G  18% /
df -h /opt
[root@localhost ~]# df -h /opt
文件系统              容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
                       18G  3.0G   14G  18% /
df -h /opt
[root@localhost ~]# df -h /opt
文件系统              容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
                       18G  3.0G   14G  18% /
[root@localhost ~]# df -h /opt
文件系统              容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
                       18G  3.0G   14G  18% /
df -h /opt
[yangkai@localhost myshell]$ -------------->>>强制终止之后,就到yangkai这个用户了
对上面例子的expect的解说
expect -c "..."  --里面输入命令
expect {...}     --里面的多行记录,从上向下扫描匹配,谁先匹配谁先处理。
interact    ---允许用户交互,停留在切换的机器上
---------------------------------------------------------------------------------
如何从机器A上ssh到机器B上,然后执行机器B上的命令?如何使之自动化完成?

下面一段脚本实现了从机器A登录到机器B,然后执行机器B上的pwd命令,并停留在B机器上,等待用户交互。具体含义请参考上文。
#!/home/tools/bin/64/expect -f
 set timeout -1  
 spawn ssh $BUser@$BHost
 expect  "*password:" { send "$password\r" }
 expect  "$*" { send "pwd\r" }
 interact
----------------expect、send分开一行一行写就可以了。。。。----------------------
yangkai@localhost myshell]$ cat test_dh*
#!/bin/bash/expect -f
set ip 192.168.23.128
set password 123456
set timeout 3
spawn ssh root@$ip
expect {
"passw" {send "$password\r";exp_continue}
#"de password: " {send "$password\r";exp_continue}
#"root@" {send "df -h /opt\r exit\r";exp_continue}
#"root@" {send "df -h /opt\r exit\r"}
"root@" {send "df -h /opt\r"}
"root@" {send "df -h /opt >>/home/yangkai/tt.txt\r";exp_continue}

}
interact
--------------------------最终的----------------------------------
#!/bin/bash/expect -f
set ip 192.168.23.128
set password 123456
set timeout 3
spawn ssh root@$ip
#expect {
#"passw" {send "$password\r";exp_continue}
#"de password: " {send "$password\r";exp_continue}
#"root@" {send "df -h /opt\r exit\r";exp_continue}
#"root@" {send "df -h /opt\r exit\r"}
#"root@" {send "df -h /opt\r"}
#"root@" {send "df -h /opt >>/home/yangkai/tt.txt\r";exp_continue}
#}
expect "passw" 
set timeout 10
send "$password\r"
set timeout 10
#expect "yes/no" {send "yes\r"} 如果匹配不到,等待10秒,执行下面的语句。
expect "root@" {send "df -h /opt\r"}
expect "root@" {send "df -h /opt >>/home/yangkai/tt.txt\r"}
interact

expect.txt

阅读(783) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~