• 为什么写博客,是因为遇到的坑,不希望别人在踩一遍!
  • 选择了,剩下的就是坚持和努力-------致自己!
  • 当能力达不到梦想时,更需要学习,努力,拼搏

Expect 使用详解

Linux运维 雪豹 7年前 (2017-08-31) 1178次浏览 0个评论

第1章 expect 概括

expect 期待
expect是Unix系统中用来进行自动化控制和测试的软件工具,由Don Libes制作,作为Tcl脚本语言的一个扩展,应用在交互式软件中如telnet,ftp,Passwd,fsck,rlogin,tip,ssh等等。该工具利用Unix伪终端包装其子进程,允许任意程序通过终端接入进行自动化控制;也可利用Tk工具,将交互程序包装在X11的图形用户界面中。
我们通过Shell可以实现简单的控制流功能,如:循环、判断等。但是对于需要交互的场合则必须通过人工来干预,比如普通用户使用sudo命令时就需要我们手动输入密码;expect就是能够完成这种自动交互任务,而无需人的干预。

第2章 使用总结:

为什么先写总结,刚才是自己使用expect也纠结了一会,总结下,在结合下面的两个案例
1、需要注意先规划好expect 大概结构,实现效果,需要参数,路径、命令

images
2、写shell脚本注意shell脚本中的变量需要对于expect中的变量.

images

2.1 使用例子

2.2 首先安装expect

Centos OS yum 安装 
yum install  -y expect
Ubuntu  系统安装
apt-get install expect

2.3 例子一:SSH 自动远程支持mkdir

#提示写expect 需要两个脚本一个 .exp 和 .sh 如下:
#创建一个expect自动执行脚本
root@xuebao shell]# cat expect_mkdir.exp 
#!/usr/bin/expect  
set date [lindex $argv 0]  
set password [lindex $argv 1]   
#spawn scp $src_file $username@$host:$dest_file  
spawn ssh 192.20.3.99 mkdir /home/tbt/webappdata/backup/$date
expect {
    "yes/no" {send "yes\r";exp_continue}
     "*password" {send "$password\r"}
}   
expect eof

#脚本解释

[root@xuebao shell]# cat expect_mkdir.exp 
#!/usr/bin/expect   #解释器,告诉操作系统,使用expect必须加。
set date [lindex $argv 0]  # expect脚本可以接受从shell 脚本中传递过来的参数.可以使用n从0开始,分别表示第一个,第二个,第三个....参数
set password [lindex $argv 1]   #从shellz中传递密码
spawn ssh 192.20.3.99 mkdir /home/tbt/webappdata/backup/$date   # spawn后面加上需要执行的shell命令、其中$date 是加的shell脚本中的时间变量
expect {
    "yes/no" {send "yes\r";exp_continue}  #行交互动作,与手工输入密码的动作等效。 
     "*password" {send "$password\r"}   #行交互动作,与手工输入密码的动作等效。 
}   
expect eof
注意:expect脚本必须以expect eof结束。

2.4 相对于的shell脚本

[root@xuebao shell]# cat expect_mkdir.sh 
#!/bin/sh  
#####################
#by xuebao
#2017.05.27
#####################
date=`date +%Y%m%d`  #定义了一个时间变量
password=123456     #传递密码
cd /home/shell     #进入存放expect_mkdir.exp 的目录
./expect_mkdir.exp $date $password   #执行并传参

2.5 例子2 自动SCP 命令

[root@xuebao shell]# cat expect_app.exp 
#!/usr/bin/expect  
set host [lindex $argv 0]  
set port [lindex $argv 1]  
set username [lindex $argv 2]  
set password [lindex $argv 3]  
set src_file [lindex $argv 4]  
set dest_file [lindex $argv 5]  
#spawn scp $src_file $username@$host:$dest_file  
spawn scp -P $port -r $src_file $username@$host:$dest_file 
expect {
    "yes/no" {send "yes\r";exp_continue}
     "*password" {send "$password\r"}
}  
expect eof

2.6 相对于的shell脚本

[root@xuebao shell]# cat expect_app.sh 
#!/bin/sh  
#####################
#by xuebao
#2017.05.27
#####################
DATE=`date +%Y%m%d`
src_file="/home/tbt/webappdata/$DATE/test* "
dest_file="/home/tbt/webappdata/backup/$DATE/"
host=109.202.3.100
port=22
username=root 
password=12345678
#scp  back host
cd /home/shell
./expect_app.exp $host $port $username $password $src_file $dest_file 
echo "end"  
最后提示:
如果文件scp 文件过大、传送中断开,因为expect默认timeout为30S
手动添加set timeout -1设置超时时间为无穷大,问题解决
在expect 脚本中添加

images


有需要可以联系微信xuebao19930721和加入微信群
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址