Expect script to automate telnet session

Hi all,

I am currently running a daemon which creates a virtual terminal for testing purposes. Essentially, if I were to interact with it manually, this is what I get.

 
john@test1:~$telnet localhost 7777
Trying ::1...
Connected to localhost.
Escape character is '^]'
mip6d> pl
eth2 2001:a:b:2:3:4:5:10/64
  valid 86400 / 86400 preferred
mip6d>

I would like to write a script that runs those commands for me and outputs the result to the shell. So by running the script, I should see the following in my terminal window:

 
eth2 2001:a:b:2:3:4:5:10/64
  valid 86400 / 86400 preferred

I have written a script, but it isn't exactly doing what I want.

 
#!/usr/bin/expect
spawn telnet localhost 7777
expect "mip6d>"
send "pl\r"

Unfortunately, the result i get is:

john@test1:~$ ./test.sh
spawn telnet localhost 7777
Trying ::1...
Connected to localhost.
Escape character is '^]'.
mip6d> 

The scripts then terminate. Can anyone give me a hand to get this working?

Thank you!

Try

#!/usr/bin/expect
spawn telnet localhost 7777
expect "mip6d>" { send "pl\r" }
interact

Thank you!

That allowed me to execute the command "pl". However, I do not wish to allow interaction with the telnet session. I have tried putting exit at the end of the script and it does not work. If i remove the interact command, I can terminate the script, but the command "pl" does not work.

I would also like to suppress all output, and only output the result from the "pl" command.

Any ideas?

Try

#!/usr/bin/expect
spawn telnet localhost 7777
expect "mip6d>" { send "pl\r" 
send -- \033
sleep 1
send "quit\r" }
interact

Thank you!

I have modified that to be the following:

#!/usr/bin/expect
spawn telnet localhost 7777
expect "mip6d>" { send "pl\r" 
send "exit\r" }
interact

Currently, I am getting:

spawn telnet localhost 7777
Trying ::1...
Connected to localhost.
Escape character is '^]'.
mip6d> pl
exit
eth2 2001:a:b:2:3:4:5:10/64
  valid 86400 / 86400 preferred
mip6d> Connection closed by foreign host.

Is there a way to supress all the other information except the output? I would like to have only

eth2 2001:a:b:2:3:4:5:10/64
  valid 86400 / 86400 preferred

returned to the terminal window.

Thanks again :slight_smile:

Basing on http://objectmix.com/tcl/376951-expect-suppress-output-screen.html this might work (but I didn't test it):

#!/usr/bin/expect
log_user 0
spawn telnet localhost 7777
expect "mip6d>" { 
log_user 1
send "pl\r" 
log_user 0
send "exit\r" }
log_user 1
interact

Thank you again for your reply! I think I am almost getting there.

I modified your code as follows:

 
#!/usr/bin/expect
log_user 0
spawn telnet localhost 7777
expect "mip6d>" { 
log_user 1
send "pl\r"}
expect "mip6d>" {
log_user 0
send "exit\r" }
interact

I now get this output:

pl
eth2 2001:a:b:2:3:4:5:10/64
  valid 86400 / 86400 preferred
mip6d> exit
Connection closed by foreign host.

I can't find a way to get the inputted commands and the final exit message to go away. Do you know what's wrong?

Thank you :slight_smile: