Run shell commands in expect script as reaction to spawned action
<p>I am trying to create an expect script for <code>FortiClient VPN</code> connection. I base my solution on <a href="https://stackoverflow.com/a/19348728/1549135">https://stackoverflow.com/a/19348728/1549135</a> which works really good.</p>

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

<p>I have managed to put down a working solution like this:</p>

<pre><code>#!/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 &gt;@stdout 2&gt;@stderr ./enable_internet.sh

interact
</code></pre>

<p>enable_internet.sh</p>

<pre><code>#!/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"
</code></pre>

<h2>Now I have two problems.</h2>

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