码迷,mamicode.com
首页 > 系统相关 > 详细

Linux expect 用法

时间:2015-09-08 20:15:25      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

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 timeout 30
set cmd_prompt "]#|~]?"


#-------------------------------------------- login by ssh 
spawn ssh -p $port $loginuser@$ipaddr 
set timeout 30
expect {
-re "Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
}
-re "assword:" {
send "$loginpass\r"
}
-re "Permission denied, please try again." {
send_user "Error: Permission denied.\n"
exit

-re "Connection refused" {
exit

timeout {
exit

eof {
exit
}
}


expect {
#password not correct
-re "Permission denied, please try again." {
send_user "Error: Password is wrong.\n"
exit

}


expect {
-re "assword:" {
send "$loginpass\r"
}
-re $cmd_prompt {
send "\r"
}
}


expect {
#password not correct
-re "Permission denied, please try again." {
send_user "Error: Password is wrong.\n"
exit

}


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


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


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




#-------------------------------------------- exit
expect {
-re $cmd_prompt {
send "exit\r"
}
}


exit
#interact





版权声明:本文为博主原创文章,未经博主允许不得转载。

Linux expect 用法

标签:

原文地址:http://blog.csdn.net/catoop/article/details/48289991

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!