zoukankan      html  css  js  c++  java
  • Linux 的expect介绍

    expect基本介绍

    expect是一个自动化交互套件,主要用于执行命令和程序时,系统以交互形式要求输入指定字符串,实现交互通信。如果在运维工作方面有大量的一体化操作,那么了解使用expect会节省很大时间。

    比如登录跳板机,登录mysql这些流程操作需要经常输入密码回车,那么它就可以作为你的工具。

    expect依赖

    expect是由tcl语言演变而来的,所以expect脚本的运行需要tcl的支持,假如 环境里没有tcl,必须先安装。

    expect命令介绍

    spawn               启动进程(由spawn启动的进程的输出库恶意被expect所捕获),交互程序开始后面跟命令或者指定程序
    expect              从晋城接收字符串,获取匹配信息匹配成功则执行expect后面的程序动作
    send                用于发送指定的字符串信息,用户模拟用户的输入,注意一定要加回车 
    exp_continue        在expect中多次匹配就需要用到
    send_user           用来打印输出 相当于shell中的echo
    exit                退出expect脚本
    eof                 expect执行结束 退出
    set                 定义变量
    puts                输出变量
    set timeout         设置超时时间
    interact 用户交互
     
    命令使用简单举例

    1、单一分支语法:

    expect "hello" {send "you said hello"} //当输出中包含hello后,输出you said hello

    2、多分支模式语法:

    expect { 
          "lilei" {send "hello lilei"; exp_continue} //  当输出中包含lilei时 输出hello lilei,同时循环此多分支语句  
          "hanmeimei" {send "hello hanmeimei"; exp_continue} //当输出中包含hanmeimei时 输出hello hanmeimei,同时循环此多分支语句
          "how do you do ?" {send "how do you do ?”} //当输出中包含how do you do ?时 输出dow do you do
    }

    3、远程登录服务器并创建文件夹

    #!/usr/bin/expect 
    set timeout -1
    spawn ssh root@192.168.0.107
    expect {
        "password" {send "123456
    ";}
        "yes/no" {send "yes
    ";exp_continue}
    }
    expect "root" {send "mkdir testExpect
    "}
    expect eof
    exit

    4、expect脚本获取参数

    expect.ex

    #!/usr/bin/expect 
    set ip [lindex $argv 0]
    set password [lindex $argv 1]
    set timeout -1
    spawn ssh root@$ip
    expect {
        "password" {send "$password
    ";}
        "yes/no" {send "yes
    ";exp_continue}
    }
    expect "root" {send "mkdir test1
    "}
    expect "root" {send "mkdir test2
    "}
    send "exit
    " //退出远程登录
    expect eof
    exit

    5、使用scp传输文件

    #!/usr/bin/expect 
    set timeout -1
    spawn scp test.txt root@192.168.0.107:/home/
    expect {
        "password" {send "123456
    ";}
        "yes/no" {send "yes
    ";exp_continue}
    }
    expect eof
    exit

    6、在本地开启socks5的代理

     #!/usr/bin/expect 
     set timeout -1 //expect匹配输出的超时时间
     spawn ssh -N -D 0.0.0.0:1080 localhost //新建一个进程,执行ssh命令
     expect {
        "yes/no" {send "yes
    ";exp_continue} //
        "password" {send "123
    "}
     }
     expect eof
     exit

    7、expect执行多条命令

    #!/usr/bin/expect -f
    
    set timeout 10
    
    spawn sudo su - root
    expect "*password*"
    send "123456
    "
    expect "#*"
    send "ls
    "
    expect "#*"
    send "df -Th
    "
    send "exit
    "
    expect eof

    8、shell调用expect执行多行命令

    #!/bin/bash 
    ip=$1  
    user=$2 
    password=$3 
    
    expect <<EOF  
        set timeout 10 
        spawn ssh $user@$ip 
        expect { 
            "yes/no" { send "yes
    ";exp_continue } 
            "password" { send "$password
    " }
        } 
        expect "]#" { send "useradd hehe
    " } 
        expect "]#" { send "touch /tmp/test.txt
    " } 
        expect "]#" { send "exit
    " } expect eof 
     EOF  
     #./ssh5.sh 192.168.1.10 root 123456

    9、使用普通用户登录远程主机,并通过sudo到root权限,通过for循环批量在远程主机执行命令

    $ cat timeout_login.txt 
    10.0.1.8
    10.0.1.34
    10.0.1.88
    10.0.1.76
    10.0.1.2
    10.0.1.3
    #!/bin/bash
    
    for i in `cat /home/admin/timeout_login.txt`
    do
    
        /usr/bin/expect << EOF
        spawn /usr/bin/ssh -t -p 22022 admin@$i "sudo su -"
    
        expect {
            "yes/no" { send "yes
    " }
        }   
    
        expect {
            "password:" { send "xxo1#qaz
    " }
        }
        
        expect {
            "*password*:" { send "xx1#qaz
    " }
        }
    
        expect "*]#"
        send "df -Th
    "
        expect "*]#"
        send "exit
    "
        expect eof
    
    EOF
    done

    10、最常用的登录跳板机 

    #!/usr/bin/expect
    #set timeout 20 #设置超时时间spawn ssh root@192.168.43.131
    expect "*password:"
    send "123
    "
    # expect "*#"
    interact
    #!/usr/bin/expect -f
    set timeout -1
    spawn ssh-add -k wangteng.pem
    expect {
      "passphrase" { send "H6goGKlAjcSvTC7u
    "; }
    }
    spawn ssh  127.0.0.1
    interact
  • 相关阅读:
    ubuntu 使用ifupdown 进行高级网络设置
    2015 9月27日 工作计划与执行
    2015 9月25日 工作计划与执行
    2015 9月24日 工作计划与执行
    2015 9月23日 工作计划与执行
    ubuntu 安装python3
    leetCode(12):Remove Duplicates from Sorted List 分类: leetCode 2015-06-18 15:48 136人阅读 评论(0) 收藏
    leetCode(11):Reverse linked list II 分类: leetCode 2015-06-18 15:16 154人阅读 评论(0) 收藏
    leetCode(10):Partition List 分类: leetCode 2015-06-18 09:08 105人阅读 评论(0) 收藏
    leetCode(9):Remove Nth Node From End of List 分类: leetCode 2015-06-18 08:15 109人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/wt645631686/p/8401289.html
Copyright © 2011-2022 走看看