Atais
Atais

Reputation: 11275

Run shell commands in expect script as reaction to spawned action

I am trying to create an expect script for FortiClient VPN connection. I base my solution on https://stackoverflow.com/a/19348728/1549135 which works really good.

After successful connection, there is a message saying STATUS::Tunnel running. Then I have to change my route settings so that only certain range of IPs would operate over VPN. I try to accomplish it in one expect script.

I have managed to put down a working solution like this:

#!/usr/bin/expect

spawn /opt/forticlient-sslvpn/64bit/forticlientsslvpn_cli --server $server_ip --vpnuser $user
expect "Password for VPN:" {send $password}
expect "to this server? (Y/N)\r" {send "y\r"}

expect "STATUS::Tunnel running\r" 
exec >@stdout 2>@stderr ./enable_internet.sh

interact

enable_internet.sh

#!/bin/bash
sudo route del default ppp0
sudo route add -net 172.20.0.0 netmask 255.255.0.0 dev ppp0
echo "Internet Enabled"

Now I have two problems.

  1. The exec script is launched before "STATUS::Tunnel running\r" appears on the screen. This might be because there is no {} but if I add them - it does not work at all.
  2. I want to have everything in one file, so basically I need to move the bash commands to expect script and call them directly. How to do it?

Upvotes: 1

Views: 8393

Answers (1)

Atais
Atais

Reputation: 11275

OK I have found out what is going on.

The final answer is:

  1. use set timeout -1 to wait longer for the text. By default expect waits only 10sec.

  2. use exec from tcl just like with the script (source: http://www.tek-tips.com/viewthread.cfm?qid=194125)

complete answer:

#!/usr/bin/expect -f

spawn /opt/forticlient-sslvpn/64bit/forticlientsslvpn_cli --server $server_ip --vpnuser $user
expect "Password for VPN:" {send $password}
expect "to this server? (Y/N)\r" {send "y\r"}

set timeout -1
expect "Tunnel running" {
    sleep 2
    exec >@stdout 2>@stderr route del default ppp0 
    exec >@stdout 2>@stderr route add -net 172.20.0.0 netmask 255.255.0.0 dev ppp0
    send_user "Internet Enabled\r"
}

interact

Upvotes: 3

Related Questions