Search code examples
macosbashtelnetexpect

How to "expect" a variable in an expect sript?


I am trying to automate some telnet commands for setting up a printer. The expected output from the terminal if I do manually is as follows :

Ebins-Mac-Mini:~ ebin$ telnet 192.168.4.104
Trying 192.168.4.104...
Connected to 192.168.4.104.
Escape character is '^]'.


Welcome to TSP100LAN TELNET Utility.
Copyright(C) 2008 Star Micronics co., Ltd.

<< Connected Device >>
   Device Model : TSP143 (STR_T-001)
   MAC Address  : 00:11:62:08:5B:F6

login: root         
password: ******

Hello root

=== Main Menu ===
  1) IP Parameters Configuration
  2) System Configuration
  3) Change Password
 96) Display Status
 97) Reset Settings to Defaults
 98) Save & Restart
 99) Quit

Enter Selection: 1

=== IP Parameters Menu ===
  1) Static
         IP Address      : 192.168.4.104
         Subnet Mask     : 255.255.255.0
         Default Gateway : 192.168.4.1
  2) Dynamic
         DHCP            : DISABLE
 99) Back to Main Menu

Enter Selection: 1

=== Static IP Address ===
  1) IP Address      : 192.168.4.104
  2) Subnet Mask     : 255.255.255.0
  3) Default Gateway : 192.168.4.1
 99) Back to IP Address Menu

Enter Selection: 

I managed to get it done till the second selection. However, for the second selection,IP Address : 192.168.4.104 is supposed to be a variable which I pass as an argument. How do I "expect" this ?? The following is my code so far.

#!/usr/bin/expect -f
        set timeout 20
    set IPaddress [lindex $argv 0]

    set Username "root"
    set Password "public"
    spawn telnet $IPaddress
    expect "login: "
    send "$Username\r"
    expect "password: "
    send "$Password\r"
    expect {"Hello root\n\n"
        "=== Main Menu ===\n"
        "1) IP Parameters Configuration\n"
        "2) System Configuration\n"
        "3) Change Password\n"
        "96) Display Status\n"
        "97) Reset Settings to Defaults\n"
        "98) Save & Restart\n"
        "99) Quit\n\n"
        "Enter Selection:"
    }
    send 1
    send "\r"
    expect {"=== IP Parameters Menu ===\n"
        " 1) Static\n"
        " IP Address      : "
        "$IPaddress\n"
        " Subnet Mask     : 255.255.255.0\n"
        " Default Gateway : 192.168.4.1\n"
        " 2) Dynamic\n"
        " DHCP            : DISABLE\n"
        " 99) Back to Main Menu\n\n"
        " Enter Selection: "
    }
    send 1
    send "\r"

It seems like the program reaches timeout and just exits. I am suspecting that I wrote the expect lines wrong. Any help would be appreciated.


Solution

  • Don't "expect" full output; instead, just focus on sequences that uniquely identify the current state of whatever you are executing. In your case, that could be the menu headers, for example "=== Main Menu ===". If you know in advance that every time such and such menu is displayed you will pick option X, then that's the best way to handle it. Expecting too much is prone to errors.

    Edited: Here's a sample snippet:

    expect "=== Main Menu ==="
    send "1\r"
    expect "=== IP Parameters Menu ==="
    send "1\r"