LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2008, 05:33 AM   #1
nik1984
LQ Newbie
 
Registered: Sep 2007
Posts: 7

Rep: Reputation: 0
Expect Scripting:- script not coming out of telnet session.


My expect script not coming out of telnet session.

code gets execute till line just before line "Before Logout" & remain in stuck state.
============================================================
script sample

#!/usr/bin/expect -f

# this script will take commandline arguments as - Telnet Server / portno / hostname

exp_internal 1

foreach {var1 var2 var3} $argv {}
if {$argc != 3} {puts "You have entered wrong number of arguments"
puts "Usage:- $argv0 Terminal-Server-Name PortNo - hostname"
exit
}


set timeout -1
spawn telnet $var1 $var2

send -- "\r"
expect -re ".*login:"
send -- "admin\r"
expect -exact "Password:"
send -- "admin\r"

sleep 2

send -- "config hostname $var3\r"
expect -exact "
\r\r
\r
\rNik> [m"

send -- "show hostname \r"
expect -exact "
\r\r
\r
Hostname\r
\r
\r
Hostname - $var3
\r\r
\rNik> "

#expect -exact "
#\r\r
#\r
#Hostname\r
#\r
#\r
#Hostname - $var3
#\r\r
#\rNik> "
/r
puts "Before Logout"
send -- "\r"
#END Telnet
send -- "^D"\r
send -- "^]"
expect -exact "\r
telnet> "

send -- "quit\r"
expect eof

==================================
need suggestions.....

-Thanks in advance
 
Old 09-11-2008, 08:04 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Please post source code in code tags:
Code:
like this
It helps us to read your script/program.

You are using the expect function to expect a very long string, including new lines. I would imagine that this is not the exact string which the spawned telnet session is producing.

I would recommend expecting only the prompt, "Nik>" instead. I assume this prompt will not return until the sent command is seen.

I know that when using expect with a telnet session, you send \r at the end of the line to send the command, but I am not sure if that is what is output by programs in your telnet session... the problem may be that the program outputs \n, not \r. However, I still think it's best to just expect the prompt.
 
Old 09-11-2008, 08:14 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
I am doing it this time, dont expect this again

Code:
#!/usr/bin/expect -f

# this script will take commandline arguments as - Telnet Server / portno / hostname

exp_internal 1

foreach {var1 var2 var3} $argv {}
if {$argc != 3} {puts "You have entered wrong number of arguments"
puts "Usage:- $argv0 Terminal-Server-Name PortNo - hostname"
exit
}


set timeout -1
spawn telnet $var1 $var2

send -- "\r"
expect -re ".*login:"
send -- "admin\r"
expect -exact "Password:"
send -- "admin\r"

sleep 2

send -- "config hostname $var3\r"
expect -exact "
\r\r
\r
\rNik> [m"

send -- "show hostname \r"
expect -exact "
\r\r
\r
Hostname\r
\r
\r
Hostname - $var3
\r\r
\rNik> "

#expect -exact "
#\r\r
#\r
#Hostname\r
#\r
#\r
#Hostname - $var3
#\r\r
#\rNik> "
/r
puts "Before Logout"
send -- "\r"
#END Telnet
send -- "^D"\r
send -- "^]"
expect -exact "\r
telnet> "

send -- "quit\r"
expect eof
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need some help in expect scripting anirudhasonar Programming 1 01-18-2007 12:11 PM
Expect scripting indienick Programming 2 09-22-2006 08:56 AM
telnet server not reliable for expect script powah Linux - Software 0 11-16-2005 11:07 AM
expect scripting Deltron3030 Linux - Newbie 2 12-16-2003 01:56 PM
shell script to autologin into a telnet session eastj1974 Linux - General 1 11-17-2003 07:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration