Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Net::SSH::Perl - Permission Denied

12 views
Skip to first unread message

Alvin Ramos

unread,
Mar 20, 2013, 4:50:24 PM3/20/13
to begi...@perl.org
I'm wanting to run through each server, but if it gets to a server that it
can't login not to die. The "server-list.txt" has:
localhost
localhost2
localhost3
localhost4

When it gets to localhost3 and it dies with: Permission denied at ./
linux_discovery.pl line 20

I would like it to keep rolling over to localhost4, code below, open for
suggestions and appreciate it!

#!/usr/bin/perl -w
use Net::SSH::Perl;

my $file = "server-list.txt";
my $usr = "root";
my $pwd = "password";
my $output_file = "servers_information.txt";

open(HANDLE, $file) or die("Cant open the file :( ");
@server_list = <HANDLE>;
close(HANDLE);
chomp @server_list;

open(HANDLE, ">>$output_file");
foreach $host (@server_list)
{

my $ssh = Net::SSH::Perl->new($host);
print "Logging into server: $host \n";
$ssh->login($usr,$pwd,$ssh);
my($unameout) = $ssh->cmd("/bin/uname -a");
my($userout) = $ssh->cmd("/bin/cat /etc/passwd");
my($upout) = $ssh->cmd("/usr/bin/uptime");
my($lastout) = $ssh->cmd("/usr/bin/last");
my($ipout) = $ssh->cmd("/sbin/ifconfig");
my($hostout) = $ssh->cmd("/bin/hostname");
print HANDLE "---------------------------------- \n";
print HANDLE "Hostname : $host \n";
print HANDLE "Uptime : $upout \n";
print HANDLE "Kernel : $unameout \n";
print HANDLE "Users : $userout \n";
print HANDLE "Last Login : $lastout \n";
print HANDLE "IP Address : $ipout \n";
print HANDLE "Real Hostname: $hostout \n";
print HANDLE "----------------------------------\n\n";
}

Andy Bach

unread,
Mar 20, 2013, 6:23:45 PM3/20/13
to Alvin Ramos, Perl Beginners
Well, line 20 is:
$ssh->login($usr,$pwd,$ssh);

so if that's the line that's dying you can wrap it in an eval -
see perldoc -f eval
If there is a syntax error or runtime error, or a "die" statement is
executed, "eval" returns an undefined value in scalar context or an empty
list in list context, and $@ is set to the error message.

so you'd test $@ and skip to the next host if the login failed. But but my
perldoc Net::SSH::Perl doesn't seem to say it'd die if the login failed so
there may be more going on. It also says the login() syntax is:
$ssh->login([ $user [, $password [, $suppress_shell ] ] ])


which, in your case, means you're passing in the ssh object in the suppress
shell spot:
By default, Net::SSH::Perl will open a channel with a shell on it. This is
usually what you want. If you are tunneling another protocol over SSH,
however, you may want to prevent this behavior. Passing a true value in
$suppress_shell will prevent the shell channel from being opened (SSH2
only).

But you're doing a number of unsafe/smart things - you die on the first
open, but don't include "$!" to get the reason why. You don't test your
second open or the return codes on any of your ssh->cmd calls
--

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk
0 new messages