linux expect

linux expect

介绍

expect 是由Don Libes基于Tcl(Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助Expect处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率

expect含有利用正则表达式进行模式匹配以及通用的编程功能,允许简单的脚本智能地管理如下工具:telnet,ftp和ssh(这些工具都缺少编程的功能),宏以及其它程序。Expect脚本的出现使得这些老的软件工具有了新的功能和更多的灵活性。

安装expect

首先检查系统是否自带了expect程序, 一般在 系统的/usr/bin目录下, 如果没有自带expect, 那么需要自行安装expect.
例如ubuntu系统安装:

sudo apt-get install expect

常用指令

expect语法

expect [选项][ -c cmds] [ [ -[f|b] ] cmdfile] [ args]

选项
-c:从命令行执行expect脚本,默认expect是交互地执行的
示例:

expect -c 'expect "\n" {send"pressed enter\n"}

-d:可以输出输出调试信息

expect中相关指令

  • spawn:交互程序开始后面跟命令或者指定程序(在shell内启动这个进程)
  • expect:获取匹配信息匹配成功则执行expect后面的程序动作(检测由shell内进程发出的特定交互指令反馈字符串后向下执行)
  • send:用于向进程发送字符串(从shell外向shell内进程发送一条字符串,换行符为确认结束)
  • interact:允许用户交互
  • exp_continue:在expect中多次匹配就需要用到
  • send_user:用来打印输出 相当于shell中的echo
  • exit:退出expect脚本
  • eof:expect执行结束 退出
  • set:定义变量
  • puts:输出变量
  • set timeout:设置超时时间

示例

创建一个ssh_auto_login.exp脚本文件

#!/usr/bin/expect
set timeout 30 
spawn ssh -l username 192.168.1.1 
expect "password:" 
send "ispass\r"
interact 
  • 第一行 #!/usr/bin/expect 与执行方式有关, 当我么以 ./ssh_auto_login.exp,我们必须通过第一行告诉操作系统,脚本需要通过/usr/bin/expect程序来运行,而且我们还要授予文件可执行权限。
    如果我们以下面这种方式执行ssh_auto_login.exp, 也可以不用写第一行, 而且不必授予其可执行权限。

    expect ssh_auto_login.exp
    
  • 第二行 set timeout 30
    设置命令的命令的超时时间, 单位是秒。

  • 第三行 spawn ssh -l username 192.168.1.1
    spawn是expect程序的一个内置命令,使用它将我们需要执行的各种shell命令包裹起来,这样就shell命令注入了和自动化脚本expect, send交互的能力。只有包裹后程序才有机会获取shell命令的输出,并根据输出做出反应。

  • 第四行expect "password:"
    这个命令的意思是判断上次输出结果里是否包含“password:”的字符串
    如果有则立即返回否则就等待一段时间后返回这里等待时长就是前面设置的30秒。
    注意这里的用词是包含,也就是如果完整输出是"please in put your password:"也算中.这里的输出是指控制台的输出不是文件输出

  • 第五行 send "ispass\r"
    当expect接收到“password:”提示符,所以上面的expect实际是在等待“password:”提示符出现,使用等待多好,wait, 等待“password:”提示符出现后继续往后执行,紧接着地就是send命令, send将密码发送到控制台,这里是输入通道。
    第六行 interact
    使将控制权由机器控制转换为人机交互, 由人来接管。

except最常用的语法(tcl语言:模式–动作)

单一分支模式语法:

expect "hi" {send "You said hi\n"}

匹配到hi后,会输出"you said hi",并换行

多分支模式语法:

expect "hi" { send "You said hi\n" } \
"hehe"{ send "Hehe yourself\n" } \
"bye"{ send "Good bye\n" }

匹配hi,hello,bye任意字符串时,执行相应输出。等同如下:

expect {
"hi"{ send "You said hi\n"}
"hehe"{ send "Hehe yourself\n"}
"bye"{ send "Good bye\n"}
}

示例

cat ssh1.exp

#!/usr/bin/expect
spawn ssh 192.168.8.100
expect {
"yes/no" { send"yes\n";exp_continue }
"password" { send"magedu\n" }
}
interact
#expect eof

示例:变量

cat ssh2.exp

#!/usr/bin/expect
set ip 192.168.8.100
set user root
set password magedu
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send"yes\n";exp_continue }
"password" { send"$password\n" }
}
interact

示例:位置参数

vim ssh3.exp

#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect {
"yes/no" { send"yes\n";exp_continue }
"password" { send"$password\n" }
}
interact
#./ssh3.exp 192.168.8.100 rootmagedu

示例:执行多个命令

cat ssh4.exp

#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send"yes\n";exp_continue }
"password" { send"$password\n" }
}
expect "]#" { send"useradd haha\n" }
expect "]#" { send"echo magedu |passwd –stdin haha\n" }
send "exit\n"
expect eof
#./ssh4.exp 192.168.8.100 rootmagedu

示例:shell脚本调用expect

vim ssh5.sh

#!/bin/bash
ip=$1
user=$2
password=$3
expect
set timeout 10
spawn ssh$user@$ip
expect {
"yes/no" { send"yes\n";exp_continue}
"password" { send"$password\n" }
}
expect "]#" { send"useraddhehe\n" }
expect "]#" { send "echomagedu|passwd–stdinhehe\n" }
expect "]#" { send"exit\n" }
expect eof
EOF
#./ssh5.sh 192.168.8.100 rootmagedus

参考

linux中expect命令详解
https://www.lxlinux.net/255.html
Expect—百科篇命令
https://ipcmen.com/expect
Linux expect 详解
https://www.jianshu.com/p/daeafa6efe30

posted @ 2022-10-31 14:54  Trigger-CN  阅读(153)  评论(0编辑  收藏  举报