expect语句--shell

expect

expect 是自动应答命令用于交互式命令的自动执行
spawn 是 expect 中的监控程序,其运行后会监控命令提出的交互问题
send发送问题答案给交互命令
“\r”表示回车
exp_continue 标示当问题不存在时继续回答下面的问题
expect eof 标示问题回答完毕退出 expect 环境
interact标示问题回答完毕留在交互界面
set NAME [ lindex $argv n ]定义变量

实验:

vim ask.sh
#!/bin/bash
read -p "what\'s yourname?:" NAME
read -p "how ols are you?:" AGE
read -p "which class you study?:" CLASS
read -p "Are you fell today?:" FELL
echo $NAME is $AGE ,study $CLASS and fell $FELL

chmod +x ask.sh
编辑回答脚本anser.sh

#!/bin/bash
sh /mnt/ask.sh <<eof
tom
18
linux_1
happy
eof

编写exp脚本

#!/usr/bin/expect
spawn /mnt/ask.sh
expect {
       name { send "tom\r";exp_continue }
       age { send "18\r";exp_continue }
       class { send "linux_1\r";exp_continue }
      fell { send "happy\r";}
}
interact

完成后执行脚本anser.sh

tom is 18 ,study linux_1 and fell happy



注释掉ask.sh脚本中的2,3问

#!/bin/bash
read -p "what\'s yourname?:" NAME
#read -p "how ols are you?:" AGE
#read -p "which class you study?:" CLASS
read -p "Are you fell today?:" FELL
echo $NAME is $AGE ,study $CLASS and fell $FELL

执行脚本anser.sh

tom is ,study and fell 18

执行脚本后ssh自动连接

 9 #!/bin/bash
 10 /usr/bin/expect <<EOF
 11 set timeout 50
 12 spawn ssh root@$1 
 13 expect {
 14         "yes/no"   { send "yes\r";exp_continue }
 15         "password" { send "redhat\r"}
 16 }
 17 expect eof
 18 EOF

脚本执行结果
sh .au.sh 172.25.254.51

[root@server210 lao]# sh  au.sh 172.25.254.51
spawn ssh root@172.25.254.51
root@172.25.254.51's password: 
Last login: Fri Mar  9 21:33:33 2018 from www10.example.com
[root@foundation51 ~]#

sh .au.sh 172.25.254.10

[root@server210 lao]# sh  au.sh 172.25.254.10
spawn ssh root@172.25.254.10
root@172.25.254.10's password: 
Last failed login: Fri Mar  9 08:31:00 EST 2018 from 172.25.254.210 on ssh:notty
There were 13 failed login attempts since the last successful login.
Last login: Fri Mar  2 08:31:22 2018
ABRT has detected 2 problem(s). For more info run: abrt-cli list --since 1519997500
[root@localhost ~]# 

实验3:显示所有主机名

#!/bin/bash
Find_Hostname()
{
/usr/bin/expect <<EOF
set timeout 50
spawn ssh root@$1 hostname
expect {
        "yes/no"   { send "yes\r";exp_continue } 
        "password" { send "redhat\r"}
}
expect eof
EOF
}
for NUM in {1..255}
do ping -c1 -w1 172.25.254.$NUM &>/dev/null && {
     Find_Hostname 172.25.254.$NUM | grep -E "spawn | password" -v
}
done

执行结果

[root@server210 lao]# sh au.sh
localhost.localdomain
foundation51
desktop110.example.com
The authenticity of host '172.25.254.210 (172.25.254.210)' can't be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.210' (ECDSA) to the list of known hosts.
server210

脚本 添加关键词过滤使执行结果更明显Find_Hostname 172.25.254.$NUM | grep -E “spawn | password|authenticity|ECDSA|connecting|Warning” -v
这里写图片描述

这是在密码相同的情况下,要是密码不一样

新建文件hostname_info,写入ip和对应的root密码

[root@server210 lao]# vim hostname_info
[root@server210 lao]# cat hostname_info 
172.25.254.10  lee
172.25.254.51  redhat
172.25.254.110 linux
172.25.254.210 123

编写脚本

 9 #!/bin/bash
 10 Find_Hostname()
 11 {
 12 /usr/bin/expect <<EOF
 13 set timeout 20
 14 spawn ssh root@$1 hostname
 15 expect {
 16         "yes/no" { send "yes\r";exp_continue }
 17         "password" { send "$2\r" }
 18 }
 19 expect eof
 20 EOF
 21 }
 22 
 23 Max_line=`wc -l $1 | awk '{print $1}'`
 24 for i in `seq 1 $Max_line`
 25 do
 26         IP=$( awk "NR==$i{print \$1}" $1 )
 27         PASSWD=$( awk "NR==$i{print \$2}" $1 )
 28         ping -c1 -w1 $IP &>/dev/null
 29         if 
 30         [ "$?" = "0" ]
 31         then
 32         Find_Hostname $IP $PASSWD | egrep "spawn|password" -v
 33         fi
 34 done

脚本执行结果:

[root@server210 lao]# sh get_hostnameinfo.sh hostname_info 
localhost.localdomain
foundation51
desktop110.example.com
server210
[root@server210 lao]# 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值