Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

SSH connection to 100 routers

by vasanth.easyrider (Acolyte)
on Feb 23, 2018 at 11:13 UTC ( #1209821=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1209821]
Approved by Sinistral
Front-paged by Sinistral
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-12-03 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (28 votes). Check out past polls.

    Notices?