expect 介绍

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

expect 安装

[root@ansible ssh]# rpm -qa | grep expect
expect-5.45-14.el7_1.x86_64
[root@ansible ssh]# yum -y 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 "Goodbye\n" } 

        匹配 hi, hehe, bye 中的任意字符串时, 发送相应字符串。等同于:

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

自动拷贝文件到远程主机

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

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

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

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

示例一

1.安装expect  系统默认没有此命令
yum install expect 2.创建配置文件
[root@ansible ssh]# vi hosts
192.168.31.134 root root
192.168.31.135 root root
192.168.31.136 root root 3.编写脚本
[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 2`
ip=`echo $line | cut -d " " -f 1`
passwd=`echo $line | cut -d " " -f 3`
expect <<EOF
set timeout 10
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 4.给脚本执行权限
chmod +x copykey.sh 5.执行脚本
./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 2: spawn: command not found
couldn't read file "{": no such file or directory
one.expect: line 4: yes/no: No such file or directory
one.expect: line 4: exp_continue: command not found
one.expect: line 5: password: command not found
one.expect: line 6: syntax error near unexpected token `}'
one.expect: line 6: `}'
[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:51:c3:dc:09:65:89:78:d2:d5:e0:9f:e9:30: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 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set timeout 10
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=$1
user=$2
password=$3
expect <<EOF
set timeout 10
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

参考: https://www.cnblogs.com/yxh168/p/9028122.html

Shell 中的 expect 命令的更多相关文章

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

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

  2. linux shell 中的sleep命令

    开始还以为是这样的语法: sleep(1), 后面发现是: linux shell 中的sleep命令 分类: LINUX 在有的shell(比如linux中的bash)中sleep还支持睡眠(分,小 ...

  3. shell 中的expect 用法

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

  4. Linux在shell中输入历史命令

    在Linux的shell中,经常输入的命令有很多雷同,甚至是一样的, 如果是长命令,再次敲一遍效率真的是很低, 不过可以通过Ctl+r,  查找history中以前输入的命令,很是好用. 按Ctrl+ ...

  5. 在shell中使用sed命令替换/为\/

    sed命令相关: https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html https://www.cnblogs.com/D ...

  6. 常见linux命令释义(第九天)—— Bash Shell 的操作环境Shell 中的管道命令一些管道命令

    最近好懒好懒.连写文章都写不好了.于是只能写读一点书,记一点读书笔记了.太懒了,没办法.慢慢恢复自己的状态吧. 管道命令仅会处理strandard output 的内容,对于strandard err ...

  7. shell中的ps命令详解

    ps简介:Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的 ...

  8. shell中判断前一个命令是否执行成功

    ]; then echo "fail" else echo "success" fi 或者 ]; then echo "success" e ...

  9. shell中常用的命令方法

    <1>Linux Shell 脚本中字符串的连接方法 [root@localhost company]# var1=/etc/[root@localhost company]# var3= ...

随机推荐

  1. NoSQL之Redis学习笔记

    一.NoSQL与Redis 1.什么是NoSQL? NoSQL=Not Only SQL ,泛指非关系型数据库.随着互联网的兴起,传统的关系型数据库已经暴露了很多问题,NoSQL数据库的产生就是为了解 ...

  2. 团队协作项目——SVN的使用

    参考文献:https://www.cnblogs.com/rwh871212/p/6955489.html 老师接了一个新项目,需要团队共同完成开发任务,因此需要SVN.SVN是C/S架构: 1.服务 ...

  3. 基于Web的质量和测试度量指标

    直观了解软件质量和测试的完整性 VectorCAST/Analytics可提供便于用户理解的web仪表盘视图来显示软件代码质量和测试完整性指标,让用户能够掌握单个代码库的趋势,或对比多个代码库的度量指 ...

  4. 1、Redis简介

    一.NOSQL 1.什么是NOSQL? NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL". 指的是非关系型的数据库.NoSQL有时也称作Not On ...

  5. shell脚本 Linux系统巡检

    一.简介 源码地址 日期:2018/4/12 介绍:非常详细的Linux系统巡检脚本,截图为一部分输出 效果图: 二.使用 适用:centos6+ 语言:中文 注意:无 下载 wget https:/ ...

  6. ciscn_2019_ne_5

    首先checksec和查看多少位的程序 可以看到是32位的程序,放入ida中 进入getflag 可以看到strcpy存在栈溢出,所以大体思路就是输入密码进入选择1造成溢出然后进入选择4获取shell ...

  7. [BUUCTF]REVERSE——简单注册器

    简单注册器 附件 步骤: apk文件,直接用apkide打开 去找反编译后的文件,反编译后的语言并没有看大懂,网上百度后找到了一个反编的神器jeb,下载地址 用它反编译后按tab,就能看懂代码了,搜索 ...

  8. 突破结构限制的“数据透视表”(Excel技巧集团)

    出个题:根据A2:C16生成E2:G18的汇总结果.这里的汇总是求和. 遇到这种情况,首选肯定是函数公式,虽然数据源表是个很规范的一维表,可以用数据透视表,可是想建构到上图那么奇葩,数据透视表无此异能 ...

  9. 我的邮箱客户端程序Popmail

    05年的时候写了一个邮箱客户端程序.当时主要目的是研究POP3和SMTP协议,同时锻炼自己的网络编程能力.当然了,如果自己写的邮箱客户端能够满足自身的日常工作需要,而不是频繁的登录不同的网页邮箱,那就 ...

  10. java 编程基础:注解的功能和作用,自定义注解

    1,什么是注解: 从JDK5开始,Java增加了对元数据 (MetaData)的支持,也就是Annotation注解,这种注解与注释不一样,注解其实是代码里的特殊标记,这些标记可以在编译.类加载 运行 ...