expect介绍

借助Expect处理交互的命令,可以将交互 过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成.尤其适用于需 要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率

expect安装

[root@ansible ssh]# rpm -qa | grep expect
       expect-5.45-14.el7_1.x86_64
   [root@ansible ssh]# yum install expect

expect 语法

expect [选项] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]

选项

-c:从命令行执行expect脚本,默认expect是交互地执行的

示例:expect -c 'expect "\n" {send "pressed enter\n"}

-d:可以输出输出调试信息

示例:expect  -d ssh.exp

expect中相关命令

spawn:启动新的进程

send:用于向进程发送字符串

expect:从进程接收字符串

interact:允许用户交互

exp_continue  匹配多个字符串在执行动作后加此命令

expect最常用的语法(tcl语言:模式-动作)

单一分支模式语法:

expect “hi” {send “You said hi\n"}           匹配到hi后,会输出“you said hi”,并换行

多分支模式语法:

expect "hi" { send "You said hi\n" } \ "hehe" { send “Hehe yourself\n" } \ "bye" { send “Good bye\n" }

匹配hi,hello,bye任意字符串时,执行相应输出.等同如下:

expect { "hi" { send "You said hi\n"} "hehe" { send "Hehe yourself\n"} "bye" { send “Good bye\n"} }

自动拷贝文件到远程主机

执行expect 不能以bash file 的方式来执行        (开启一个子shell进程)

必须通过  chmod +x file     ./file  这样的方式    (不会开启子shell进程,只在当前shell环境中执行)

expect 如果只交互一次如拷贝文件   结尾就使用                                            expect eof

如果需要连续交互如登录远程主机执行各种命令结尾就需使用           interact

.安装expect  系统默认没有此命令
yum install expect .创建配置文件
[root@ansible ssh]# vi hosts
192.168.31.134 root root
192.168.31.135 root root
192.168.31.136 root root .编写脚本
[root@ansible ssh]# ls
copykey.sh hosts
[root@ansible ssh]# vi copykey.sh
#!/bin/bash
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
else
echo "id_rsa has created ..."
fi
#分发到各个节点
while read line
do
user=`echo $line | cut -d " " -f `
ip=`echo $line | cut -d " " -f `
passwd=`echo $line | cut -d " " -f `
expect <<EOF
set timeout
spawn ssh-copy-id $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$passwd\n" }
}
expect "password" { send "$passwd\n" }
EOF
done < hosts .给脚本执行权限
chmod +x copykey.sh .执行脚本
./copykey.sh

读取配置文件自动执行ssh

 #!/usr/bin/expect
spawn scp /etc/fstab root@192.168.33.129:/root
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "root\n" }
}
expect eof [root@centos7 ~]# bash one.expect
one.expect: line : spawn: command not found
couldn't read file "{": no such file or directory
one.expect: line : yes/no: No such file or directory
one.expect: line : exp_continue: command not found
one.expect: line : password: command not found
one.expect: line : syntax error near unexpected token `}'
one.expect: line : `}'
[root@centos7 ~]# ./one.expect
spawn scp /etc/fstab root@192.168.33.129:/root
The authenticity of host '192.168.33.129 (192.168.33.129)' can't be established.
RSA key fingerprint is SHA256:FzQU22CgZBnSbmZAuoypliidxPK9PsOFjJwcYUZWk5E.
RSA key fingerprint is MD5:a8:2b::c3:dc:::::d2:d5:e0:9f:e9::1a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.33.129' (RSA) to the list of known hosts.
root@192.168.33.129's password:
fstab
 #!/usr/bin/expect
set ip [lindex $argv ]
set user [lindex $argv ]
set password [lindex $argv ]
set timeout
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect "]#" { send "useradd haha\n" }
expect "]#" { send "echo aaa|passwd --stdin haha\n" }
send "exit\n" expect eof
#./ssh4.exp 192.168.8.100 root aa

执行多条命令

 #!/bin/bash
ip=$
user=$
password=$
expect <<EOF
set timeout
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect "]#" { send "useradd hehe\n" }
expect "]#" { send "echo rrr|passwd --stdin hehe\n" }
expect "]#" { send "exit\n" } expect eof
EOF
#./ssh5.sh 192.168.8.100 root aaa

shell调用expect

最新文章

  1. MVC5网站开发之一 总体概述
  2. MahApps.Metro使用
  3. MySQL 基础及性能优化工具
  4. [IOS初学]ios 第一篇 storyboard 与viewcontroller的关系
  5. php面试题之二——数据结构和算法(高级部分)
  6. IP地址的定义和含义
  7. 《A First Course in Abstract Algebra with Applications》-chaper1-数论
  8. 关于System.out.println()与System.out.print(&quot;\n&quot;)的区别
  9. js 函数(function)
  10. SpringMVC 前端获得定义JSON对象的方法
  11. Android-Bluetooth Low Energy(BLE)
  12. GitLab的安装及使用教程
  13. SkylineGlobe 7.0.1 &amp; 7.0.2版本Web开发 如何正确使用三维地图控件和工程树控件
  14. scrapy选择器归纳
  15. jquery中$.get()提交和$.post()提交的区别
  16. Netty 源码 ChannelHandler(四)编解码技术
  17. 【转】web.xml不同版本的头
  18. 2018-2019-2 20165212《网络攻防技术》Exp5 MSF基础应用
  19. SyncTrayzor -- Windows tray utility / filesystem watcher / launcher for syncthing
  20. webstrom中的快捷键

热门文章

  1. lamp环境配置
  2. Docker 修改国内镜像地址
  3. 静态,关键字:static 接口,IUSB , API 接口关键字:interface
  4. rabbitmq简单实例
  5. SQL server 清除缓存
  6. 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
  7. 内部排序-&gt;其它-&gt;地址排序(地址重排算法)
  8. LeetCode 868 Binary Gap 解题报告
  9. 第三方python 加密库 --- cryptography
  10. pacakge-info.java