Re: [Ssh-sftp-perl-users] Problems with using Net::SSH::Perl/Net::SFTP
Brought to you by:
dbrobins
From: Frank C. <car...@ld...> - 2006-11-16 21:10:02
|
All, here is a sample bit of code I've written in testing how to ssh from one server to a remote system. It's not the cleanest bit of code I've written however it works. Basically I was desperate in finding a solution in grabbing /etc/shadow and /etc/sudoers for our auditing department (long story). Using the poll and read methods for some reason wasn't working properly. I've had other perl coders here check my work and no soap. They were getting the same results I'm experiencing. I left those sections I was messing around with so you can see how I was trying make it work. This bit of sample code only shows the basics of connecting, running commands and printing the results to the screen. Usually I grab the results to a filehandle locally opened. BTW, the first attempt at reading /etc/shadow doesn't seem to provide any results in the channel however it seems once I've sent the password to sudo I'm able to capture it on the second attempt. I think this means we're somehow missing the output on the first try and since we're still connected and sudo has been satisfied we can capture it on the second attempt. If anyone has any idea how to use the poll method I would appreciate a hint or two. I need to setup a process that watches STDERR and run's a subroutine if STDERR traffic is detected (similar to register_handler found in the old module). I've invested a great deal of time reading through perldoc and various online references. So far nothing seems to be helping me figure this out. I've lightly tested the below code. Use at your own risk. Enjoy! P.S. I don't see the ssh-sftp-perl-users forums webpage updating over the last couple of months. The last update was September 29th. -------- #!/usr/bin/perl use Net::SSH2; use Term::InKey; #use diagnostics; use strict; use warnings; my $server = server_name_here; my $login= login_name_here; my $timeout = 100; my ($password,$confirmpasswd); my ($code,$errname,$errstr,$output); my $debug = 0; &Clear; print Type account password for $login: ; $password = &ReadPassword; print Type again to confirm password: ; $confirmpasswd = &ReadPassword; die \n\n ** Exiting ... Need a password \n\n unless ($password eq $confirmpasswd); # Create the ssh2 object my $ssh2 = Net::SSH2->new(); # Create my STDERR trigger - this isn't working yet! # my $self = shift; # my @poll = ({ handle => $self, events => 'ext' }); # my $subroutine = $ssh2->poll($timeout, \@poll); # connect to remote server $ssh2->connect($server) or die ** Failed to connect via Net::SSH2: $!\n; print ** Connect to server $server successful\n\n; # Authenticate to remote system. Works with SLESS9 (PasswordAuthentication no) $ssh2->auth_keyboard($login,$password) or die \n\n** Exiting ... Failed to login to server $server\n\n; # Authenticate to remote system. Works with SLES8 (PasswordAuthentication yes) # $ssh2->auth_password($login,$password) or die \n\n** Exiting ... Failed to login to server $server\n\n; # Open a channel and create a shell first. my $chan = $ssh2->channel(); $chan->shell(); # Execute a command print \n** Entering the Execute Command routine \n\n if $debug =~ 1; my $cmd = 'ls -l /'; $chan = $ssh2->channel(); $chan->exec($cmd); print $chan sudo cat /etc/shadow \n; print $chan $password . \n; print \n** Exiting the Execute Command routine \n\n if $debug =~ 1; # Read the channel - this isn't working yet! # print \n** Entering the Read routine \n\n if $debug =~ 1; # my $buffer; # $chan->read($buffer, 1024, '1'); # print \n** Exiting the Read routine \n\n if $debug =~ 1; # Print the results. print \n** Entering the print routine\n\n if $debug =~ 1; print \n** Command results\n; print $_ while <$chan>; print \n** Exiting the print routine\n\n if $debug =~ 1; # STDERR routine print \n\n** Entering STDERR routine \n\n if $debug =~ 1; $chan = $ssh2->channel(); $chan->exec('sudo cat /etc/shadow'); print $chan $password . \n; print \n** /etc/shadow results\n; print $_ while <$chan>; print \n\n** Exiting STDERR routine \n\n if $debug =~ 1; $chan->close; ---------------------------------------------------------------------- NOTICE: This email message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. |