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""
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…