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

shell中expect介绍的更多相关文章

  1. shell中expect免交互

    expect前言观察ssh登录的交互现象有些程序难以避免的需要交互.你该如何解决脚本与程序的交互问题?名词解释期待, 预期, 盼望, 预料,料想, 指望, 希望, 要求,想, 认为一.概述 我们通过S ...

  2. Shell 中的 expect 命令

    目录 expect 介绍 expect 安装 expect 语法 自动拷贝文件到远程主机 示例一 示例二 示例三 示例四 expect 介绍 借助 expect 处理交互的命令,可以将交互过程如 ss ...

  3. shell 中的expect 用法

    expect一般用于实现用脚本来自动远程登录,对远程机器执行相关操作 测试机上的expect目录一般在/usr/bin/expect路径 下面是从网上查询的用法总结: 1. expect中的判断语句: ...

  4. shell中使用expect命令进行远程执行命令脚本

    expect是用来实现自动交互功能的工具之一,使用expect-send来实现交互过程. 注意: 1.脚本的执行方法与bash shell不一样,比如:expect example.sh 2.向一个脚 ...

  5. 介绍下Shell中的${}、##和%%使用范例,本文给出了不同情况下得到的结果。

    介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...

  6. /usr/bin/expect介绍

    /usr/bin/expect介绍 http://blog.csdn.net/zhu_tianwei/article/details/44180637 概述 我们通过Shell可以实现简单的控制流功能 ...

  7. Shell 信号处理 & Expect 免交互

    监控脚本项目 信号处理 1 什么是信号 由键盘组合键或者 kill 命令发出操作称之为信号 信号是发送给进程的,进程在收到信号后会作出默认的响应 2 为何要在进程内处理信号 进程在收到信号后会有默认的 ...

  8. shell中读写mysql数据库

    本文介绍了如何在shell中读写mysql数据库.主要介绍了如何在shell 中连接mysql数据库,如何在shell中创建数据库,创建表,插入csv文件,读取mysql数据库,导出mysql数据库为 ...

  9. 理解 Linux shell 中的一个方言:2>&1

    理解 Linux shell 中的一个方言:2>&1  2016-11-14 杜亦舒 前言 在使用 linux 命令或者 shell 编程时,这个用法常会遇到 2>&1 如 ...

随机推荐

  1. python 迭代器模式

    迭代器模式 迭代器模式(Iterator Pattern)是 Java 和 .Net 编程环境中非常常用的设计模式.这种模式用于顺序访问集合对象的元素,不需要知道集合对象的底层表示. 迭代器模式属于行 ...

  2. [Laravel] 08 - Auth & Data Migration

    登录注册框架 一.加载Auth模块 Step 1, 安装Auth模块 生成相关 laravel 框架内部的代码模块: $ php artisan make:auth 自动添加了路由代码 在larave ...

  3. Android数据解析——JSON

    示例一: 有这样一个JSON需要解析,比如: {"thresholds": {"1e-3": 65.3,"1e-5": 76.5," ...

  4. Windows Server 2008 R2 64位操作系统安装Oracle 11g 64位数据库,在客户终端上安装Oracle 11g 32位,才能安装P/L Sql Developer并配置

    1.下载Oracle 11g R2 for Windows的版本 下载地址: http://www.oracle.com/technetwork/database/enterprise-edition ...

  5. cordova 企业应用打包Archive的时候报 "#import <Cordova file not found"

    可能原因是Cordova的路径问题: For xcode7 add "$(OBJROOT)/UninstalledProducts/$(PLATFORM_NAME)/include" ...

  6. html5__Notifications API 桌面通知

    MDN地址 google 文档 https://developers.google.cn/web/fundamentals/push-notifications/ const koa2 = requi ...

  7. Saltstack之通过grains在配置文件中赋值

    案例,使用salt给客户端安装zabbix agent时需要在配置文件中自动生成主机名信息 zabbix agent安装sls zabbix-agent-install: file.managed: ...

  8. 通过Rabbitmq从ipone手机传输imu和相机数据到电脑端

    https://github.com/tomas789/iOSmsg_client https://github.com/tomas789/iOSmsg 通过xcode工具把iosmsg打包发布到ip ...

  9. sql语句 isnull(列名,'')='' /STUFF的意思

    (1) SELECT  SYXH,ZYHM,YEXH,ISNULL(YETZ,'') AS YETZ ,RYKSMC,RYBQMC,HZXM FROM YG_BRSYK 如果列名数据等于NULL,那么 ...

  10. Arcengine 在SDE创建数据集提示应用程序未获得创建或修改此类型数据的方案的许可

    解决方案:将license Control的属性修改一下,ArcGIS Engine的√去掉,把第二个打√就可以了: 参考资料:http://www.docin.com/p-925448534.htm ...