shell中expect介绍
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介绍的更多相关文章
- shell中expect免交互
expect前言观察ssh登录的交互现象有些程序难以避免的需要交互.你该如何解决脚本与程序的交互问题?名词解释期待, 预期, 盼望, 预料,料想, 指望, 希望, 要求,想, 认为一.概述 我们通过S ...
- Shell 中的 expect 命令
目录 expect 介绍 expect 安装 expect 语法 自动拷贝文件到远程主机 示例一 示例二 示例三 示例四 expect 介绍 借助 expect 处理交互的命令,可以将交互过程如 ss ...
- shell 中的expect 用法
expect一般用于实现用脚本来自动远程登录,对远程机器执行相关操作 测试机上的expect目录一般在/usr/bin/expect路径 下面是从网上查询的用法总结: 1. expect中的判断语句: ...
- shell中使用expect命令进行远程执行命令脚本
expect是用来实现自动交互功能的工具之一,使用expect-send来实现交互过程. 注意: 1.脚本的执行方法与bash shell不一样,比如:expect example.sh 2.向一个脚 ...
- 介绍下Shell中的${}、##和%%使用范例,本文给出了不同情况下得到的结果。
介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...
- /usr/bin/expect介绍
/usr/bin/expect介绍 http://blog.csdn.net/zhu_tianwei/article/details/44180637 概述 我们通过Shell可以实现简单的控制流功能 ...
- Shell 信号处理 & Expect 免交互
监控脚本项目 信号处理 1 什么是信号 由键盘组合键或者 kill 命令发出操作称之为信号 信号是发送给进程的,进程在收到信号后会作出默认的响应 2 为何要在进程内处理信号 进程在收到信号后会有默认的 ...
- shell中读写mysql数据库
本文介绍了如何在shell中读写mysql数据库.主要介绍了如何在shell 中连接mysql数据库,如何在shell中创建数据库,创建表,插入csv文件,读取mysql数据库,导出mysql数据库为 ...
- 理解 Linux shell 中的一个方言:2>&1
理解 Linux shell 中的一个方言:2>&1 2016-11-14 杜亦舒 前言 在使用 linux 命令或者 shell 编程时,这个用法常会遇到 2>&1 如 ...
随机推荐
- 07装饰模式Decorator
一.什么是装饰模式 装饰( Decorator )模式又叫做包装模式.通 过一种对客户端透明的方式来扩展对象的功能, 是继承关系的一个替换方案. 二.装饰模式的结构 三.装饰模式的角色和职责 抽象组件 ...
- jquery-ui弹框登录前端写法
新建一个div: <div class="container" id="loginForm" hidden> <h4 class=" ...
- UILabel(富文本)
本文转载至 http://www.jianshu.com/p/5d24d22f99c3 富文本 NSString *str = @"人生若只如初见,何事秋风悲画扇.\n等闲变却故人心,却道故 ...
- 关于思科C2950交换机清除密码,恢复初始配置的方法
上个月河南做项目,因需要大批量的对服务器进行操作系统的安装,于是想到了PXE网络批量安装, 好不容易到机房的仓库找到网线及一台思科交换机,但到安装的时候,发现思科交换机里应该有配置了 通过配置线连接交 ...
- win10 开启蓝 由于其配置信息(注册表中的)不完整或已损坏
在管理员命令提示符下键入以下命令: Dism /Online /Cleanup-Image /ScanHealth 这条命令将扫描全部系统文件并和官方系统文件对比,扫描计算机中的不一致情况. Dism ...
- dts中memreserve和reserved-memory的区别 (转)
https://blog.csdn.net/kickxxx/article/details/54631535
- Ubuntu将网卡名称eno160改为eth0并且设置静态IP
修改配置文件/etc/default/grub GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0" 设置生效 update-grub ...
- Red Hat6设置使用CentOS的yum源
环境查看 red hat系统使用自己默认的yum源未注册在使用yum安装软件的时候会出现以下错误提示 可以修改成centos的yum源 卸载yum软件 rpm -qa|grep yum|xargs r ...
- Linux系统下公式编辑器KLatexFormula
方法1:源码安装 https://blog.csdn.net/ouening/article/details/79008636 方法2:通过apt-get 安装 首先安装libqt4-sql-sqli ...
- ubuntu 搜狗输入法 在中断失效
实测是更换了皮肤后,出现在中断输入故障,乱码.在其他界面可能是正常的,更换语言没用. killall fcitx输入法突然好了(根据网友所说,更改一堆东西貌似并没有什么用) 此时关闭输入法皮肤一切正常 ...