使用expect命令实现交互式输入参数自动启动VPN程序。脚本实现如下;
[root@localhost serviceclient]# cat VpnStart.sh
#!/bin/sh
username='ivs-001'
password='Edmund@110'
/usr/bin/expect <<-EOF
cd /usr/local/SecoClient/serviceclient/
set timeout -1
spawn ./SecoClientCS
expect "**3:openlab**"
send "3\r"
expect "**4:Cancel**"
send "1\r"
expect "**Please input the login user name**"
send "$username\r"
expect "**Please input the login user password**"
send "$password\r"
expect "**connect Success,Enjoy!(^_^)**"
EOF
[root@localhost serviceclient]#
注意事项;
- set timeout -1即永不超时
- \r表示回车
- spawn 启动进程
- send 发送命令
- expect 匹配字符