zoukankan      html  css  js  c++  java
  • expect的安装与使用

    expect的安装与使用

    是什么

    expect 是用来进行自动化控制和测试的工具。主要是和交互式软件telnet ftp ssh 等进行自动化的交互。

    如何安装

    1.检测是否安装

    ls /usr/bin |grep expect
    

    如果不存在,则进行安装

    2.安装

    sudo apt-get install expect
    
    $ ls /usr/bin |grep expect
    autoexpect
    expect
    expect_autoexpect
    expect_autopasswd
    expect_cryptdir
    expect_decryptdir
    expect_dislocate
    expect_ftp-rfc
    expect_kibitz
    expect_lpunlock
    expect_mkpasswd
    expect_multixterm
    expect_passmass
    expect_rftp
    expect_rlogin-cwd
    expect_timed-read
    expect_timed-run
    expect_tknewsbiff
    expect_tkpasswd
    expect_unbuffer
    expect_weather
    expect_xkibitz
    expect_xpstat
    

    具体使用

    案例一,进入ssh脚本

    spawn是进入expect环境后才可以执行的expect内部命令。expect是一种脚本语言,它能够代替我们实现与终端的交互,我们不必再守候在电脑旁边输入密码,或是根据系统的输出再运行相应的命令。

    1.创建脚本

    #! /usr/bin/expect
    
    # 设置超时时间
    set timeout 3
    # fork一个子进程执行ssh
    spawn ssh root@xxx.xxx.xxx.xxx
    
    # 捕获到密码
    expect "*password*"
    # 输入密码并回车
    send "xxxxxx
    "
    
    # 捕获#
    expect "*#"
    # 进入常用目录下
    send "cd /home/wwwroot/default
    "
    
    # 允许用户进行交互
    interact          
    

    2.创建权限

    sudo chmod +x xxx.sh
    

    3.执行脚本

    ./xxx.sh
    
    jiqing@Ubuntu:~/sh$ ./xxx.sh 
    spawn ssh root@xxx
    root@xxx's password: 
    Last login: Thu Jun 21 15:07:03 2018 from 218.93.209.10
    
    Welcome to Alibaba Cloud Elastic Compute Service !
    
    [root@iZuf6ingetk3pr7xgv1vq1Z ~]# cd /home/wwwroot/default
    [root@iZuf6ingetk3pr7xgv1vq1Z default]# 
    

    优化通用版本,支持第一次yes判断,支持ip输入

    #! /usr/bin/expect
    set ip [lindex $argv 0]
    set password [lindex $argv 1]
    if {$ip == ""} {
        puts "请输入ip"
        exit 
    }
    
    if {$password == ""} {
        set password "123456"
    }
    
    # 设置超时时间
    set timeout 3
    # fork一个子进程执行ssh
    spawn ssh root@$ip
    
    expect {  
        "*yes/no*" { send "yes
    "; exp_continue}
        "*password*" { send "$password
    " }
    }  
    
    # 捕获到密码
    # expect "*password*"
    # 输入密码并回车
    # send "$password
    "
    
    # 捕获#
    expect "*#"
    # 进入常用目录下
    send "cd /home/wwwroot/default
    " 
    
    # 允许用户进行交互
    interact
    
    

    继续升级成昵称,比ip更好用

    #! /usr/bin/expect
    set pro_name [lindex $argv 0]
    set password [lindex $argv 1]
    if {$pro_name == ""} {
        puts "请输入名称"
        exit 
    }
    
    switch $pro_name {
        "meiren" -
        "yanglu" -
        "wenbo" {
            set ip "ip1"
        }
        "siemens" {
            set ip "ip2"
        }
        "tqmp" {
            set ip "ip3"
        }
        default {
            puts "请输入正确的名称"
            exit
        }
    }
    
    if {$password == ""} {
        set password "xxx"
    }
    
    # 设置超时时间
    set timeout 3
    # fork一个子进程执行ssh
    spawn ssh root@$ip
    
    expect {  
        "*yes/no*" { send "yes
    "; exp_continue}
        "*password:*" { send "$password
    " }
    }  
    
    # 捕获到密码
    # expect "*password*"
    # 输入密码并回车
    # send "$password
    "
    
    # 捕获#
    expect "*#"
    # 进入常用目录下
    send "cd /home/wwwroot/default
    " 
    
    # 允许用户进行交互
    interact
    
    

    666 ,一个脚本,一键链接ssh。

    案例二,进行cd操作

    #! /usr/bin/expect
    # 跳转到项目目录下
    set pro_name [lindex $argv 0]
    spawn bash
    if {$pro_name != ""} {
        set target_dir "/home/wwwroot/default/$pro_name"
    } else {
        set target_dir "/home/wwwroot/default"
    }
    # 判断目录是否存在
    if {![file isdirectory $target_dir]} {
        puts "项目目录不存在"
        set target_dir "/home/wwwroot/default"
    }
    send "cd $target_dir
    "
    interact
    
    

    ps:expect的语法与shell脚本有点不同,多用用就习惯了。运用起来,让他们帮助你更好的工作。

    更多功能,需要在工作中去探索和使用。我爱linux。

  • 相关阅读:
    HTML5新特性之离线缓存技术
    摘要
    典藏百度前端面试题
    idea连接mysql数据库
    报错:[stack Error: Can't find Python executable "python"] vue项目npm install
    SVN客户端(小乌龟)checkout(检出)文件(项目)到本地
    java日期Date工具类 日期格式转换
    radio标签 onchange事件
    js 写带有返回值的function遇到的返回值不正常的情况
    读取zip包内根目录文件的文件名
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/9209178.html
Copyright © 2011-2022 走看看