expect

本文最后更新于:2023年12月5日 晚上

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

expect 可以认为是一种脚本语言,下面介绍的是 expect 环境下的变量和命令:

变量

定义变量

set name value

调用变量

$name

命令

close [-slave] [-onexec 0|1] [-i spawn_id]

debug [[-now] 0|1]

disconnect

exit [-opts] [status]

exp_continue [-continue_timer] ★

不返回结果,继续执行。

exp_internal [-f file] value

exp_open [args] [-i spawn_id]

exp_pid [-i spawn_id]

exp_send

exp_send_error

exp_send_log

exp_send_tty

exp_send_user

exp_version [[-exit] version]

expect [[-opts] pat1 body1] … [-opts] patn [bodyn] ★

模式匹配,匹配成功后执行指定操作,如果没有指定操作,则忽略。
eof 和 timeout 是关键字
如果匹配的是关键字”eof”,则在文件结束时候执行指定操作,如果匹配的是关键字”timeout”,则在超时时执行指定的操作,默认的超时时间是 10 秒,可以设置为 30 秒set timeout 30,或者设置不超时set timeout -

set timeout 30
# 匹配一次
expect 'hi' {send "You said hi\n"}
# 匹配多次:每次匹配成功后执行指定操作,返回的结果用来进行下一次模式匹配。
expect {
    busy               {puts busy\n ; exp_continue}
    failed             abort
    "invalid password" abort
    timeout            abort
    connected
}

expect_after [expect_args]

expect_background [expect_args]

expect_before [expect_args]

expect_tty [expect_args]

expect_user [expect_args]

fork

interact [string1 body1] … [stringn [bodyn]] ★

interact 是交互的意思。将当前进程的控制权交给用户,就是让用户手动处理的意思。

interpreter [args]

log_file [args] [[-a] file]

log_user -info|0|1

match_max [-d] [-i spawn_id] [size]

overlay [-# spawn_id] [-# spawn_id] […] program [args]

parity [-d] [-i spawn_id] [value]

remove_nulls [-d] [-i spawn_id] [value]

send [-flags] string ★

将字符串发送给当前进程。

  • -i
  • -null
  • -break
  • -s
  • -h

send_error [-flags] string

send_log [–] string

send_tty [-flags] string

send_user [-flags] string

sleep seconds

spawn [args] program [args] ★

spawn 是繁衍的意思。新建一个进程,运行指定命令,标准输入,标准输出、标准错误都在 expect 环境内,方便其他 expect 命令进行后续处理。
spawn 启动进程时,会产生一个变量 spawn_id, 存储了进程的描述符。用 close 关闭当前进程及其相关文件。

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

strace level

stty args

system args

timestamp [args]

trap [[command] signals]

wait [args]

范例

写成 expect 脚本,可以接收参数,[lindex $argv 0]表示调用第一个参数。

#!/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 123456 |passwd --stdin haha\n" }
send "exit\n"
expect eof

#./ssh4.exp 10.0.0.7 root 123456
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
 "yes/no" { send "yes\n";exp_continue }
 "password" { send "$password\n" }
}
expect "]#" { send "useradd hehe\n" }
expect "]#" { send "echo 123456 |passwd --stdin hehe\n" }
expect "]#" { send "exit\n" }
expect eof
EOF

#./ssh5.sh 192.168.8.10 root 123456

expect
http://blog.lujinkai.cn/运维/基础/shell脚本编程/expect/
作者
像方便面一样的男子
发布于
2020年12月9日
更新于
2023年12月5日
许可协议