当前位置: 首页 > news >正文

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. 


http://www.mrgr.cn/news/25799.html

相关文章:

  • 【深度学习】【onnxruntime】C++调用onnx
  • 力扣刷题--73. 矩阵置零【中等】
  • Java语法糖
  • 【网络安全】-ssrf服务器请求伪造攻击-burp
  • 智汇云舟斩获创客北京2024鲲鹏应用创新大赛北京区总决赛一等奖
  • 帮领导、客户代买火车票,公司报销怎么开发票?
  • Zookeeper工作机制、特点、数据结构、应用场景、配置参数解读
  • 便宜好用的无人机集群组网技术详解
  • 稀有 Punk 10E 到手?「捡漏」的背后是一个已停止运营的 NFT 碎片化协议
  • Unity3D 实现水体交互详解
  • Java 17 LTS-增强的 switch 表达式
  • 18 C语言实现深度优先搜索
  • 介绍 Apache Spark 的基本概念和在大数据分析中的应用。
  • OpenAI发布最强推理模型o1,它真的会思考,但API比4o贵好几倍
  • VMware替换需重点关注这件事,可能直接影响迁移的成败
  • AI图像篡改检测:如何识破Deepfake的虚假伪装?
  • 自己掏耳朵怎么弄干净?可视挖耳勺排行榜!
  • linux入门到实操-4 linux系统网络配置、连接测试、网络连接模式、修改静态IP、配置主机名
  • 惠海升压恒流芯片ICH6901B支持2.7V3.7V升12V24V36V48V60V100V调光无频闪 300W大功率应急灯
  • 家庭期待与学业重压:青少年心理健康的双重挑战