Search code examples
macostelnetexpect

Using expect on a Mac to access a router via telnet


I'm trying to write a script on a Mac which should access a router via telnet. This is to enhance the router's power, which cannot be done via web.

The problem is that the telnet channel is blocked, and in order to unlock it I need to run the following instruction:

/users/shared/telnetenable - 200CC8132A36 admin password >/dev/udp/192.168.0.1/23

Now, I can open a telnet connection, but in order to send commands to the router I need to do all that with expect. So, my file begins with:

#!/usr/bin/expect -f

and all instructions are preceded by spawn, e.g.

spawn telnet 192.168.0.1

while the command sent to the router is:

send "wl -a wl0 txpwr 100\n"

My problem is that I do not know how to run via spawn the instruction that unlocks telnet on the router. Can anybody help me?


Solution

  • You don't have to use spawn to run a non-interactive command. Tcl's exec command is enough. For example:

    #!/usr/bin/expect
    
    # the ``/dev/udp/host/port'' syntax is bash specific
    exec bash -c "/users/shared/telnetenable - 200CC8132A36 \
                  admin password > /dev/udp/192.168.0.1/23"
    
    spawn telnet 192.168.0.1
    ... ...
    

    Expect also has a system command so you can also

    system "/users/shared/telnetenable - 200CC8132A36 \
            admin password > /dev/udp/192.168.0.1/23"