ImJustAFriend has asked for the wisdom of the Perl Monks concerning the following question:

Venerable monks, I come seeking wisdom of a middling nature. I am attempting to create a script to automate a very tedious set of manual steps done through an SSH connection to a RHEL machine. I am developing on a Windows laptop running Windows 7 and ActiveState Perl 5.16.1 Build 1601. I am in the first stages, where I am attempting to use a test script to learn the ins and outs of Net::SSH2. I can get my script to login to the machine. I can get it to do things like "ls" and print the output. Where I am getting stuck is on 3 parts: (1) the first command I run, no matter what it is, doesn't seem to run - nothing prints on my command window; (2) running a command (like another Perl script) that produces output - capturing that output and processing it further; and (3) when I try #2, my whole script blows up (see output). One more oddity is that I am getting "Term type is now linux" in my second channel run output. My code follows:

#!C:/Perl/bin/perl.exe use strict; use warnings; use Net::SSH2; my $BE_conn; my $BE_ip; my $BE_chan; my $out; $BE_ip="nnn.n.nn.nnn"; $BE_conn = Net::SSH2->new(); $BE_conn->debug(0); $BE_conn->connect($BE_ip) or die "BE_conn: $!\n"; $BE_conn->auth_password('user','pass') or die "Unable to login to BE\n +"; $BE_chan = $BE_conn->channel(); $BE_chan->blocking(0); $BE_chan->shell(); #Next 2 lines needed or $BE_chan only gets "Term type is now linux +" #Note that NOTHING prints preceded by "CHAN LINE" to my window print $BE_chan "\nwho am i\n"; print "CHAN LINE: $_" while <$BE_chan>; #Run a simple command, test process the output with regex print $BE_chan "\nls /u/ainet\n"; while ( <$BE_chan> ) { $out .= $_; } print "DEBUG: \$out = $out\n"; if ( $out =~ m/^.*tar.*$/m ) { print "Found \"tar\" files in \$out...\n"; } $out = ""; #Run second simple command to ensure $out works still print $BE_chan "\nls /opt/config/servers/0-0-1/local/root/etc/sysc +onfig/network-scripts\n"; while ( <$BE_chan> ) { $out .= $_; } print "DEBUG: \$out = $out\n"; if ( $out =~ m/^.*netba.*$/m ) { print "Found \"netba\" files in \$out...\n"; } $out = ""; #Run a script, capture the output, regex it #print $BE_chan "\n/u/ainet/BatchPing.pl pinglist tcp\n"; #while ( <$BE_chan> ) { $out .= $_; } #print "DEBUG: \$out = $out\n"; #if ( $out =~ m/^.*NOT.*$/m ) { # print "Found \"NOT\" lines in \$out...\n"; #} #$out = ""; $BE_chan->close;

When I run my script with the last section commented, I see these results (file names redacted - this is business related):

C:\Users\ImJustAFriend\Documents\Perl>LDAP-Config.pl DEBUG: $out = Term type is now linux script1.pl application script2.pl file1.tar backup1.tar backup2.tar backup3.tar pinglist Found "tar" files in $out... DEBUG: $out = ifcfg-if01 ifcfg-if02 ifcfg-if03 ifcfg-if04 ifcfg-if05 ifcfg-if06 ifcfg-if07 ifcfg-if08 ifcfg-if09 ifcfg-if10 ifcfg-if11 ifcfg-if12 ifcfg-if13 ifcfg-if14 ifcfg-if15 ifcfg-if16 ifcfg-if17 ifcfg-if18 ifcfg-if19 ifcfg-if20 ifcfg-if21 ifcfg-if22 ifcfg-if23 ifcfg-if24 ifcfg-if25 ifcfg-if26 ifcfg-if27 ifcfg-if28 route-if29 route-if30 route-if31 Found "netba" files in $out...

When I run my script with the last section not commented, I get this output instead:

C:\Users\ImJustAFriend\Documents\Perl>LDAP-Config.pl DEBUG: $out = Term type is now linux DEBUG: $out = script1.pl application script2.pl file1.tar backup1.tar backup2.tar backup3.tar pinglist ifcfg-if01 ifcfg-if02 ifcfg-if03 ifcfg-if04 ifcfg-if05 ifcfg-if06 ifcfg-if07 ifcfg-if08 ifcfg-if09 ifcfg-if10 ifcfg-if11 ifcfg-if12 ifcfg-if13 ifcfg-if14 ifcfg-if15 ifcfg-if16 ifcfg-if17 ifcfg-if18 ifcfg-if19 ifcfg-if20 ifcfg-if21 ifcfg-if22 ifcfg-if23 ifcfg-if24 ifcfg-if25 ifcfg-if26 ifcfg-if27 ifcfg-if28 route-if29 route-if30 route-if31

Can someone please help me see the error of my ways? I tried the application example in the Net::SSH2 documentation (semi-daemonizing) but that didn't work either...

Thanks in advance!!
ImJustAFriend

Replies are listed 'Best First'.
Re: Net::SSH2 Help Request
by jellisii2 (Hermit) on Nov 06, 2013 at 12:48 UTC
    Look at Control::CLI. It handles a LOT of the ugly channel switching and other underpinnings nicely.

      THANK YOU for that, jellisii2!!! That module is AWESOME and let's me do precidely what I need to do!!