expect应用
2014-01-19 01:48
274 查看
expect的安装
yum -y install expect
注:expect最简单的实例就是ssh连接回有交互式,为了让其自动,以下脚本能实现自动登录功能如下:
#!/usr/bin/expect
spawn ssh 192.168.1.117 ifconfig eth0
set timmeout 60
expect "*password:" ##等待捕捉出现*password:
send "12344\n" ##发送字符
expect eof
exit
注:exp__send就是send一样
在ssh第一次连接的时候会提示yes/no的交互式的,不用这个关键词会出错,用exp_continue解决,-timeout 意思就是超过X秒就会执行下面对应的那行timeout “puts xxxx“命令,这里的puts相当于echo? ,;return意思就是返回
实例脚本:oldboy-4.exp
![](http://popli3.blog.51cto.com/attachment/201401/18/353526_13900672176Q8g.jpg)
注: send_user参数也和echo一样,以下为
oldboy-6.exp脚本
#!/usr/bin/expect
###################################
#this scripts is created by oldboy
###################################
if { $argc != 3 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set dir [lindex $argv 2]
set password "123456"
spawn scp -P22 -p $file root@$host:$dir
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
expect eof
exit
exit就是直接退出,加个-onexit {}可以做一些退出之后执行的命令 如oldboy-8.exp脚本:
#!/usr/bin/expect
if { $argc != 3 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
}
set file [lindex $argv 0] # 设置变量名file代表参数1
set host [lindex $argv 1] #设置host为参数2
set dir [lindex $argv 2] #设置dir为参数3
set password "123456" #设置set 变量名 变量内容
spawn scp -P22 -r -p $file root@$host:$dir
#spawn scp -P 22 -r -p /root/.ssh root@192.168.1.179:/root/
set timeout 60
expect {
-timeout 20
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
timeout {puts "expect connect timeout,pls contact oldboy."; return}
}
expect eof
exit -onexit {
send_user "Oldboy say good bye to you!\n"
}
yum -y install expect
注:expect最简单的实例就是ssh连接回有交互式,为了让其自动,以下脚本能实现自动登录功能如下:
#!/usr/bin/expect
spawn ssh 192.168.1.117 ifconfig eth0
set timmeout 60
expect "*password:" ##等待捕捉出现*password:
send "12344\n" ##发送字符
expect eof
exit
注:exp__send就是send一样
在ssh第一次连接的时候会提示yes/no的交互式的,不用这个关键词会出错,用exp_continue解决,-timeout 意思就是超过X秒就会执行下面对应的那行timeout “puts xxxx“命令,这里的puts相当于echo? ,;return意思就是返回
实例脚本:oldboy-4.exp
![](http://popli3.blog.51cto.com/attachment/201401/18/353526_13900672176Q8g.jpg)
注: send_user参数也和echo一样,以下为
oldboy-6.exp脚本
#!/usr/bin/expect
###################################
#this scripts is created by oldboy
###################################
if { $argc != 3 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set dir [lindex $argv 2]
set password "123456"
spawn scp -P22 -p $file root@$host:$dir
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
expect eof
exit
exit就是直接退出,加个-onexit {}可以做一些退出之后执行的命令 如oldboy-8.exp脚本:
#!/usr/bin/expect
if { $argc != 3 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
}
set file [lindex $argv 0] # 设置变量名file代表参数1
set host [lindex $argv 1] #设置host为参数2
set dir [lindex $argv 2] #设置dir为参数3
set password "123456" #设置set 变量名 变量内容
spawn scp -P22 -r -p $file root@$host:$dir
#spawn scp -P 22 -r -p /root/.ssh root@192.168.1.179:/root/
set timeout 60
expect {
-timeout 20
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
timeout {puts "expect connect timeout,pls contact oldboy."; return}
}
expect eof
exit -onexit {
send_user "Oldboy say good bye to you!\n"
}
相关文章推荐
- 可以恢复flashfxp密码的FlashFXP Password Recovery v1.0.160.2006 汉化版
- You must SET PASSWORD before executing this statement的解决方法
- MYSQL安装时解决要输入current root password的解决方法
- Linux 入门常用命令 password ― 修改密码,改变用户
- shell结合expect写的批量scp脚本工具
- Oracle listener password encryption
- 如何破解root以及grub密码
- openssl安装配置
- indows下mysql忘记root密码的解决方法
- php篇 重拾自学之路456(自娱自乐)
- SQL SERVER2008 忘記SA密碼怎麼辦
- switch基本配置
- 局域网交换(交换机三大原理.基本配置)
- router password crack
- java连接池的问题
- Linux中的/etc/gshadow中密碼的作用
- 修改mysql密码
- Linux 远程执行命令,expect
- Oracle默认的用户名和密码是什么?
- 博科BROCADE交换机配置(详细)