expect自动登录ssh,ftp

expect是一种能够按照脚本内容里面设定的方式与交互式程序进行“会话”的程序。根据脚本内容,Expect可以知道程序会提示或反馈什么内容以及 什么是正确的应答。它是一种可以提供“分支和嵌套结构”来引导程序流程的解释型脚本语言。

shell功能很强大,但是不能实现有交互功能的多机器之前的操作,例如ssh和ftp.而expect可以帮助我们来实现.

安装expect

yum install expect  

其实expect根bash形势上差不多的.

实例

1、根据不同IP和密码ssh自动连接到不同的服务器,并停在登录服务器上

1.1新建脚本

vim test.exp 

#!/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                                //交互模式,用户会停留在远程服务器上面.  

1.2 运行结果如下

[root@ubuntu]# chmod 755 test.exp

[root@ubuntu]# ./test.exp 192.168.1.130 admin  

spawn ssh root@192.168.1.130  

Last login: Fri Sep  7 10:47:43 2012 from 192.168.1.142  

[root@linux ~]#  

2、根据固定IP和密码ssh自动连接服务器,并停在登录服务器上

2.1新建脚本

vim web.exp

#!/usr/bin/expect -f  

set ip 192.168.1.130  

set password admin  

set timeout 10  

spawn ssh root@$ip  

expect {  

"*yes/no" { send "yes\r"; exp_continue}  

"*password:" { send "$password\r" }  

}  

interact  

2.2运行结果如下

[root@ubuntu]# chmod 755 test.exp

[root@ubuntu]# ./web.exp  

spawn ssh root@192.168.1.130  

Last login: Fri Sep  7 12:59:02 2012 from 192.168.1.142  

[root@linux ~]#  

3、ssh远程登录到服务器,并且执行命令,执行完后并退出

3.1新建脚本

vim test3.exp

#!/usr/bin/expect -f  

set ip 192.168.1.130  

set password admin  

set timeout 10  

spawn ssh root@$ip  

expect {  

"*yes/no" { send "yes\r"; exp_continue}  

"*password:" { send "$password\r" }  

}  

expect "#*"  

send "pwd\r"  

send  "exit\r"  

expect eof  

3.2运行结果如下

[root@ubuntu]# chmod 755 test3.exp

[root@ubuntu]# ./test3.exp  

spawn ssh root@192.168.1.130  

root@192.168.1.130's password:  

Last login: Fri Sep  7 14:05:07 2012 from 116.246.27.90  

[root@localhost ~]# pwd  

/root  

[root@localhost ~]# exit  

logout  

Connection to 192.168.1.130 closed.  

4、远程登录到ftp,并且下载文件

4.1新建脚本

vim test2.exp

#!/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 "ftp-user\r"  

expect "Password:*"  

send "ftp-passwd\r"  

expect "ftp>*"  

send "cd $dir\r"  

expect {  

"*file"  { send_user "local $dir No such file or directory";send "quit\r" }  

"*successfully*"  { 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  

4.2 运行结果如下

[root@ubuntu]# chmod 755 test2.exp

[root@ubuntu]# ./test2.exp 192.168.1.130 /var/www/www aaa.html  

spawn ftp 192.168.1.130  

Connected to 192.168.1.130.  

220 (vsFTPd 2.0.5)  

Name (192.168.1.130:root): frt-user

331 Please specify the password.  

Password:  

230 Login successful.  

Remote system type is UNIX.  

Using binary mode to transfer files.  

ftp> lcd /var/www/www  

Local directory now /var/www/www  

ftp> get /var/www/www/aaa.html /var/www/www/aaa.html  

local: /var/www/www/aaa.html remote: /var/www/www/aaa.html  

200 PORT command successful. Consider using PASV.  

150 Opening BINARY mode data connection for /var/www/www/aaa.html (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. 

  • 17
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码哝小鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值