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/bash10 /usr/bin/expect <<EOF11 set timeout 5012 spawn ssh root@$1 13 expect {14         "yes/no"   { send "yes\r";exp_continue }15         "password" { send "redhat\r"}16 }17 expect eof18 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/bash10 Find_Hostname()11 {12 /usr/bin/expect <<EOF13 set timeout 2014 spawn ssh root@$1 hostname15 expect {16         "yes/no" { send "yes\r";exp_continue }17         "password" { send "$2\r" }18 }19 expect eof20 EOF21 }22 23 Max_line=`wc -l $1 | awk '{print $1}'`24 for i in `seq 1 $Max_line`25 do26         IP=$( awk "NR==$i{print \$1}" $1 )27         PASSWD=$( awk "NR==$i{print \$2}" $1 )28         ping -c1 -w1 $IP &>/dev/null29         if 30         [ "$?" = "0" ]31         then32         Find_Hostname $IP $PASSWD | egrep "spawn|password" -v33         fi34 done

脚本执行结果:

[root@server210 lao]# sh get_hostnameinfo.sh hostname_info
localhost.localdomain
foundation51
desktop110.example.com
server210
[root@server210 lao]#

expect语句--shell相关推荐

  1. linux 脚本 expected,使用expect实现shell中scp自动输入密码

    使用expect实现shell中scp自动输入密码 前段时间有一个需求,要实现一个自动备份脚本,因为不需要全部备份,所以没有使用rsync,在这里使用scp.因为scp需要输入用户密码,在网上查了好多 ...

  2. linux expect 输入密码,shell脚本无密码登录 expect的使用方法详解

    shell脚本无密码登录 expect的使用方法详解 今天需要做一个定时任务脚本将最新的数据包文件传到远程的服务器上,虽然有密钥但也是要求输入密码的那种,所以只能另想办法实现让脚本自动输入密码了. 从 ...

  3. 知识点033-利用expect和shell分发密钥之后用ansible统计哪些没有分发成功

    2019独角兽企业重金招聘Python工程师标准>>> 环境准备: linux 操作系统 expect分发密钥 sublime绿色版本 第一阶段:分发密钥 选择一个目录里面新建一个. ...

  4. 使用expect实现shell自动交互

    shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd.scp 对自动部署免去用户交互很痛苦,expect能很好的解决这类问题. expect的核心是 ...

  5. linux脚本case语句,shell中的case语句,数组及函数

    case示例 ---------------------------------------------------------------------- .. echo "case&quo ...

  6. linux 自动化交互套件 expect 介绍 shell非交互

    expect是一个自动化交互套件,主要应用于执行命令和程序时,系统以交互形式要求输入指定字符串,实现交互通信. expect自动交互流程: spawn启动指定进程---expect获取指定关键字--- ...

  7. linux脚本多分支if语句,shell脚本基础应用(二)if单分支,双分支,多分支语句...

    前言:为了使shell脚本具有一定的"判断"能力,根据不同的条件来完成不同的管理任务.使脚本具有一定的"智能". 一.条件测试操作 文件测试常见操作: -d:: ...

  8. linux教程for语句,Shell脚本for循环语句简明教程

    与其他编程语言类似,shell支持for循环. for循环一般格式为: for 变量名 in 列表 do command1 command2 ... commandn done 当变量值在列表里,fo ...

  9. Linux expect与Shell交互

    shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd.scp 对自动部署免去用户交互很痛苦,expect能很好的解决这类问题. expect的核心是 ...

最新文章

  1. 1063 Set Similarity
  2. ELK不香了?企业级日志平台后起之秀 Graylog
  3. 什么是Linux的原生GUI API?
  4. mac下androidStudio 运行模拟器出现:
  5. 8个高效的Python爬虫框架分享
  6. 未捕获typeerror: $形象。cropper不是函数_没有学不会的python--细说函数
  7. [USACO12FEB]牛的IDCow IDs
  8. Pandas 文本数据方法 slice( )
  9. 做餐饮服务员有前途吗
  10. 【c++ primer读书笔记】【第6章】函数
  11. Android APK反编译详解(转)
  12. AI模型的大一统!多模态领域乱杀的十二边形战士
  13. 我从零开始学黑莓开发的过程
  14. DIV+CSS排版技巧
  15. 如何让RS485总线挂接更多数量的设备?
  16. WARNING: --master-data is deprecated and will be removed in a future version
  17. matlab srgb,matlab – 将Photoshop sRGB复制到LAB转换
  18. 【机器学习】谱聚类(Spectral Clustering)
  19. OSPF虚链路(学习笔记+实验验证)
  20. 1697_python编程_assertions and exceptions

热门文章

  1. jaeger 链路追踪
  2. 社交类App如何防黑产垃圾用户?
  3. python儿童编程培训班-儿童编程培训班有用吗
  4. 微信分享官方第三方接入(图片及文字)
  5. mysql索引实战_MySQL索引实战经验总结
  6. python笔记:数据分析的实际应用 工具小记
  7. 美团Java开发实习生面经
  8. 郝健: Linux内存管理学习笔记-第2节课
  9. opencv实现行人检测(C++)
  10. 小虎电商浏览器:拼多多退款对商家有什么影响?如何降低退款率?