expect自动登录ssh,ftp

devtools/2024/10/21 11:36:57/

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.ppmy.cn/devtools/113238.html

相关文章

苹果发布新款iPhone 16,与Apple Intelligence配套:查看功能和价格

在2024年9月9日(星期一)的苹果Glowtime活动中,Apple在其位于加利福尼亚州库比蒂诺总部的Apple Park发布了新款iPhone 16。 苹果公司首席执行官蒂姆库克在2024年9月9日(星期一)于加利福尼亚州库比蒂诺的Apple Park校园…

目标检测经典算法的个人理解

one stage 1、RCNN -> Fast-RCNN:RPN部分从用传统的算法 -> 用深度学习网络来实现。 2、Fast-RCNN -> Faster-RCNN:从先选region再求Feature -> 先求Feature再选region。 two stage 1、SSD(2016):VGG做…

【系统架构设计师-2010年真题】案例分析-答案及详解

更多内容请见: 备考系统架构设计师-核心总结索引 文章目录 【材料1】【问题 1】(7 分)【问题 2】(13 分)【问题 3】(6 分)【材料2】【问题 1】(8 分)【问题 2】(13 分)【问题 3】(4 分)【材料3】【问题 1】(共 7 分)【问题 2】(共 10 分)【问题 3】(共 8 分)【材料4】【问题 1…

MYSQL数据库基础篇——MYSQL的安装与使用

一.下载并安装MYSQL 下载mysql,地址MySQL,进行如下操作​​​​: ​​​ 安装好后,接下来配置信息: 这里选择第一个,当然,有可能你的版本下的MYSQL并没有这个选项,那么我们可以选择Custom&…

Leetcode面试经典150题-350.两个数组的交集II

题目比较简单,散散心吧 这个题竟然是349更简单的版本,可以先看看349题的解法: Leetcode面试经典150题-349.两个数组的交集-CSDN博客 349会了,这个也就会了 解法都在代码里,不懂就留言或者私信 class Solution {/…

基于SpringBoot+Vue+MySQL的网上甜品蛋糕售卖店管理系统

系统展示 用户前台界面 管理员后台界面 系统背景 在数字化浪潮的推动下,我们设计了一款基于SpringBoot后端框架、Vue前端框架及MySQL数据库的网上甜品蛋糕售卖店管理系统。该系统旨在帮助甜品蛋糕店实现线上线下的无缝对接,提升运营效率与顾客体验。通过…

Python 解析 JSON 数据

1、有如下 JSON 数据,存放在 data.json 文件: [{"id":1, "name": "小王", "gender": "male", "score": 96.8}, {"id":2, "name": "小婷", "gender&qu…

音视频入门基础:AAC专题(1)——AAC官方文档下载

一、AAC简介 高级音频编码(英语:Advanced Audio Coding,AAC)是有损音频压缩的专利数字音频编码标准,由Fraunhofer IIS、杜比实验室、贝尔实验室、Sony、Nokia等公司共同开发。出现于1997年,为一种基于MPEG…