Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Wednesday, December 7, 2011

Perl SSH Log-in

Hi Folks, 

In the following post I will demonstrate a password-less login into Cisco appliance via the SSH protocol using a Perl script.

When combined with cron, it can be a great solution for saving your appliance configuration (show run) or checking status without the need to log-in into the appliance each time.



Pre requirements:
  • Perl
  • SSH client (duh!)
  • Net-SSH-Perl module

On Red-Hat (and friends) the module can be obtained via:
#yum install perl-Net-SSH-Perl -y 

Be sure to have a proper repository installed such as rpmforge 
 
Our simple script connects to the Cisco appliance via SSH and runs "show run" command (this account has enabled privilege).
The script itself should look like this:

#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new('hostname');
$ssh->login('username', 'password');
my($out) = $ssh->cmd("show run");
print $out;

Since the script contains your appliance username & password don't forget to remove permissions for others:
#chmod o-rwx script.pl

Now, let's run the script:
#./script.pl

Output:
Building configuration...

Current configuration : 8011 bytes
...more output ommited...


Works like charm!
Now, the only thing is left is to synchronize it with cron :)

Enjoy.

Friday, February 19, 2010

Generate hosts file with Perl

A sweet way to add couple of servers to your hosts file/NIS map using a tiny perl script:

#!/usr/bin/perl -w

use strict;
use warnings;
print `clear`;
my $j=10;

open (HOSTS ,">>/etc/hosts.txt") or die $!;
for (my $num=0;$num <= 10; $num++) {
printf(HOSTS "server$j \t server$j.domain.org \t 192.168.0.$num\n");
$j++;
}
close (HOSTS) or die $! ;

#END


The output will be:

server10 server10.domain.org 192.168.0.0

server11 server11.domain.org 192.168.0.1
server12 server12.domain.org 192.168.0.2
server13 server13.domain.org 192.168.0.3
server14 server14.domain.org 192.168.0.4
server15 server15.domain.org 192.168.0.5
server16 server16.domain.org 192.168.0.6
server17 server17.domain.org 192.168.0.7
server18 server18.domain.org 192.168.0.8
server19 server19.domain.org 192.168.0.9
server20 server20.domain.org 192.168.0.10