Expect script to automate telnet session


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect script to automate telnet session
# 1  
Old 09-12-2010
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.

Code:
 
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:

Code:
 
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.

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

Unfortunately, the result i get is:
Code:
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!
# 2  
Old 09-12-2010
Try
Code:
#!/usr/bin/expect
spawn telnet localhost 7777
expect "mip6d>" { send "pl\r" }
interact

# 3  
Old 09-12-2010
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?
# 4  
Old 09-12-2010
Try
Code:
#!/usr/bin/expect
spawn telnet localhost 7777
expect "mip6d>" { send "pl\r" 
send -- \033
sleep 1
send "quit\r" }
interact

# 5  
Old 09-12-2010
Thank you!

I have modified that to be the following:

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

Currently, I am getting:

Code:
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

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

returned to the terminal window.

Thanks again Smilie
# 6  
Old 09-12-2010
Basing on http://objectmix.com/tcl/376951-expe...ut-screen.html this might work (but I didn't test it):
Code:
#!/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

# 7  
Old 09-12-2010
Thank you again for your reply! I think I am almost getting there.

I modified your code as follows:

Code:
 
#!/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:

Code:
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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to break out of hung or waiting telnet session in shell script?

I have to put together telnet instructions for 100s of hosts for verifying basic connectivity and get output in a neat format. Problem- If a telnet is hung with message "Trying .... <hostname" due to firewall or routing issue the commands waits for a very long time before it times out and my... (2 Replies)
Discussion started by: desiphantom
2 Replies

2. Shell Programming and Scripting

expect telnet script execute by cronjob

hi, please help, keep getting this bolded error and look it up and people say its your environment variable though i tried to set it manually in expect..it run fine if i run it manually but once i run it by cronjob it error below..i tried to comment out ip/login info with *.. logfile:: START... (0 Replies)
Discussion started by: cssanangeles
0 Replies

3. Shell Programming and Scripting

Calling Expect Script - Telnet

Hi All, I have an Expect script which logs into Cisco switch, performs a show interface command. I want to read a file of ip addresses which will be passed to the expect script. The script to read the file works, the expect script works on it's own but when i call the 'expect' script from the... (12 Replies)
Discussion started by: trinak96
12 Replies

4. Shell Programming and Scripting

EXPECT script for Telnet automation. Need your support.

Dear experts, please help me . I've found simple EXPECT scripts and all works fine. But I need more automation in error handling and sending list of commands/output logging from multiple remote hosts. I have 10 hosts, for example: host1 192.168.1.1 LOGIN1 PASSWORD1 ...... ... (2 Replies)
Discussion started by: starchen
2 Replies

5. Shell Programming and Scripting

Unable to close telnet session from script

Hello people, I am creating a script that will alert me in an ftp or telnet account on my system expires. FTP part is ok, but when I am entering the 3 script, it stucks. I can see that the script will not close the telnet seession. Can you please check and let me know what I am doing wrong? What if... (7 Replies)
Discussion started by: drbiloukos
7 Replies

6. Shell Programming and Scripting

Expect Script to Automate SSH

How would I write an expect script to automate ssh and what file extention do expect files use? (11 Replies)
Discussion started by: Prodiga1
11 Replies

7. Shell Programming and Scripting

How to automate sftp without using expect script?

How to automate sftp with out using expect script? My batch file has the password but it is not taking. Please see below. I want to use this sftp connection in a loop for pushing new files in a directory one at a time. Hence I can not use an expect script. bash-2.05$... (5 Replies)
Discussion started by: Tuxidow
5 Replies

8. Shell Programming and Scripting

Telnet Expect script question

Hi all, I have written a small expect script which should spawn a telnet session login and execute some commands. #!/usr/bin/expect -f spawn telnet $env(IP) match_max 100000 expect "login:" send -- "******\n" expect -exact "Password:" send -- "****\n" expect "%" Now I have got... (2 Replies)
Discussion started by: stinkefisch
2 Replies

9. Shell Programming and Scripting

closing a telnet session on error, in a shell script

I am using a shell script to telnet into a remote unix server and execute certain commands. But if any command being executed remotely, throws an error, the script just hangs. And the telnet session does not get closed. I want to be able to close the session and complete the script execution in... (1 Reply)
Discussion started by: farahzaiba
1 Replies
Login or Register to Ask a Question