一,安装expect

  1. yum install expect

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

二,实例

1,ssh实现自动登录,并停在登录服务器上

  1. #!/usr/bin/expect -f
  2. set ip [lindex $argv 0 ]     //接收第一个参数,并设置IP
  3. set password [lindex $argv 1 ]   //接收第二个参数,并设置密码
  4. set timeout 10                   //设置超时时间
  5. spawn ssh root@$ip       //发送ssh请求
  6. expect {                 //返回信息匹配
  7. "*yes/no" { send "yes\r"; exp_continue}  //第一次ssh连接会提示yes/no,继续
  8. "*password:" { send "$password\r" }      //出现密码提示,发送密码
  9. }
  10. interact          //交互模式,用户会停留在远程服务器上面.

运行结果如下:

  1. root@ubuntu:/home/zhangy# ./test.exp 192.168.1.130 admin
  2. spawn ssh root@192.168.1.130
  3. Last login: Fri Sep  7 10:47:43 2012 from 192.168.1.142
  4. [root@linux ~]#

这个例子有统一的接口,根据IP和密码可以连接到不同的机器.如果你嫌输入IP和密码麻烦,看下面的例子

  1. #!/usr/bin/expect -f
  2. set ip 192.168.1.130
  3. set password admin
  4. set timeout 10
  5. spawn ssh root@$ip
  6. expect {
  7. "*yes/no" { send "yes\r"; exp_continue}
  8. "*password:" { send "$password\r" }
  9. }
  10. interact

运行结果如下:

  1. root@ubuntu:/home/zhangy# ./web.exp
  2. spawn ssh root@192.168.1.130
  3. Last login: Fri Sep  7 12:59:02 2012 from 192.168.1.142
  4. [root@linux ~]#

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

  1. #!/usr/bin/expect -f
  2. set ip 192.168.1.130
  3. set password admin
  4. set timeout 10
  5. spawn ssh root@$ip
  6. expect {
  7. "*yes/no" { send "yes\r"; exp_continue}
  8. "*password:" { send "$password\r" }
  9. }
  10. expect "#*"
  11. send "pwd\r"
  12. send  "exit\r"
  13. expect eof

运行结果如下:

  1. root@ubuntu:/home/zhangy# ./test3.exp
  2. spawn ssh root@192.168.1.130
  3. root@192.168.1.130's password:
  4. Last login: Fri Sep  7 14:05:07 2012 from 116.246.27.90
  5. [root@localhost ~]# pwd
  6. /root
  7. [root@localhost ~]# exit
  8. logout
  9. Connection to 192.168.1.130 closed.

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

  1. #!/usr/bin/expect -f
  2. set ip [lindex $argv 0 ]
  3. set dir [lindex $argv 1 ]
  4. set file [lindex $argv 2 ]
  5. set timeout 10
  6. spawn ftp $ip
  7. expect "Name*"
  8. send "zwh\r"
  9. expect "Password:*"
  10. send "zwh\r"
  11. expect "ftp>*"
  12. send "lcd $dir\r"
  13. expect {
  14. "*file"  { send_user "local $_dir No such file or directory";send "quit\r" }
  15. "*now*"  { send "get $dir/$file $dir/$file\r"}
  16. }
  17. expect {
  18. "*Failed" { send_user "remote $file No such file";send "quit\r" }
  19. "*OK"     { send_user "$file has been download\r";send "quit\r"}
  20. }
  21. expect eof

运行结果如下:

    1. root@ubuntu:/home/zhangy# ./test2.exp 192.168.1.130 /var/www/www aaa.html
    2. spawn ftp 192.168.1.130
    3. Connected to 192.168.1.130.
    4. 220 (vsFTPd 2.0.5)
    5. Name (192.168.1.130:root): zwh
    6. 331 Please specify the password.
    7. Password:
    8. 230 Login successful.
    9. Remote system type is UNIX.
    10. Using binary mode to transfer files.
    11. ftp> lcd /var/www/www
    12. Local directory now /var/www/www
    13. ftp> get /var/www/www/aaa.html /var/www/www/aaa.html
    14. local: /var/www/www/aaa.html remote: /var/www/www/aaa.html
    15. 200 PORT command successful. Consider using PASV.
    16. 150 Opening BINARY mode data connection for /var/www/www/aaa.html (66 bytes).
    17. 226 File send OK.
    18. 66 bytes received in 0.00 secs (515.6 kB/s)
    19. quit aaa.html has been download
    20. 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的更多相关文章

  1. linux tcl expect 安装(转)

    linux tcl expect 安装 一.Tcl安装 1.  下载:tcl8.4.20-src.tar.gz http://www.tcl.tk/software/tcltk/downloadnow ...

  2. Linux 下 expect 脚本语言中交互处理常用命令

    Linux 下 expect 脚本语言中交互处理常用命令 1. #!/usr/bin/expect 告诉操作系统脚本里的代码使用那一个 shell 来执行.这里的 expect 其实和 Linux 下 ...

  3. Linux安装expect命令

    [Linux安装expect命令]:--expect是在Tcl基础上创建起来的,所以在安装expect前我们应该先安装Tcl.①:tcl安装源码下载:http://www.tcl.tk/softwar ...

  4. linux下expect使用教程

    一.expect介绍 Expect是Unix系统中用来进行自动化控制和测试的软件工具,由DonLibes制作,作为Tcl脚本语言的一个扩展,应用在交互式软件中如telnet,ftp,Passwd,fs ...

  5. 用了一天的时间,linux下expect实现ssh自动登录服务器记,鄙视下网上各种抄来抄去残段子

    因为要对客户方的快30个项目进行特别有顺序的重启,所以不得不想办法写个脚本,网上看了不少段子.真是残缺的可以.没有一段是可以正常运行的.我来按顺序记录一下 脚本的本身 使用expect实现自动登录的脚 ...

  6. linux下expect环境安装以及简单脚本测试

    expect是交互性很强的脚本语言,可以帮助运维人员实现批量管理成千上百台服务器操作,是一款很实用的批量部署工具!expect依赖于tcl,而linux系统里一般不自带安装tcl,所以需要手动安装 下 ...

  7. Linux使用expect实现免手动密码输入,linux免密码登陆

    使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写却不知其然.本文用一个最短的例子说明脚本的原理.  脚本代码如下:  ###### ...

  8. linux利器expect的使用

    1.什么是expect在做系统管理时,我们很多时候需要输入密码,例如:连接 ssh,连接ftp,那么如何能做到不输入密码,我们需要有一个工具,能代替我们实现与终端的交互,它能够代替我们实现与终端的交互 ...

  9. Linux使用expect实现自动登录的脚本

    前提条件服务器已经安装过tcl和expect, 若未安装:可以先执行 yum  install tcl  expect  进行安装 第一步.编写以下自动登录脚本login.sh ########### ...

随机推荐

  1. 拦截iOS系统导航栏返回按钮事件-三种方法

    方法一:在dealloc里面书写监听事件,因为只有pop才会调用dealloc,push不会掉用 - (void)dealloc {YLLog(@"123"); } 方法二:在- ...

  2. 完成wamp安装后Mysql配置记录

    注意:下面如没有特殊说明,默认都是修改my.ini配置,修改完配置需要重启mysql服务. 1.安装完wamp后输入命令有乱码提示: 运行mysql命令时,出现的错误提示是乱码 :    ERROR ...

  3. u-boot bootz 加载kernel 流程分析

    u-boot 加载 kernel 的流程分析. image重要结构体头文件 // include/image.h * * Legacy and FIT format headers used by d ...

  4. kubernetes外部访问的几种方式

    1:用的最多的是nodePort,如下nginx的service,将type设置成NodePort,同时nodePort设置成30010(k8s为了不与宿主机的端口冲突,默认限制了30000以下的端口 ...

  5. eclipse 里的tomcat一直处于starting状态

    今天困扰了我一天,eclipse里面明明吧tomcat启动成功了,但是有个starting tomcat的进程一直在启动着,当你修改页面,build workspace的时候,进程就一直处于等待状态, ...

  6. Elasticsearch5.X IN Windows 10 系列文章(5)

    ElasticSearch版本: 5.5.1 (最新稳定版为5.5.2),由于用到IK中文分词插件,最新版本没有5.5.2 ,所以使用5.5.1 日期:2017-08-31 第五章:Kibana 安装 ...

  7. XMLHttpRequest对象的常用属性与方法

    方法 一, open(); 书上解释: 用于设置请求的目标url请求方法, 以及其他参数信息 个人理解: 发送请求的页面在不刷新的情况能将参数传给一个服务器进行处理, 这个方法就是将这些个参数传送过去 ...

  8. 关于矢量图片资源向后兼容:CompatVectorFromResourcesEnabled标志的使用

    StackOverflow上摘抄的: some things have changed since I asked this question so I will answer it myself.W ...

  9. Delphi中使一个窗口居中

    把窗口的position属性设置为poScreenCenter,就可以使窗口居于屏幕的中央.

  10. Navicat连接Oracle11g 错误的解决办法

    一.换成32位的Navicat!!! 二.去Oracle官网下载你要连接数据版本的client. 注意:需要下载两个文件,以11.2.0.4.0版本为例,需要下载 instantclient-sqlp ...