expect用法
1.[#!/usr/bin/expect]
这一行告诉操作系统脚本里的代码使用那一个shell来执行。这里的expect其实和linux下的bash、windows下的cmd是一类东西。
注意:这一行需要在脚本的第一行。
2.[set timeout 30]
基本上认识英文的都知道这是设置超时时间的,现在你只要记住他的计时单位是:秒 。timeout -1 为永不超时
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运行进程加个壳,用来传递交互指令。
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开始,分别表示第一个,第二个,第三个....参数
8.send_user用来发送内容给用户
ssh用例
#!/usr/bin/expect -f
set ip [lindex $argv 0 ] //接收第一个参数,并设置IP
set password [lindex $argv 1 ] //接收第二个参数,并设置密码
set timeout 10 //设置超时时间
spawn ssh root@$ip //发送ssh请滶
expect { //返回信息匹配
"*yes/no" { send "yes\r"; exp_continue} //第一次ssh连接会提示yes/no,继续
"*password:" { send "$password\r" } //出现密码提示,发送密码
}
interact //交互模式,用户会停留在远程服务器上面.
运行结果
[root@localhost script]# ./a.exp 192.168.100.100 admin
spawn ssh root@192.168.100.100
root@192.168.100.100's password:
Last login: Wed Aug 14 11:08:09 2013 from 192.168.100.177
[root@localhost ~]#
固定ip地址
#!/usr/bin/expect -f
set ip 192.168.100.100
set password admin
set timeout 10
spawn ssh root@$ip
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "$password\r" }
}
interact
运行结果
[root@localhost script]# ./b.exp
spawn ssh root@192.168.100.100
root@192.168.100.100's password:
Last login: Wed Aug 14 11:14:32 2013 from 192.168.100.177
[root@localhost ~]#
ftp下载文件用例
#!/usr/bin/expect -f
set ip [lindex $argv 0 ]
set dir [lindex $argv 1 ]
set file [lindex $argv 2 ]
set timeout 10
spawn ftp $ip
expect "Name*"
send "test\r"
expect "Password:*"
send "test\r"
expect "ftp>*"
send "lcd $dir\r"
expect {
"*file" { send_user "local $_dir No such file or directory";send "quit\r" }
"*now*" { send "get $dir/$file $dir/$file\r"}
}
expect {
"*Failed" { send_user "remote $file No such file";send "quit\r" }
"*OK" { send_user "$file has been download\r";send "quit\r"}
}
expect eof
运行结果
[root@localhost script]# ./test.exp 192.168.100.100 /var/www/html a.index
spawn ftp 192.168.100.100
Connected to 192.168.100.100.
220 (vsFTPd 2.0.5)
Name (192.168.100.100:root): test
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> lcd /var/www/html
Local directory now /var/www/html
ftp> get /var/www/html/a.index /var/www/html/a.index
local: /var/www/html/a.index remote: /var/www/html/a.index
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for /var/www/html/a.index (66 bytes).
226 File send OK.
66 bytes received in 0.00 secs (515.6 kB/s)
quit aaa.html has been download
221 Goodbye.
转载于:https://blog.51cto.com/520and519/1272744