1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use feature 'say';
6
7use Net::OpenSSH::Parallel;
8use Getopt::Long;
9
10my ($login, $password, $file, $verbose);
11my $retries = 1;
12my $timeout = 10;
13GetOptions( "login|l=s"               => \$login,
14            "password|passwd|pwd|p=s" => \$password,
15            "file|f=s"                => \$file,
16            "retries|r=i"             => \$retries,
17            "timeout|t=i"             => \$timeout,
18            "verbose|v"               => \$verbose);
19
20my @hosts = @ARGV;
21
22# read hosts from file when "file" option is given.
23if (defined $file) {
24    open my $fh, '<', $file or die "unable to open $file: $!";
25    while (<$fh>) {
26        next if /^\s*(?:#.*)$/;
27        chomp;
28        push @hosts, $_;
29    }
30    close $fh or die "unable to read $file: $!";
31}
32
33my $pssh = Net::OpenSSH::Parallel->new;
34$pssh->add_host($_, user => $login, password => $password,
35                reconnections => $retries,
36                master_stderr_discard => 1,
37                master_opts => ["-oConnectTimeout=$timeout"]) for @hosts;
38$pssh->push('*', 'connect');
39$pssh->run;
40
41for (@hosts) {
42    my $error = $pssh->get_error($_);
43    if ($error) {
44        say "Connection to host $_ failed: $error" if $verbose;
45        next;
46    }
47    say "Connection to host $_ succeeded!";
48}
49