expect

In Expect script the simple patterns (which look correct) never matched the output


Is there a way to optimize an expect command to make it faster? This is my current code which runs fine, but it takes around 25-30 seconds to complete, this is not related how it longs take to connect to the VPN, the VPN connectivity process that takes around 3-5 seconds if I do it manually, seems the slowness is more related to the expect command:

#!/usr/bin/expect -f

spawn atlasvpn connect
expect "*Enter a number corresponding to the desired category:*" 
send "1\r"
expect "*Enter a number corresponding to the desired server:*" 
send "1\r"
interact

Is there something that I can improve?

This screenshot is before the first send "1\r", selecting any of these options takes 1 second to display the next screenshot (server selection).

enter image description here

This screenshot is after the first send "1\r" is sent. Selecting any of these options takes around 3-5 seconds to connect to the VPN.

enter image description here

expect -d script.sh output the following, seems is validating each result in the output against my expect match:

expect: does "1 - Standard\r\n2 - Streaming\r\n3 - SafeSwap\r\n4 - MultiHop\r\n\u001b[?25l\u001b[2K\r\u001b[1m\u001b[31m\u2717\u001b[0m \u001b[1mEnter a number corresponding to the desired category\u001b[0m\u001b[1m:\u001b[0m \u2588\r\n\u001b[J\u001b[2K\r \u0008\u001b[J\u001b[2K\r1\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[1m\u001b[32m\u2714\u001b[0m \u001b[1mEnter a number corresponding to the desired category\u001b[0m\u001b[1m:\u001b[0m 1\u2588\r\n1\u001b[J\u001b[2K\r \u0008\u001b[J\u001b[2K\r \u0008\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[1m\u001b[32m\u2714\u001b[0m \u001b[1mEnter a number corresponding to the desired category\u001b[0m\u001b[1m:\u001b[0m 1\u2588\r\n \u0008\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[2mEnter a number corresponding to the desired category\u001b[0m\u001b[2m:\u001b[0m 1\r\n \u0008\u001b[J\u001b[2K\r\u001b[?25h \u0008\u001b[J\u001b[2K\r1 - Netherlands - Amsterdam\r\n2 - United States - New York\r\n3 - Hong Kong - Hong Kong\r\n4 - United States - Dallas\r\n5 - United States - Los Angeles\r\n6 - Singapore - Singapore\r\n7 - Finland - Helsinki\r\n8 - Norway - Oslo\r\n9 - New Zealand - Auckland\r\n10 - Argentina - Buenos Aires\r\n11 - Hungary - Budapest\r\n12 - Denmark - Copenhagen\r\n13 - Germany - Frankfurt\r\n14 - Bulgaria - Sofia\r\n15 - Sweden - Stockholm\r\n16 - United States - New Jersey\r\n17 - Netherlands - Amsterdam\r\n18 - UAE - Dubai\r\n19 - Serbia - Belgrade\r\n20 - Japan - Tokyo\r\n21 - United Kingdom - London\r\n22 - United Kingdom - Manchester\r\n23 - France - Paris\r\n24 - Poland - Warsaw\r\n25 - Latvia - Riga\r\n26 - Ireland - Dublin\r\n27 - Estonia - Tallinn\r\n28 - Czech Republic - Prague\r\n29 - United States - Las Vegas\r\n30 - Slovakia - Bratislava\r\n31 - United Kingdom - London\r\n32 - Italy - Milan\r\n33 - Spain - Madrid\r\n34 - Canada - Montreal\r\n35 - Turkey - Istanbul\r\n36 - Brazil - Sao Paulo\r\n37 - United States - Miami\r\n38 - Mexico - Mexico City\r\n39 - South Africa - Johannesburg\r\n40 - United States - Phoenix\r\n41 - Israel - Tel Aviv\r\n42 - Chile - Santiago\r\n43 - Thailand - Bangkok\r\n44 - Belgium - Brussels\r\n45 - Albania - Tirana\r\n46 - Romania - Bucharest\r\n47 - Switzerland - Z\u00fcrich\r\n48 - Portugal - Lisbon\r\n49 - Australia - Sydney\r\n50 - Austria - Vienna\r\n\u001b[?25l\u001b[2K\r\u001b[1m\u001b[31m\u2717\u001b[0m \u001b[1mEnter a number corresponding to the desired server\u001b[0m\u001b[1m:\u001b[0m \u2588\r\n\u001b[J\u001b[2K\r \u0008" (spawn_id exp4) match glob pattern "*Enter a number corresponding to the desired server:*"? no
expect: timed out
send: sending "1\r" to { exp4 }
tty_raw_noecho: was raw = 0  echo = 1
spawn id exp4 sent <\u001b[J\u001b[2K\r1\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[1m\u001b[32m\u2714\u001b[0m \u001b[1mEnter a number corresponding to the desired server\u001b[0m\u001b[1m:\u001b[0m 1\u2588\r\n1\u001b[J\u001b[2K\r \u0008\u001b[J\u001b[2K\r \u0008\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[1m\u001b[32m\u2714\u001b[0m \u001b[1mEnter a number corresponding to the desired server\u001b[0m\u001b[1m:\u001b[0m 1\u2588\r\n \u0008\u001b[J\u001b[2K\r\u001b[1A\u001b[2K\r\u001b[2K\r\u001b[2mEnter a number corresponding to the desired server\u001b[0m\u001b[2m:\u001b[0m 1\r\n \u0008\u001b[J\u001b[2K\r\u0Enter a number corresponding to the desired server: 1
spawn id exp4 sent <You have successfully connected to Netherlands - Amsterdam.\r\n>
You have successfully connected to Netherlands - Amsterdam.
interact: received eof from spawn_id exp4
tty_set: raw = 0, echo = 1
tty_set: raw = 3, echo = 0

Solution

  • In the expect -d output it has:

    Enter a number corresponding to the desired category\u001b[0m\u001b[1m:
    ... ...
    Enter a number corresponding to the desired server\u001b[0m\u001b[1m:
    

    As we can see there are other chars before the : char. You can just write like this:

    expect "Enter a number corresponding to the desired category" 
    send "1\r"
    expect "Enter a number corresponding to the desired server" 
    send "1\r"