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. post请求参数设置

    控制器参数有[FromBody]修饰参数这么传: 控制器没有[FromBody]修饰参数这么传:

  2. Window应急响应(四):挖矿病毒

    0x00 前言 ​ 随着虚拟货币的疯狂炒作,挖矿病毒已经成为不法分子利用最为频繁的攻击方式之一.病毒传播者可以利用个人电脑或服务器进行挖矿,具体现象为电脑CPU占用率高,C盘可使用空间骤降,电脑温度升 ...

  3. iOS开发-- Xcode 6单元测试

    占坑 http://m.oschina.net/blog/377800 http://www.cnblogs.com/sunshine-anycall/p/4155649.html http://ob ...

  4. js for in 获得遍历数组索引和对象属性

    for in 遍历对象属性 获取的是对象的属性名 var person ={ name:"admin", age:"21", address:"sha ...

  5. windows上测试磁盘io性能

    一.问题由来 前两天搭建一套演示环境,同样的java war包,放在我们这边服务器好好的,放在那边就运行缓慢. 后来把日志改成异步之后就好了. 后边找了个程序测了下io性能,竟然差了7,8倍. 二.软 ...

  6. C# 反射(Reflection)

    什么是反射 发射是 .net framework 提供的一个帮助类库,用于读取和使用元数据. 用到的类:System.Reflection,System.Type.System.Type 类对于反射起 ...

  7. JavaBean 和 pojo 的区别

    JavaBean 是一种JAVA语言写成的可重用组件.它的方法命名,构造及行为必须符合特定的约定: 这个类必须有一个公共的缺省构造函数. 这个类的属性使用getter和setter来访问,其他方法遵从 ...

  8. MySql 远程连接的条件

    1.首先看服务器防火墙 引用:http://www.cnblogs.com/silent2012/archive/2015/07/28/4682770.html CentOS 7.0默认使用的是fir ...

  9. 专访姚冬:All-in-One,智能时代下企业需要更快速的变革

    2017年,msup将咨询服务列入公司发展战略目标,并邀请前IBM大中华区技术总监姚冬成为咨询合伙人.近一年来,msup在咨询服务方面持续发力,与包括百度.平安科技.用友等在内的大型公司形成企业合作联 ...

  10. [No000016C]做企业分析的三个重要工具

    个工具>这本书,内容很基础,但是逻辑很清晰.里面介绍了三个常用的分析方法:pest分析法.波特五力模型以及swot分析法,为脑袋里没有分析思路的人提供了分析框架. 故将这三个分析方法做成了思维导 ...