您的位置:首页 > 运维架构 > Linux

Linux expect 用法

2015-09-08 13:48 323 查看
expect是建立在tcl基础上的一个工具,它用来让一些需要交互的任务自动化地完成。

因为expect是基于tcl的,所以需要你的系统中安装有tcl
如何检查?
[root@dev ~]# whereis tcl
tcl: /usr/lib/tcl8.4 /usr/share/tcl8.4
如果看不到结果,请先安装tcl

安装

> 安装tcl
解压tcl安装包后
cd tcl8.4.11/unix/
./configure --prefix=/usr/local/tcl/ --enable-shared
make && make install

> 安装expect
解压expect安装包后
cd expect-5.43
./configure --prefix=/usr/local/expect/ --with-tcl=/usr/local/tcl/lib/ --with-tclinclude=/opt/tcl8.4.11/generic/ --enable-shared
make && make install
注意:指定的/opt/tcl8.4.11/generic/ 为我们上面解压的tcl目录

> 创建连接符号
ln -s /usr/local/expect/bin/expect /usr/bin/expect
> 查看连接符号
ls -l /usr/bin/expect
lrwxrwxrwx. 1 root root 28 9月 8 11:21 /usr/bin/expect -> /usr/local/expect/bin/expect
这个符号链接将在编写expect脚本文件时用到,例如在expect文件头部会指定用于执行该脚本的shell
#!/usr/bin/expect

> 测试
[root@localhost opt]# expect
expect1.1> exit
[root@localhost opt]#
这样就可以开始运行expect脚本了。

用法

1. [#!/usr/bin/expect]
这一行告诉操作系统脚本里的代码使用那一个shell来执行。这里的expect其实和linux下的bash、windows下的cmd是一类东西。
注意:这一行需要在脚本的第一行。

2. [set timeout 30]
基本上认识英文的都知道这是设置超时时间的,现在你只要记住他的计时单位是:秒。timeout -1 为永不超时,默认情况下,timeout是10秒;

3. [spawn ssh -l username 192.168.1.1]
spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。所以不要用 “which spawn“之类的命令去找spawn命令。好比windows里的dir就是一个内部命令,这个命令由shell自带,你无法找到一个dir.com 或 dir.exe 的可执行文件。
它主要的功能是给ssh运行进程加个壳,用来传递交互指令。
spawn后面加上需要执行的shell命令,比如说spawn sudo touch testfile

4. [expect "password:"]
这里的expect也是expect的一个内部命令,有点晕吧,expect的shell命令和内部命令是一样的,但不是一个功能,习惯就好了。这个命令的意思是判断上次输出结果里是否包含“password:”的字符串,如果有则立即返回,否则就等待一段时间后返回,这里等待时长就是前面设置的30秒

5. [send "ispass\r"]
这里就是执行交互动作,与手工输入密码的动作等效。
温馨提示: 命令字符串结尾别忘记加上“\r”,如果出现异常等待的状态可以核查一下。

6. [interact]
执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。如果你只是登录过去执行

7. $argv 参数数组
expect脚本可以接受从bash传递过来的参数.可以使用[lindex $argv n]获得,n从0开始,分别表示第一个,第二个,第三个....参数
其中,$argc为命令行参数的个数,$argv0为脚本名字本身,$argv为命令行参数。[lrange $argv 0 0]表示第1个参数,[lrange $argv 0 4]为第一个到第五个参数。与c语言不一样的地方在于,$argv不包含脚本名字本身。

8. send和send_user
send会将expect脚本中需要的信息发送给spawn启动的那个进程,而send_user只是回显用户发出的信息,类似于shell中的echo而已。

9. 如果你在第一行(#!那行)使用-d (debug参数),可以在运行的时候输出一些很有用的信息
比如你会看见
argv[0] = /usr/bin/expect argv[1] = -d argv[2] = ./launch.exp argv[3] = 1 argv[4] = 2 argv[5] = 3
使用这些也可以完成参数传递

10. exp_continue的用法
expect {
-re "Permission denied, please try again." {
send_user "Error:Permission denied.\n"
exit
}
-re "Are you sure you want to continue connecting (yes/no)?" {
send "yes\r";exp_continue
}
-re "assword:" {
send "$loginpass\r";exp_continue
}
-re "Connection refused" {
exit
}
timeout {
exit
}
eof {
exit
}
}
使用exp_continue后,会重新从当前expect块的开始重新执行,可以简单理解问while循环的continue

例子

下面的脚本例子,实现了:登录一个Linux服务器,执行 df -h 等命令
vi expectExample.sh 后输入下面的脚本内容保存后,使用chmod +x expectExample.sh 命令为脚本赋予可执行权限。

#!/usr/bin/expect -f

#-------------------------------------------- set the variable,you can modify the value
set ipaddr [lrange $argv 0 0]
set port [lrange $argv 1 1]
set loginuser [lrange $argv 2 2]
set loginpass [lrange $argv 3 3]
set cmd_prompt "]#|~]?"

# 超时时间,单位(秒)
set timeout 3600

#-------------------------------------------- login by ssh
spawn ssh -p $port $loginuser@$ipaddr

expect {
-re "Permission denied, please try again." {
send_user "Error:Permission denied.\n"
exit
}
-re "Connection refused" {
send_user "Error:Connection refused.\n"
exit
}
timeout {
exit
}
eof {
exit
}
-re "Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
exp_continue
}
-re "assword:" {
send "$loginpass\r"
exp_continue
}
-re $cmd_prompt {
send "\r"

}
}

#-------------------------------------------- now,we do some commands
expect {
-re $cmd_prompt {
send "df -h\r"
}
}

expect {
-re $cmd_prompt {
send "free -m\r"
}
}

expect {
-re $cmd_prompt {
send "uptime\r"
}
}

send "cd /opt\r"
send "mkdir shanhy123\r"
send "cd shanhy123\r"
send "echo 123 > 123.txt\r"

send "/opt/xs.sh\r"

send "exit\r"

expect eof
#interact

-------------------------------------------------
其中 xs.sh 为我写的一个测试的 shell 脚本,里面故意做了一个sleep 模拟耗时操作,代码如下:
#!/bin/bash
sleep 10
echo abc > /opt/abc.txt

注意:expect脚本必须以interact或expect eof结束,执行自动化任务通常expect eof就够了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: