《云计算》-shell脚本编程-expect预期交互

expect预期交互
3.1 问题

本案例要求编写一个expect脚本,实现SSH登录的自动交互:

提前准备好目标主机,IP地址为192.168.4.5
用户名为mike、密码为1234567
执行脚本后自动登入,并且在目标主机建立测试文件 /tmp/mike.txt
  • 1
  • 2
  • 3

3.2 方案

expect可以为交互式过程(比如FTP、SSH等登录过程)自动输送预先准备的文本或指令,而无需人工干预。触发的依据是预期会出现的特征提示文本。

储备知识(发送邮件的几种方式):

[root@svr5 ~]# echo "test mail" | mail -s test root
[root@svr5 ~]# mail -s test root < /etc/passwd
[root@svr5 ~]# mail -s test root << EOF
test mail
hell world
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:准备expect及SSH测试环境

1)安装expect工具

[root@svr5 ~]# yum  -y  install  expect                  //安装expect
.. ..
Installed:
  expect.x86_64 0:5.44.1.15-5.el6_4                                                            
Dependency Installed:
  tcl.x86_64 1:8.5.7-6.el6 
[root@svr5 ~]# which expect                              //确认expect路径
/usr/bin/expect
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

步骤二:编写expect_ssh脚本,实现免交互登录

1)任务需求及思路分析

在SSH登录过程中,如果是第一次连接到该目标主机,则首先会被要求接受密钥,然后才提示输入密码:

[root@svr5 ~]# ssh mike@192.168.4.5                              //连接目标主机
The authenticity of host '192.168.4.5 (192.168.4.5)' can't be established.
RSA key fingerprint is 58:a0:d6:00:c7:f1:34:5d:6c:6d:70:ce:e0:20:f8:f3.
Are you sure you want to continue connecting (yes/no)? yes          //接受密钥
Warning: Permanently added '192.168.4.5' (RSA) to the list of known hosts.
mike@192.168.4.5's password:                                   //验证密码
Last login: Thu May  7 22:05:44 2015 from 192.168.4.5
[mike@svr5 ~]$ exit                                             //返回客户端
logout
Connection to 192.168.4.5 closed.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

当然,如果SSH登录并不是第一次,则接受密钥的环节就没有了,而是直接进入验证密码的过程:

[root@svr5 ~]# ssh mike@192.168.4.5                              //连接目标主机
mike@192.168.4.5's password:                                   //验证密码
Last login: Mon May 11 12:02:39 2015 from 192.168.4.5
[mike@svr5 ~]$ exit                                             //返回客户端
logout
Connection to 192.168.4.5 closed.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2)根据实现思路编写脚本文件

脚本内容参考如下版本1:

[root@svr5 ~]# vim  expect_ssh.sh 
#!/bin/bash
expect << EOF
spawn ssh 172.25.0.10                               #//创建交互式进程
expect "password:" { send "123456\r" }              #//自动发送密码
expect "#   { send "pwd > /tmp/$user.txt \r" }      #//发送命令
expect "#"  { send "exit\r" }
EOF
[root@svr5 ~]# chmod  +x  expect_ssh.sh 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

通过循环批量操作,版本2:

[root@svr5 ~]# vim  expect_ssh.sh 
#!/bin/bash
for i in 10 11
do
expect << EOF
spawn ssh 172.25.0.$i                               #//创建交互式进程
expect "password:" { send "123456\r" }              #//自动发送密码
expect "#   { send "pwd > /tmp/$user.txt \r" }      #//发送命令
expect "#"  { send "exit\r" }
EOF
done
[root@svr5 ~]# chmod  +x  expect_ssh.sh 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

注意事项:

expect脚本的最后一行默认不执行

如果不希望ssh时出现yes/no的提示,远程时使用如下选项:

ssh -o StrictHostKeyChecking=no server0

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了357 篇原创文章</span> · <span>获赞 54</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
发布了272 篇原创文章 · 获赞 272 · 访问量 8145
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览