shell—expect工具
本文最后更新于 209 天前,如有失效请评论区留言。

Auther | CuckooYang

Expect

expect的安装

[root@qfedu ~] yum -y install expect

expect的语法

是一个免费的编程工具, 用来实现自动的交互式任务, 而无需人为干预. 说白了 expect 就是一套用来实现自动交互功能的软件

在实际工作中我们运行命令、脚本或程序时, 都需要从终端输入某些继续运行的指令,而这些输 入都需要人为的手工进行. 而利用 expect 则可以根据程序的提示, 模拟标准输入提供给程序, 从而实现自动化交互执 行. 这就是 expect

如果能够在工作中熟练的使用Shell脚本就可以很大程度的提高工作效率, 那么再搭配上expect这个时候很多工作都可以实现自动化进行。

用法: 
1)定义expect脚本执行的shell
        #!/usr/bin/expect     -----类似于#!/bin/bash 
2)set timeout 30
        设置超时时间30s 
3)spawn
        spawn是执行expect之后后执行的内部命令开启一个会话 #功能:用来执行shell的交互命令
4)expect ---相当于捕捉
        功能:判断输出结果是否包含某项字符串(相当于捕捉返回的结果),没有则会断开,否则等待一段时间后返回,等待通过timeout设置 
5)send
        执行交互动作,将交互要执行的命令进行发送给交互指令,命令字符串结尾要加上“\r”,#---相当于回车
6)interract 
        执行完后保持交互状态,需要等待手动退出交互状态,如果不加这一项,交互完成会自动退出
7)exp_continue 
        继续执行接下来的操作

实战非交互式ssh连接

案例1:普通操作
[root@qfedu script]# vim expect01.sh
#!/usr/bin/expect
spawn ssh root@192.168.246.115

expect {
        "yes/no" {  send "yes\r"; exp_continue }
        "password:" { send "1\r" };
}
interact
[root@qfedu script]# chmod +x expect01.sh
[root@qfedu script]# ./expect01.sh
spawn ssh root@192.168.246.115
root@192.168.246.115's password: 
Last login: Fri Aug 28 16:57:09 2019

#如果添加interact参数将会等待我们手动交互进行退出。如果不加interact参数在登录成功之后会立刻退出。
============================================================================
2.设置变量与进行传参的方式
#注意:expect环境中设置变量用set,识别不了bash方式定义的变量 

[root@qfedu script]# vim expect01.sh
#!/usr/bin/expect
set user root
set pass 1
set ip [ lindex $argv 0 ]  #expect固定写法,从0开始,第一个位置参数,相当于shell中的$1
set timeout 10

spawn ssh $user@$ip
expect {
        "yes/no" {  send "yes\r"; exp_continue }
        "password:" { send "$pass\r" };
}
interact

[root@qfedu script]# ./expect01.sh 192.168.246.115
spawn ssh root@192.168.246.115
root@192.168.246.115's password: 
Last login: Fri Aug 28 07:13:57 2019 from 192.168.246.135

#如果想登录成功自动结束交互模式也就是expect,可以采用下面方式:
expect "#"
send "useradd test\r"
send "pwd\r"
send "exit\r"
expect eof  #直接退出expect模式
============================================================================
3.进行批量推送公钥实现免密连接,ping通一个ip地址连接一个ip------!!!!扩展
[root@qfedu script]# vim getip_push.sh
#!/usr/bin/bash
pass=1
#判断expect命令是否安装
rpm -qa expect &> /dev/null
if [ $? -ne 0 ];then
        yum install -y expect
fi

#判断主机下面是否生成秘钥,如果没有生成秘钥
if [ ! -f ~/.ssh/id_rsa ];then
        ssh-keygen -P "" -f ~/.ssh/id_rsa
fi

#循环执行获取up状态的ip地址。
for i in {2..254}
do
        {
        ip=192.168.198.$i
        ping -c1 $ip &> /dev/null
        if [ $? -eq 0 ];then
                echo "$ip" >> up_ip.txt
                set timeout 10
                /usr/bin/expect <<-EOF   #shell脚本中调用expect命令
                spawn ssh-copy-id $ip
                expect {
                        "yes/no" { send "yes\r"; exp_continue }
                        "password:" { send "$pass\r" };
                }
                expect eof
                EOF
        fi      
        } &
wait
done
echo "finish..."

[root@qfedu script]# chmod +x getip_push.sh 
[root@qfedu script]# ./getip_push.sh

测试....

---END

版权声明:除特殊说明,博客文章均为cuckooyang原创,依据CC BY-SA 4.0许可证进行授权,转载请附上出处链接及本声明。 | 博客订阅:RSS | 广告招租:留言板 | 博客VPS |
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇