Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

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

"
expect "Password:"
send "password

"
expect "*$*"
send "AT

"
expect "OK"
send "AT&CSR
"
expect "OK"
send "AT!G=A6
"
expect "OK"
send "AT^MI=0
"
expect "OK"
send "AT^SX=0
"
expect "OK"
send "AT^SR=0,1"
expect "*$*"
send "exit
"

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

" to { exp4 }
> 
> expect: does "" (spawn_id exp4) match glob pattern "OK"? no
> ********
> 
> OK
> 
> expect: does "********

OK
" (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)
> "********

OK" send: sending "AT&CSR
" to { exp4 }
> 
> expect: does "
" (spawn_id exp4) match glob pattern "OK"? no AT
> 
> expect: does "
AT
" (spawn_id exp4) match glob pattern "OK"? no
> 
> OK
> 
> expect: does "
AT

OK
" (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)
> "
AT

OK" send: sending "AT!G=A6
" to { exp4 }
> 
> expect: does "
" (spawn_id exp4) match glob pattern "OK"? no AT&CSR
> expect: does "
AT&CSR" (spawn_id exp4) match glob pattern "OK"? no
> AT!G=A6 expect: does "
AT&CSRAT!G=A6" (spawn_id exp4) match glob
> pattern "OK"? no expect: timed out send: sending "AT^MI=0
" to { exp4
> }
> 
> expect: does "
AT&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""

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The way you send your AT commands is wrong.

According to ETSI specification 127.007, by default AT commands are terminated by (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 instead of AT , AT&CSR instead of AT&CSR 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 synthax:

ATS3=<value>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...