http://qs321.pair.com?node_id=1209821

vasanth.easyrider has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl monks

I have a requirement where i need to connect to 900 routers and execute few commands and capture the output. I am using Net::OpenSSH module for fulfilling my requirement. But we have ssh key authentication issue that is causing a problem for me. I cant manually connect to 900 servers to establish the SSH key. Is there a method for me to tackle it in script. The script given below is being used to connect to 1 router and execute the command

#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; my ($ssh,$password); my $host = "10.0.0.183"; my $user_name = q(username); $password = q(My_Password); $ssh = Net::OpenSSH->new($host, user => $user_name, password => $passw +ord); my $output = $ssh->system("show isis neighbors") or die "remote command failed: " . $ssh->error; print "output is = $output\n";

Replies are listed 'Best First'.
Re: SSH connection to 100 routers
by thanos1983 (Parson) on Feb 23, 2018 at 11:23 UTC
Re: SSH connection to 100 routers
by karlgoethebier (Abbot) on Feb 24, 2018 at 11:26 UTC
    "...I cant manually connect to 900 servers to establish the SSH key..."

    Probably you can delegate this to a script. What about something like this: sshpass -f secrets.file ssh-copy-id user@host?

    I don't remember the exact syntax. Please see sshpass and ssh-copy-id for details.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: SSH connection to 100 routers
by eighty-one (Curate) on Feb 26, 2018 at 15:06 UTC

    There is a tool called rancid that handles this via expect. The expect man page is actually pretty good and should give you a decent overview of what it can do.

    I think the rancid devs use the shell version of expect via a bash script (I haven't looked in quite a while so I'm not sure).

    CPAN has a few Expect modules. Rex::Helper::SSH2::Expect looks interesting but I have no experience with it.

Re: SSH connection to 100 routers
by Anonymous Monk on Feb 26, 2018 at 14:44 UTC
    Also do not overlook the capabilities of existing infrastructure-maintenance tools such as Nagios.