linux expect spawn、linux expect 用法
2016-10-20 10:16
330 查看
使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄、收藏。可是为什么要这么写却不知其然。本文用一个最短的例子说明脚本的原理。 脚本代码如下: ############################################## #!/usr/bin/expect set timeout 30 set username "xiaoliu" set password "123456" set port "22" set host "172.16.30.[lindex $argv 0]" spawn ssh -p $port $username@$host expect "password:" send "$password\r" interact ############################################## 1. [#!/usr/bin/expect] 这一行告诉操作系统脚本里的代码使用那一个shell来执行。这里的expect其实和linux下的bash、windows下的cmd是一类东西。 注意:这一行需要在脚本的第一行。 2. [set timeout 30] 基本上认识英文的都知道这是设置超时时间的,现在你只要记住他的计时单位是:秒 3. [spawn ssh -p $port $username@$host] spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。所以不要用 “which spawn“之类的命令去找spawn命令。好比windows里的dir就是一个内部命令,这个命令由shell自带,你无法找到一个dir.com 或 dir.exe 的可执行文件。 它主要的功能是给ssh运行进程加个壳,用来传递交互指令。 4. [expect "password:"] 这里的expect也是expect的一个内部命令,有点晕吧,expect的shell命令和内部命令是一样的,但不是一个功能,习惯就好了。这个命令的意思是判断上次输出结果里是否包含“password:”的字符串,如果有则继续执行,否则就等待一段时间后返回,这里等待时长就是前面设置的30秒 5. [send "$password\r"] 这里就是执行交互动作,与手工输入密码的动作等效。 温馨提示: 命令字符串结尾别忘记加上“\r”,如果出现异常等待的状态可以核查一下。 6. [interact] 执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。 注意: 1 #!/usr/bin/expect #注意安装的路径,不确定 whereis expect 一下 2 expect 后面跟的字符串,是交互过程中,服务器返回的字符串。如果服务器返回password: ,我们就发送 password. # Change a login shell to bash set user [lindex $argv 0] spawn bash $user expect "]:" send "/bin/bash " expect eof exit
相关文章推荐
- expect spawn、linux expect 用法
- linux expect spawn的用法
- expect spawn、linux expect 用法小记
- expect spawn、linux expect 用法小记
- expect spawn、linux expect 用法小记
- shell expect spawn、linux expect 用法小记
- expect spawn、linux expect 用法
- expect spawn、linux expect 用法小记
- expect spawn、linux expect 用法
- shell expect spawn、linux expect 用法小记
- expect spawn、linux expect 用法小记
- linux expect spawn的用法
- expect spawn、linux expect 用法
- expect spawn、linux expect 用法小记(转)
- expect spawn、linux expect 用法小记
- expect spawn、linux expect 用法小记
- Centos expect spawn、linux expect 用法
- expect spawn、linux expect 用法小记
- shell expect spawn、linux expect 用法小记 看着舒服点
- shell expect spawn、linux expect 用法小记 看着舒服点