Search code examples
tclexpect

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


i am using an expect script


#!/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

file which will connect VPN, when VPN connection is successful it will display STATUS::Tunnel running, But when connection closed, STATUS will be Tunnel closed and still the script will keep on running

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

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

Solution

  • By adding expect Stopping and expect closed in the last lines of the script will exit the script.

    expect Stopping
    expect closed
    exit