From: Pothuri, S. K <spo...@vi...> - 2009-06-08 05:24:00
|
Hi, I would appreciate if someone can help me with this. I am writing a script in Perl that uses expect feature to SSH to a server and perform ping and telnet tests from that server. 1. Spawn SSH to a server 2. Send commands for ping and telnet (one after another) to the spawned command (SSH). My question is how I can interrupt/terminate the telnet process if it is taking too long to complete. I am trying to send "\cC" (control - C) if my expect times out waiting for something after sending the telnet command, but it doesn't seem to be working. sub log_in { my ($line,$userid,$passwd) = @_; my $exp = new Expect; my $command = "ssh -l $userid $line"; .................... ..................... ..................... ..................... $exp->send("telnet $telnetip $telnetport\n"); if($exp->expect(5, 'Escape character is')) { print "Telnet successful.\n"; $exp->send("\^]\n"); $exp->expect(30, 'telnet'); $exp->send("quit\r\n"); ................. ................. ................. ................. else { ............... ............... print "Telnet Failed.\n"; $exp->send("\cC\r"); =========> This is not working as expected. It does not terminate telnet process. I even tried hex and octal representations. if($exp->expect(5, $PROMPT)) Appreciate any help. Thanks in advance. Thanks Satish |
From: Robin L. P. <rlp...@di...> - 2009-06-08 17:05:19
|
On Sun, Jun 07, 2009 at 10:08:25PM -0700, Pothuri, Satish K wrote: > Hi, > > > > I would appreciate if someone can help me with this. > > > > I am writing a script in Perl that uses expect feature to SSH to a > server and perform ping and telnet tests from that server. > > > > 1. Spawn SSH to a server > 2. Send commands for ping and telnet (one after another) to the > spawned command (SSH). > > > > My question is how I can interrupt/terminate the telnet process if it is > taking too long to complete. I am trying to send "\cC" (control - C) if > my expect times out waiting for something after sending the telnet > command, but it doesn't seem to be working. Telnet is a terminal emulator; it passes \cC to the remote side. Try it on the command line; you'll see it doesn't work (assuming a connection has been made). Try \c] followed by "exit" (or whatever command your telnet wants; do ^] on a live connection by hand to see the menu). -Robin |
From: Ian M. <iw...@do...> - 2009-06-09 13:23:02
|
Pothuri, Satish K wrote: > Hi, > > > > I would appreciate if someone can help me with this. > > > > I am writing a script in Perl that uses expect feature to SSH to a > server and perform ping and telnet tests from that server. > > > > 1. Spawn SSH to a server > 2. Send commands for ping and telnet (one after another) to the > spawned command (SSH). > > > > My question is how I can interrupt/terminate the telnet process if it is > taking too long to complete. I am trying to send “\cC” (control – C) if > my expect times out waiting for something after sending the telnet > command, but it doesn’t seem to be working. > > Telnet and ssh both support escape characters which can specified on the command line. Providing the escape characters for ssh and telnet are different, send the telnet escape, when you get the telnet prompt send the telnet command quit to close the connection and exit telnet. Ian |
From: Robin L. P. <rlp...@di...> - 2009-06-09 15:50:07
|
On Tue, Jun 09, 2009 at 01:22:05PM +0100, Ian Moor wrote: > Telnet and ssh both support escape characters which can specified > on the command line. Providing the escape characters for ssh and > telnet are different, send the telnet escape, when you get the > telnet prompt send the telnet command quit to close the connection > and exit telnet. We've been over that, I'm afraid; this is before the connection has succeeded. The escape key is ignored. At the commandline, ^C works in this state, but apparently not in his script. -Robin -- They say: "The first AIs will be built by the military as weapons." And I'm thinking: "Does it even occur to you to try for something other than the default outcome?" See http://shrunklink.com/cdiz http://www.digitalkingdom.org/~rlpowell/ *** http://www.lojban.org/ |
From: Austin S. <te...@of...> - 2009-06-09 16:36:39
|
On Tue, 9 Jun 2009 08:49:55 -0700 Robin Lee Powell <rlp...@di...> wrote: > On Tue, Jun 09, 2009 at 01:22:05PM +0100, Ian Moor wrote: > > Telnet and ssh both support escape characters which can specified > > on the command line. Providing the escape characters for ssh and > > telnet are different, send the telnet escape, when you get the > > telnet prompt send the telnet command quit to close the connection > > and exit telnet. > > We've been over that, I'm afraid; this is before the connection has > succeeded. The escape key is ignored. At the commandline, ^C > works in this state, but apparently not in his script. > What happens if you close() it? what happens if you kill the pid()? Austin |
From: Robin L. P. <rlp...@di...> - 2009-06-09 16:40:50
|
On Tue, Jun 09, 2009 at 09:18:37AM -0700, Austin Schutz wrote: > On Tue, 9 Jun 2009 08:49:55 -0700 Robin Lee Powell > <rlp...@di...> wrote: > > > On Tue, Jun 09, 2009 at 01:22:05PM +0100, Ian Moor wrote: > > > Telnet and ssh both support escape characters which can > > > specified on the command line. Providing the escape characters > > > for ssh and telnet are different, send the telnet escape, when > > > you get the telnet prompt send the telnet command quit to > > > close the connection and exit telnet. > > > > We've been over that, I'm afraid; this is before the connection > > has succeeded. The escape key is ignored. At the commandline, > > ^C works in this state, but apparently not in his script. > > > > What happens if you close() it? what happens if you kill the > pid()? He's doing it through ssh, i.e. ssh to box X, telnet to box Y from there. -Robin -- They say: "The first AIs will be built by the military as weapons." And I'm thinking: "Does it even occur to you to try for something other than the default outcome?" See http://shrunklink.com/cdiz http://www.digitalkingdom.org/~rlpowell/ *** http://www.lojban.org/ |
From: Roland G. <rgi...@cp...> - 2009-06-10 11:16:47
|
How about directly doing a "ssh -t -l $user $line telnet $telnetip $telnetport" and using close() (and maybe pid() and kill()) in case of timeout just as Austin suggested? HTH, Roland |
From: Satish P. <sat...@gm...> - 2009-06-11 03:30:12
|
I can not use this solution as I need to ssh and then perform many other tests apart from telnet. So I can not use a single ssh just for telnet. Killing it using PID requires us to login again using another ssh session which is not the requirement, considering that there will be more than 1 telnet tests I will be performing. Sending $exp->send("\003") should work, but I am checking if any other issue is preventing this. On Wed, Jun 10, 2009 at 4:27 PM, Roland Giersig <rgi...@cp...> wrote: > How about directly doing a > > "ssh -t -l $user $line telnet $telnetip $telnetport" > > and using close() (and maybe pid() and kill()) in case of timeout just > as Austin suggested? > > HTH, Roland > > > > > ------------------------------------------------------------------------------ > Crystal Reports - New Free Runtime and 30 Day Trial > Check out the new simplified licensing option that enables unlimited > royalty-free distribution of the report engine for externally facing > server and web deployment. > http://p.sf.net/sfu/businessobjects > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss > |
From: Bryan B. <br...@bu...> - 2009-06-11 13:28:24
|
> I can not use this solution as I need to ssh and then perform many other > tests apart from telnet. So I can not use a single ssh just for telnet. > Killing it using PID requires us to login again using another ssh session > which is not the requirement, considering that there will be more than 1 > telnet tests I will be performing. > Sending $exp->send("\003") should work, but I am checking if any other > issue > is preventing this. > Is telnet hanging because the IP address is not online? It seems that is the behavior from my testing. Why not skip the telnet portions if it doesnt respond to ping? You could write echo statements on failure and expect against that: $ping_success = 1; $exp->send("ping -W 1 -c 3 $ip || echo PING_FAILED"); $exp->expect(5, [ "^PING_FAILED" => sub {$ping_success = 0; exp_continue;} ], $shell_prompt); if ( $ping_success ) { {do telnets here}; } Bryan Bueter http://sourceforge.net/projects/rover |