Solved

NET::SSH2 Terminates Unexpectedly

Posted on 2010-11-22
5
680 Views
Last Modified: 2013-11-05
My goal is to write a Perl script which connects to a remote server using the SSH2 package.  The script runs a command which generates a list of all files and their md5sum.  The problem is occasionally the script will terminate unexpectedly.

 
#!/usr/bin/perl

use Net::SSH2;

$ssh2 = Net::SSH2->new();

$ssh2->connect('asdfasdfas.com') or die $!;
$ssh2->auth_password('user','pass') or die "Unable to login $@ \n";
$ssh2->debug(0);

$chan2 = $ssh2->channel();
$chan2->blocking(0);
$chan2->shell();
print $chan2 "bash ~/md5xml\n";
print "LINE : $_" while <$chan2>;
$chan2->close;

print 'done...';

Open in new window

nothing?john@sporkit.com-> ./ssh_web_sync.pl
LINE : MD5 (./.cshrc) = 7cd795d80cbc7cb24a96828992a4897e
LINE : MD5 (./.login) = 753b51225e21863dd2278ba0753d4bc4
LINE : MD5 (./.mailrc) = d0bbf8bf09a43ffdbc902bcbdc08304f
LINE : MD5 (./.profile) = 5d598ba6fb89ec78c77128b560ce4d78
LINE : MD5 (./.rhosts) = 8487c7b9ad8bde81f31377610ff7397d
LINE : MD5 (./.mail/new/1288302002.13058_2.brunner.asdffdsa.com) = 8dc6c40e37b91ebe4eb4c22b0c42aaa2

Open in new window


Most of the time I will get about 50 results back, but sometimes I'll get as little as 6 lines.  The remote script being called is a simple find and exec command.

find ./ -type f -exec md5 {} \;

My goal is to grab an md5 list of files on the remote machine so I can compare is with a local list.

Thanks

I've also tried this with NET:SSH and NET:OPENSSH, both of which failed to even open a connection.
0
Comment
Question by:sporkit150
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:jmatix
ID: 34194860
Try changing:

print "LINE : $_" while <$chan2>;

To:

<$chan2>, print "LINE : $_" while !eof($chan2);

0
 

Author Comment

by:sporkit150
ID: 34197664
Now the script appears to hang for a minute or two then dumps this code every two minutes or so.
john@sporkit.com-> ./ssh_web_sync.pl
LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LIN

Open in new window

I added an exit command to jump out of the shell when run and that eventually brings me to the "done..." print statement at the end.  It appears as if were close but $_ might not be getting the data we need from the buffer.
 
$chan2->shell();
print $chan2 "bash ~/md5xml\n";
print $chan2 "exit\n";
<$chan2>, print "LINE : $_" while !eof($chan2);
$chan2->close;
print 'done...';

Open in new window

john@sporkit.com-> ./ssh_web_sync.pl
LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LINE : LI......................E : LINE : LINE : done...

Open in new window

0
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 34197977
Assign it to a variable and print as:

$l=<$chan2>, print "LINE : $l" while !eof($chan2);
0
 

Author Comment

by:sporkit150
ID: 34198113
FINALLY... I think that did the trick.  I had to remove the LINE statement statement as it was adding extra returns while printing.  Here is my final output.

 
#!/usr/bin/perl

use Net::SSH2;

$ssh2 = Net::SSH2->new();
$ssh2->connect('asdfasdf.com') or die $!;
$ssh2->auth_password('asdfasdf','asdfasdf') or die "Unable to login $@ \n";
$ssh2->debug(0);

$chan2 = $ssh2->channel();
$chan2->blocking(0);
$chan2->shell();
print $chan2 "bash ~/md5xml\n";
print $chan2 "exit\n";
$l=<$chan2>, print "$l" while !eof($chan2);
$chan2->close;

print 'done...';

Open in new window


Could you also explain how this print statement is working?  It says something like, while not end of file from channel2 print "LINE: $l".  What is up with the comma though?  Could you write it like...

 
$l=<$chan2>;
print "LINE : $l" while !eof($chan2);

Open in new window

0
 
LVL 16

Expert Comment

by:jmatix
ID: 34198338
No. Because reading also should inside the while loop. Otherwise it will always print one value and the loop will never end since it never reads again after the first read. That is why it should be:

$l=<$chan2>, print "$l" while !eof($chan2);

OR

while (!eof($chan2))
{
   $l = <$chan2>;
   print "$l";
}

OR just

print <$chan2> while !eof($chan2);

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
We all know how boring and exhausting it is to transfer huge web projects developed locally to a webserver simply via FTP. The File Transfer Protocol is a really nice solution if you need to transfer small amounts of files, but if you're plannin…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now