LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Expect Scripting:- script not coming out of telnet session. (https://www.linuxquestions.org/questions/programming-9/expect-scripting-script-not-coming-out-of-telnet-session-669162/)

nik1984 09-11-2008 05:33 AM

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

matthewg42 09-11-2008 08:04 AM

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.

vikas027 09-11-2008 08:14 AM

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



All times are GMT -5. The time now is 07:09 AM.