• Linux之expect


    一,安装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 "; exp_continue}  //第一次ssh连接会提示yes/no,继续  
    8.  "*password:" { send "$password " }      //出现密码提示,发送密码  
    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 "; exp_continue}  
    8.  "*password:" { send "$password " }  
    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 "; exp_continue}  
    8.  "*password:" { send "$password " }  
    9.  }  
    10.  expect "#*"  
    11.  send "pwd "  
    12.  send  "exit "  
    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 "  
    9.  expect "Password:*"  
    10.  send "zwh "  
    11.  expect "ftp>*"  
    12.  send "lcd $dir "  
    13.  expect {  
    14.  "*file"  { send_user "local $_dir No such file or directory";send "quit " }  
    15.  "*now*"  { send "get $dir/$file $dir/$file "}  
    16.  }  
    17.  expect {  
    18.  "*Failed" { send_user "remote $file No such file";send "quit " }  
    19.  "*OK"     { send_user "$file has been download ";send "quit "}  
    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 200
    spawn ssh test1@192.168.1.114
    expect {
    "*yes/no" { send "yes
    "; exp_continue }
    "*password*" { send "test1
    " }
    }
    expect "]$"
    send "pwd
    "
    send "touch  express
    "
    interact
    EOF
    
  • 相关阅读:
    Django关于StreamingHttpResponse与FileResponse响应文件或视频的下载请求
    APScheduler可能遇到的问题
    django中model聚合使用
    Java 递归判断迷宫问题是否有路
    direct path read/write (直接路径读/写)
    DRM 简介
    SQL Server2008表名中含“.”号处理方法
    Java学习之:JDK动态代理与CGLIB动态代理
    强大易用!新一代爬虫利器 Playwright
    为什么cudaMalloc()参数是二级指针
  • 原文地址:https://www.cnblogs.com/zydev/p/4952467.html
Copyright © 2020-2023  润新知