运维自动化实战(一)Linux Shell 自动交互

本文主要介绍自动化输入脚本的方式。

关键词:shell

bash脚本输入方式

1
echo -n "ServiceName\nOperator\nPassword" | upx login

输入每行文字后输入一个回车,某些情况不适用。

Expect脚本

1
2
3
4
5
6
7
8
9
#!/bin/bash    
/usr/bin/expect <<-EOF
set timeout 50
spawn upx login
expect "*ServiceName*" {send "ServiceName\r"}
expect "*Operator*" {send "Operator\r"}
expect "*Password*" {send "Password\r"}
expect eof
EOF

expect 提示的字段是捕获输出,然后在后面跟着输入。