This is my first time working with Net::OpenSSH, and I am attempting to create an SSH for loop, where I print the hostname of the server I am on using whoami
. I am ale to SSH to my intended servers from the command line, but when I run my script, I encounter the following error...
ssh: could not resolve hostname
: Name or service not known
username
To get more information, I printed the the output of $ssh->error
, where $ssh
is my Net::OpenSSH object.
unable to establish master SSH connection: bad password or master process exited unexpectedly at line 24, $fh line 1
This is the script so far...
#!/usr/bin/perl
use strict;
use warnings;
use Net::OpenSSH;
my $file = 'servers.txt';
open (my $fh, $file) or die "Could not open";
while (my $row = <$fh>) {
my $user = "username";
my $password = "password";
my $ssh = Net::OpenSSH->new(host => $row, user => $user, password => $password);
$ssh-error and die "SSH connection failed: " . $ssh->error;
my $whoami = `whoami`;
print $whoami;
}
The servers.txt file is a plain text file with a list of the servers hostnames, one after another.
My understanding is that if I can SSH to these servers from the command line, the problem must lie in the script, and not the servers/network. Any insight into why I Net::OpenSSH cannot resolve a seemingly accessible server would be appreciated.
"the problem must lie in the script" Yes, there are two problems with the script that I executed. The first one is a syntax error. Other commenters have noted it.
The line
$ssh-error and die "SSH connection failed: " . $ssh->error;
should be
$ssh->error and die "SSH connection failed: " . $ssh->error;
The other problem, which is coming from a version of the script that you haven't shown yet, and which was also noted by others, is that you didn't remove the trailing newline from $row. You do that with
chomp($row)
Do that as the opening instruction of the while block and you should be good to go.
I have to stress the importance of being extra careful in posting MCVs. Before you press that submit button, copy the code in your post to a temp file and execute it, and you will get fewer complaints from responders.
By the way, a good debugging habit that might have let you solve the problem on your own is to have added this line before executing Net::OpenSSH->new
print STDERR ";$row;\n" if $debugging; # or something to this effect
If you had done this, you would have seen something like this on STDERR
;myhost
;
Additionally, the module you're using has a decent debugging feature mentioned in its pod. The debugging output is not so easy to interpret but if you have no idea where the problem is, then you might start there. If you had added this line
$Net::OpenSSH::debug = 8;
Then you would have seen this on STDERR
$ cat servers.txt
localhostx
$ perl x.pl
...
ssh: Could not resolve hostname localhostx
: nodename nor servname provided, or not known
which would have looked like this if you had called chomp but the hostname truly did not exist
ssh: Could not resolve hostname localhostx: nodename nor servname provided, or not known