Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

perl-expect interact command

by Frits (Initiate)
on Jul 02, 2012 at 10:07 UTC ( #979407=perlquestion: print w/ replies, xml ) Need Help??
Frits has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, at the moment I have a script running that logs me automatically on routers, switches, firewalls, etc. Yet when I want to exit the device I have to do an additional exit to get my script ended. The part I'm looking for is that when interact(); detects my domain-name prompt in the CLI it will send an additional "exit" to log me off a jump-host. This will stop the part for me doing the exit manually.. (PS the interact is needed for me to do additional commands on the device manually) Any ideas? part of script is:

my $exp = new Expect; $exp = Expect->spawn("ssh $username\@domain-name\r"); $exp->expect( $timeout, ('domain-name')); $exp->send ("ssh admin\@$device\n"); $exp->expect( $timeout, [ '-re', qr'yes/no', sub { my $exp = shift; $exp->send("yes\n"); sleep 2; exp_continue; } ], [ '-re', qr'(C|c)onnection refused', sub {my $exp = shift; print color ("yellow on_red")," ssh not allowed or incorre +ct password ", color ("yellow on_black"),"\n"; print color ("reset"); $exp->send ("exit\n"); endscript (); } ], [ '-re', qr'(P|p)assword', sub {my $exp = shift; $exp->send ("$cpeenablepassword\n"); $exp->expect( $timeout, ("Finished")); } ] ); $exp->send ("terminal length 0\n"); $exp->expect( $timeout, ("#terminal length 0")); $exp->send ("show disks details\n"); $exp->expect( $timeout, ("#show disks details")); $exp->send ("show cms info\n"); $exp->expect( $timeout, ("#show cms info")); $exp->send ("show alarms\n"); $exp->expect( $timeout, ("#show alarms")); $exp->send ("show accelerator\n"); $exp->expect( $timeout, ("#show accelerator")); $exp->send ("terminal length 40\n"); $exp->expect( $timeout, ("#terminal length 40")); $exp->interact(); $exp->hard_close(); endscript (); } ####### ENDING AND END OF SCRIPT ####### sub endscript { print "\n\n\n The Session is Closed.!\n\n"; exit 1; eof; }

Comment on perl-expect interact command
Download Code
Re: perl-expect interact command
by Neighbour (Friar) on Jul 02, 2012 at 10:21 UTC
    Or you could add
    while ($exp->pid()) { $exp->send("exit\n"); }
    right after the $exp->interact(); to just keep sending 'exits' while the ssh connection is still open :)

      The pid is not closing the process as expected. Next to this is that this is hanging the script to be closed. Also the second option to just keep sending an exit also closes sessions at times that I'm still logged into the device.. I suspect that when there is an option of doing a expect in the interact will solve my issue. The point is that all options I have tried failed.

        Ok...how about looking at $exp->exitstatus() or using the $escape_sequence in $exp->interact()?
Re: perl-expect interact command
by salva (Monsignor) on Jul 02, 2012 at 12:00 UTC
    use Net::OpenSSH; my $ssh_proxy = Net::OpenSSH->new("$username\@domain-name"); my $proxy_command = $ssh_proxy->make_remote_command({tunnel => 1}, $de +vice, 22); my $ssh = Net::OpenSSH->new("admin\@$device", password => $password, p +roxy_command => $proxy_command); $ssh->system;
    untested, requires a relatively recent version of OpenSSH installed locally and tunnels enabled on the gateway host.
      Hi All, I have tried all options in different senarios and I the script is not taking it. The part of Net::OpenSSH is nice, but the problem part for me here is that I do not own the server and OpenSSH is not an option for me. (just user) I was wondering if there is an option within $exp->interact(); Like when within the interact process if the hostname of the device is detected it will send and additional exit.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://979407]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-07-31 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (255 votes), past polls