Shell编程之Expect自动化交互程序
一、Expect自动化交互程序
1.spawn命令
通过spawn执行一个命令或程序,之后所有的Expect操作都会在这个执行过的命令或程序进程中进行,包括自动交互功能。
语法:
spawn [ 选项 ] [ 需要自动交互的命令或程序 ]
- -open 表示启动文件进程
- -ignore 表示忽略某些信号
spawn ssh root@192.168.1.1 uptime
2.expect命令
获取spawn命令执行后的信息,看卡是否和其事先指定的相匹配,一旦匹配上指定的内容就执行expect
语法:
expect 表达式 [ 动作 ]
spawn ssh root@192.168.1.1 uptime
expect "*password" {sshd "123456\r"}
3.expect实践
(1)执行ssh命令远程获取服务器负载,并实现自动输入密码
[root@codis-178 ~]# cat 18_1.sh
#!/bin/expect
#Author:xiaoda
#Time:2017-09-06 11:53:30
#Name:18_1.sh
#Version:V1.0
#Description:This is a test script
spawn ssh -p12541 test@192.168.1.22 uptime
expect "*password" {send "123456\n"}
expect eof
[root@codis-178 ~]# expect 18_1.sh
spawn ssh -p12541 test@192.168.1.22 uptime
Address 192.168.1.22 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
test@192.168.1.22's password:
11:56:01 up 1754 days, 23:53, 0 users, load average: 0.00, 0.00, 0.00
说明:
通过spawn执行ssh后,系统会提示输入密码,expect按照匹配password字符串,之后根据send或exp_send动作执行。
(2)执行ssh命令远程获取服务器负载,并自动输入“yes”及用户密码
[root@codis-178 ~]# cat 18_2.sh
#!/bin/expect
#Author:xiaoda
#Time:2017-09-06 11:53:30
#Name:18_1.sh
#Version:V1.0
#Description:This is a test script
spawn ssh -p12541 test@192.168.1.22 uptime
expect {
"yes/no" {exp_send "yes\r";exp_continue}
"*password" {exp_send "12345465\r"}
}
expect eof
[root@codis-178 ~]# expect 18_2.sh
spawn ssh -p12541 test@192.168.1.22 uptime
Address 192.168.1.22 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
test@192.168.1.22's password:
12:03:18 up 1755 days, 1 min, 0 users, load average: 0.00, 0.00, 0.00
(3)利用expect响应Shell脚本中的多个read读入
[root@codis-178 ~]# cat 18_3_1.sh
#!/bin/bash
#Author:xiaoda
#Time:2017-09-06 12:05:38
#Name:18_3_1.sh
#Version:V1.0
#Description:This is a test script
read -p 'Please input your username:' name
read -p 'Please input your password:' pass
read -p 'Please input your email:' email
echo -n "your name is $name."
echo -n "your password is $pass."
echo "your email is $email."
[root@codis-178 ~]# cat 18_3_2.sh
#!/bin/expect
#Author:xiaoda
#Time:2017-09-06 12:07:37
#Name:18_3_2.sh
#Version:V1.0
#Description:This is a test script
spawn /bin/sh 18_3_1.sh
expect {
"username" {exp_send "oldboy\r";exp_continue}
"*pass*" {send "123456\r";exp_continue}
"*email*" {exp_send "12313@.qq.com\r"}
}
expect eof'
[root@codis-178 ~]# expect 18_3_2.sh
spawn /bin/sh 18_3_1.sh
Please input your username:oldboy
Please input your password:123456
Please input your email:12313@.qq.com
your name is oldboy.your password is 123456.your email is 12313@.qq.com.
4.命令简介
(1)send命令
发送指定的字符串给系统
(2)exp_continue命令
让Expect程序继续匹配
(3)send_user命令
用来打印Expect脚本信息,类似echo
(4)exit命令
直接退出Expect脚本
5.Expect关键字
(1)eof
用于匹配结束符
(2)timeout
控制时间,是一个全局性的时间控制开关,可以通过为这个变量赋值来规定整个Expect操作的时间
(即使命令没有任何错误,到了时间仍然会激活这个变量)
spqwn ssh root@192.168.1.1 uptime
set timeout 30
expect "yes/no" {exp_send "yes\r";exp_continue}
expect timeout {puts "Request timeout by oldboy.";return}
spqwn ssh root@192.168.1.1 uptime
expect {
-timeout 3
expect "yes/no" {exp_send "yes\r";exp_continue}
expect timeout {puts "Request timeout by oldboy.";return}
}
6.企业应用
(1)实现Expect自动交互
#!/usr/bin/expect
if [ $argc != 2 ] {
puts "usage: expect $argv0 ip command"
exit
}
set ip [lindex $argv 0]
set cmd [lindex $argv 1]
set password "123456"
spawn ssh root@192.168.1.1 $cmd
expect {
expect "yes/no" {exp_send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
export eof
(2)批量发送文件
#!/usr/bin/expect
if [ $argc != 3 ] {
puts "usage: expect $argv0 file host dir"
exit
}
set file [lindex $argv 0]
set host [lindex $argv 1]
set dir [lindex $argv 2]
set password "123456"
spawn ssh -P22 root@192.168.1.1 $cmd
expect {
expect "yes/no" {exp_send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
export eof
#!/bin/bash
if [ $# -ne 2 ];then
echo $"Usage:$0 file dir"
exit 1
fi
file=$1
dir=$2
for n in 128 129 130
do
expect 18_13_1.exp $file 192.168.1.$n $dir
done
Shell编程之Expect自动化交互程序的更多相关文章
- Expect自动化交互程序
Expect介绍: 1.什么是Expect Expect是一个用来实现自动化交互功能的软件套件,基于TCL的脚本编程工具语言,方便学习,功能强大. 2.为什么要使用expcet: 当今的企业运维中,自 ...
- shell编程之awk命令详解
shell编程之awk命令详解 a:focus { outline: thin dotted #333; outline: 5px auto -webkit-focus-ring-color; out ...
- 03 shell编程之case语句与函数
本文所有内容均来自当年博主当年学习笔记,若有不足欢迎指正 Shell编程之case语句与函数 学习目标: 掌握case语句编程 掌握shell函数的使用 目录结构: Case语句 Case语句的作用 ...
- shell编程之case分支语句
shell编程之case分支语句 case分支语句和if的多分支语句很相似. if多分支语句一般用在有(区间范围)的地方 :例如:0-100之间. if需要判断多个不同的条件. case的分支语句用在 ...
- shell编程之if语句
shell编程之if判断 目录 shell编程之if判断 1.整数比较 2.字符串比较 3.举例 1.数字比较 2.字符串比较 4.Other 1.整数比较 -eq 等于,如:if [ "$ ...
- Shell基础(四):字符串截取及切割、字符串初值的处理、基使用Shell数组、expect预期交互、使用正则表达式
一.字符串截取及切割 目标: 使用Shell完成各种Linux运维任务时,一旦涉及到判断.条件测试等相关操作时,往往需要对相关的命令输出进行过滤,提取出符合要求的字符串. 本案例要求熟悉字符串的常见处 ...
- shell编程之sed编辑器&gawk程序
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/560372 s ...
- Shell编程之Shift的用法
位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1. 非常 ...
- shell编程之SHELL基础(1)
shell脚本基础 shell是一个命令行解释器,她为互用提供了一个想linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动.挂起.停止甚至编写一些程序. shell还是一个功 ...
随机推荐
- Servlet 发送电子邮件
使用 Servlet 发送一封电子邮件是很简单的,但首先您必须在您的计算机上安装 JavaMail API 和 Java Activation Framework)JAF). 您可以从 Java 网站 ...
- Eclipse 添加书签
Eclipse 添加书签 关于书签 Eclipse 中可以在编辑器的任意一行添加书签. 您可以使用书签作为提示信息,或者使用书签快速定位到文件中的指定的行. 添加书签 如果你想设置书签,你只需要在垂直 ...
- ios [__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x7a97d4c0'报错
今天接口由get换成post,我去改进行登录但出现了这个错误,首先出错先看能不能与服务器交互,能不能获得数据,其次,获得的数据是不是你想要的,记住,首先出错要想到是自己的问题,还有就是程序崩了要学会自 ...
- Python gevent学习笔记-2
在上一篇里面介绍了gevent的最主要的功能,先来来了解一下gevent里面一些更加高级的功能. 事件 事件是一种可以让greenlet进行异步通信的手段. ? 1 2 3 4 5 6 7 8 9 1 ...
- PHP实现自己活了多少岁
1.mktime()函数的功能 2.代码: $birth = mktime(0,0,0,10,2,1992);//出生的时间戳 $time = time();//当前的时间戳 $age = floor ...
- 线程中sleep和wait区别
sleep和wait的区别有: 1,这两个方法来自不同的类分别是Thread和Object 2,最主要是sleep方法没有释放锁,而wait方法释放了锁,使得敏感词线程可以使用同步控制块或者方法. 3 ...
- Carrot2 in action 初步印象
RawCluster:聚类中的类别单位 RawCluster.getDocuments():获得该类的文档列表 RawDocument:每个类的文档单位 STC:后缀树表示法 2008-11-13 C ...
- Achartengine.jar绘制动态图形-饼图
Achartengine.jar绘制动态图形一 --饼图 PS:我们在做安卓程序的时候,免不了会做一些图形,自己可以选择自定义view ,就是用Canvas画,也可以用写好的jar包,就是achart ...
- Springboot中jar 重复冲突 导致 静态资源加载问题!
这个jar 其实在common 中也是存在的 ,当时没注意看,就导入进来了,然后 css js 等一些静态资源全部不能加载!具体原因我没去深挖!后面找个时间深挖下,先填坑!
- Python3.6全栈开发实例[025]
25.文件a1.txt内容(升级题)name:apple price:10 amount:3 year:2012name:tesla price:100000 amount:1 year:2013通过 ...