Linux 的expect介绍

expect基本介绍

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

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

expect依赖

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

expect命令介绍

spawn               启动进程(由spawn启动的进程的输出库恶意被expect所捕获),交互程序开始后面跟命令或者指定程序
expect              从晋城接收字符串,获取匹配信息匹配成功则执行expect后面的程序动作
send                用于发送指定的字符串信息,用户模拟用户的输入,注意一定要加回车 \r
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\r";}
    "yes/no" {send "yes\r";exp_continue}
}
expect "root" {send "mkdir testExpect\r"}
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\r";}
    "yes/no" {send "yes\r";exp_continue}
}
expect "root" {send "mkdir test1\r"}
expect "root" {send "mkdir test2\r"}
send "exit\r" //退出远程登录
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\r";}
    "yes/no" {send "yes\r";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\r";exp_continue} //
    "password" {send "123\r"}
 }
 expect eof
 exit

7、expect执行多条命令

#!/usr/bin/expect -f

set timeout 10

spawn sudo su - root
expect "*password*"
send "123456\r"
expect "#*"
send "ls\r"
expect "#*"
send "df -Th\r"
send "exit\r"
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\n";exp_continue } 
        "password" { send "$password\n" }
    } 
    expect "]#" { send "useradd hehe\n" } 
    expect "]#" { send "touch /tmp/test.txt\n" } 
    expect "]#" { send "exit\n" } 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\r" }
    }   

    expect {
        "password:" { send "xxo1#qaz\r" }
    }
    
    expect {
        "*password*:" { send "xx1#qaz\r" }
    }

    expect "*]#"
    send "df -Th\r"
    expect "*]#"
    send "exit\r"
    expect eof

EOF
done

10、最常用的登录跳板机

#!/usr/bin/expect
#set timeout 20 #设置超时时间spawn ssh root@192.168.43.131
expect "*password:"
send "123\r"
# expect "*#"
interact
#!/usr/bin/expect -f
set timeout -1
spawn ssh-add -k wangteng.pem
expect {
  "passphrase" { send "H6goGKlAjcSvTC7u\r"; }
}
spawn ssh  127.0.0.1
interact