退出 #!/usr/bin/expect 当状态为 VPN 隧道关闭时

Exit #!/usr/bin/expect when Status was VPN tunnel closed

我正在使用 expect 脚本


#!/usr/bin/expect -f
set force_conservative 0  ;
if {$force_conservative} {
   set send_slow {1 .1}
   proc send {ignore arg} {
           sleep .1
           exp_send -s -- $arg
   }
}
set timeout -1
spawn $env(SHELL)
match_max 100000
send -- "cd /home/forticlientsslvpn/64bit/\r"
send -- "./forticlientsslvpn --server ip:port --vpnuser UN"
expect -exact "./forticlientsslvpn --server ip:PORT  --vpnuser UN"
send -- "\r"
expect -exact "\r
Password for VPN:"
send -- "PW\r"
expect -exact "\r
STATUS::Setting up the tunnel\r
STATUS::Connecting...\r"
send -- "Y\r"
expect eof

将连接 VPN 的文件, VPN连接成功后会显示STATUS::Tunnel running, 但是当连接关闭时,STATUS will be Tunnel closed 脚本仍然会继续 运行

STATUS::Stopping tunnel
STATUS::Tunnel closed...```

How to exit the script when we get ```STATUS::Tunnel closed```

问题是你产生了一个 shell,当 vpn 客户端关闭时,shell 仍然是 运行。

直接生成 vpn 客户端:

#!/usr/bin/expect -f
set timeout -1
cd /home/forticlientsslvpn/64bit
spawn ./forticlientsslvpn --server ip:port --vpnuser UN
match_max 100000
expect "Password for VPN:"
send -- "PW\r"
expect "STATUS::Connecting..."
send -- "Y\r"
expect eof

通过在脚本的最后几行添加expect Stopping和expect closed将退出脚本。

expect Stopping
expect closed
exit