Linux之expect
一,安装expect
- yum install expect
其实expect根bash形势上差不多的.
二,实例
1,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@ubuntu:/home/zhangy# ./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 ~]#
这个例子有统一的接口,根据IP和密码可以连接到不同的机器.如果你嫌输入IP和密码麻烦,看下面的例子
- #!/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
运行结果如下:
- root@ubuntu:/home/zhangy# ./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 ~]#
2,ssh远程登录到服务器,并且执行命令,执行完后并退出
- #!/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
运行结果如下:
- root@ubuntu:/home/zhangy# ./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.
3,远程登录到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 "zwh\r"
- expect "Password:*"
- send "zwh\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@ubuntu:/home/zhangy# ./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): zwh
- 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.
4.在bash中也可以加expect
#!/bin/bash
/usr/bin/expect <<-EOF
set time
spawn ssh test1@192.168.1.114
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password*" { send "test1\r" }
}
expect "]$"
send "pwd\r"
send "touch express\r"
interact
EOF
Linux之expect的更多相关文章
- linux tcl expect 安装(转)
linux tcl expect 安装 一.Tcl安装 1. 下载:tcl8.4.20-src.tar.gz http://www.tcl.tk/software/tcltk/downloadnow ...
- Linux 下 expect 脚本语言中交互处理常用命令
Linux 下 expect 脚本语言中交互处理常用命令 1. #!/usr/bin/expect 告诉操作系统脚本里的代码使用那一个 shell 来执行.这里的 expect 其实和 Linux 下 ...
- Linux安装expect命令
[Linux安装expect命令]:--expect是在Tcl基础上创建起来的,所以在安装expect前我们应该先安装Tcl.①:tcl安装源码下载:http://www.tcl.tk/softwar ...
- linux下expect使用教程
一.expect介绍 Expect是Unix系统中用来进行自动化控制和测试的软件工具,由DonLibes制作,作为Tcl脚本语言的一个扩展,应用在交互式软件中如telnet,ftp,Passwd,fs ...
- 用了一天的时间,linux下expect实现ssh自动登录服务器记,鄙视下网上各种抄来抄去残段子
因为要对客户方的快30个项目进行特别有顺序的重启,所以不得不想办法写个脚本,网上看了不少段子.真是残缺的可以.没有一段是可以正常运行的.我来按顺序记录一下 脚本的本身 使用expect实现自动登录的脚 ...
- linux下expect环境安装以及简单脚本测试
expect是交互性很强的脚本语言,可以帮助运维人员实现批量管理成千上百台服务器操作,是一款很实用的批量部署工具!expect依赖于tcl,而linux系统里一般不自带安装tcl,所以需要手动安装 下 ...
- Linux使用expect实现免手动密码输入,linux免密码登陆
使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写却不知其然.本文用一个最短的例子说明脚本的原理. 脚本代码如下: ###### ...
- linux利器expect的使用
1.什么是expect在做系统管理时,我们很多时候需要输入密码,例如:连接 ssh,连接ftp,那么如何能做到不输入密码,我们需要有一个工具,能代替我们实现与终端的交互,它能够代替我们实现与终端的交互 ...
- Linux使用expect实现自动登录的脚本
前提条件服务器已经安装过tcl和expect, 若未安装:可以先执行 yum install tcl expect 进行安装 第一步.编写以下自动登录脚本login.sh ########### ...
随机推荐
- bash deploy.sh 通过bash命令 执行scp -r 命令将本地文件拷贝到服务器
deploy.sh 文件内容如下 #!/bin/bash #scp -r ./* root@XXXXX:/root/sunSH/xadserver/ function getdir(){ for el ...
- java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec
今天迁移老项目到linux服务器,jdk8 ,tomcat8.5遇到这个问题. java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.J ...
- 【Unity笔记】角色的移动方法
方法一:改变物体的transform public class ExampleClass : MonoBehaviour { ; // 跟随摄像机的移动要写在LateUpdate中 void Late ...
- [shell]shell脚本统计数值大小
#! /bin/bash array=( ... ) var1= var2= ;i<=;i++)); do array[i]="$( cat /sys/bus/iio/devices/ ...
- 经典SQL回顾之晋级篇
上篇博文在说SQL基础的时候,有一个地方有点误导大家,文中说到SQL 中的substring()和C#中的substring()相同,这有点歧义.基本原理虽然相同,但是有一点很不一样,就是C#中索引是 ...
- [转]getHibernateTemplate出现的所有find方法的总结
一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有Use ...
- -27979 LoadRunner 错误27979 找不到请求表单 Action.c(73): Error -27979: Requested form not found
LoadRunner请求无法找到:在录制Web协议脚本回放脚本的过程中,会出现请求无法找到的现象,而导致脚本运行停止. 错误现象:Action.c(41): Error -27979: Request ...
- 获取FirefoxProfile配置文件以及使用方法介绍
使用默认方式构建的(WebDriver)FirefoxDriver实例: WebDriver driver = new FirefoxDriver(); 这种方式下,打开的Firefox浏览器将是不带 ...
- e648. 双击和三击事件
component.addMouseListener(new MyMouseListener()); public class MyMouseListener extends MouseAdapter ...
- R语言低级绘图函数-grid
grid 函数用来在一张图表上添加网格线, 基本用法:默认在添加刻度线的地方添加网格线 plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = &qu ...