Search code examples
bashexpectat-command

bash telnet get stdout from expect


I need to get a dataset from a telnet shell, for this I have looked at expect https://www2.lib.uchicago.edu/keith/tcl-course/topics/expect.html

This is my expect script:

/usr/bin/expect -d <<  EOD
spawn telnet myhost
log_user 1
set timeout 10
expect "Login:"
send "user\n\r"
expect "Password:"
send "password\n\r"
expect "*$*"
send "AT\n\r"
expect "OK"
send "AT&CSR\n"
expect "OK"
send "AT!G=A6\n"
expect "OK"
send "AT^MI=0\n"
expect "OK"
send "AT^SX=0\n"
expect "OK"
send "AT^SR=0,1"
expect "*$*"
send "exit\r"

Now I can connect to the remote host, send command, but cannot read the results, here a shell result:

> expect: does " " (spawn_id exp4) match glob pattern "*"? yes expect:
> set expect_out(0,string) " " expect: set expect_out(spawn_id) "exp4"
> expect: set expect_out(buffer) " " send: sending "AT\n\r" to { exp4 }
> 
> expect: does "" (spawn_id exp4) match glob pattern "OK"? no
> ********
> 
> OK
> 
> expect: does "********\r\n\r\nOK\r\n" (spawn_id exp4) match glob
> pattern "OK"? yes expect: set expect_out(0,string) "OK" expect: set
> expect_out(spawn_id) "exp4" expect: set expect_out(buffer)
> "********\r\n\r\nOK" send: sending "AT&CSR\n" to { exp4 }
> 
> expect: does "\r\n" (spawn_id exp4) match glob pattern "OK"? no AT
> 
> expect: does "\r\nAT\r\n" (spawn_id exp4) match glob pattern "OK"? no
> 
> OK
> 
> expect: does "\r\nAT\r\n\r\nOK\r\n" (spawn_id exp4) match glob pattern
> "OK"? yes expect: set expect_out(0,string) "OK" expect: set
> expect_out(spawn_id) "exp4" expect: set expect_out(buffer)
> "\r\nAT\r\n\r\nOK" send: sending "AT!G=A6\n" to { exp4 }
> 
> expect: does "\r\n" (spawn_id exp4) match glob pattern "OK"? no AT&CSR
> expect: does "\r\nAT&CSR" (spawn_id exp4) match glob pattern "OK"? no
> AT!G=A6 expect: does "\r\nAT&CSRAT!G=A6" (spawn_id exp4) match glob
> pattern "OK"? no expect: timed out send: sending "AT^MI=0\n" to { exp4
> }
> 
> expect: does "\r\nAT&CSRAT!G=A6" (spawn_id exp4) match glob pattern
> "OK"? no AT^MI=0

If the result of the command doesn't match the expected it goes to next command, is there a way to catch the response result?

EDIT:
I tried to simplify thing and added interact, without no luck:

 /usr/bin/expect -d <<  EOD
> spawn telnet myhost
> log_user 1
> interact"
> EOD
expect version 5.45.4
argv[0] = /usr/bin/expect  argv[1] = -d  
set argc 0
set argv0 "/usr/bin/expect"
set argv ""
executing commands from command file
spawn telnet myhost
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {11306}
invalid command name "interact""
    while executing
"interact""

Solution

  • The way you send your AT commands is wrong.

    According to ETSI specification 127.007, by default AT commands are terminated by \r (IRA 13 - ASCII carriage return) character[1]. For further details, see paragraph "4.1 - Command line".

    In order to fix your issue, just send AT\r instead of AT\n\r, AT&CSR\r instead of AT&CSR\n and so on (the latter is a mistake you repeated for every following AT command). Otherwise the AT parser won't recognize your commands correctly.


    [1] Command line termination character can be customized by means of ATS3 command, with the following syntax:

    ATS3=<value>
    

    Where <value> is the decimal ASCII value of the new command line termination character (default is 13).