Thursday, August 9, 2007

[Linux] perl script สำหรับไป show config ของ cisco

perl script สำหรับเก็บ config cisco โดยดึงข้อมูลจาก Database ของ cacti แล้ว telnet ไป show running-config แล้วมาเก็บไว้ จากนั้นก็ทำการ compare กับของเดิมที่มีอยู่โดยใช้ unix diff command ถ้า config มีการเปลี่ยนแปลงให้ส่งเมล์

#!/usr/bin/perl
use Net::Telnet::Cisco;
use DBI();
use File::Copy;
use MIME::Lite;
#$|++;

$diff_cmd ="/usr/bin/diff -b -B -i -w ";
$mail_server = "mail.server.co.th";
$mail_from = 'tacacs@server.co.th';
$mail_to = 'admin@server.co.th';

$username = 'cisco_priv15';
$password = 'cisco_password';

$db_name = 'cacti';
$db_username = 'cacti';
$db_password = 'cacti';

$mail_diff_enable = 1;
$mail_diff_alert = 0;


($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$time=sprintf "%4d-%02d-%02d_%02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;

$backup_path = sprintf "./config-backup/%4d%02d%02d", $year+1900,$mon+1,$mday;
$backup_lastest = "./config-backup/lastest";
$mail_data ="";
$config_change_count = 0;
mkdir($backup_path);

# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=localhost",$db_username,$db_password,{'RaiseError' => 1});
my $sth = $dbh->prepare("SELECT id,description,hostname FROM host WHERE hostname != '127.0.0.1' and disabled != 'on' ");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
#print "Found a row: id = $ref->{'id'}, description = $ref->{'description'}, hostname = $ref->{'hostname'}\n";

$session = ""; # restore to default value
my $session = Net::Telnet::Cisco->new(Host => $ref->{'hostname'}, Errmode => "return");
if($session){
$session->login($username, $password);

$session->cmd('term len 0');
@output = $session->cmd('show running-config');
$num = @output;

if($num > 2 ){
$backup_file = $backup_path."/".$ref->{'description'}."_".$ref->{'hostname'}.".cfg";
$backup_lastest_file = $backup_lastest."/".$ref->{'description'}."_".$ref->{'hostname'}.".cfg";
#print "backup file: $backup_file\n";
#print "backup lastest file: $backup_lastest_file\n";
open(FP,">$backup_file");
for( $i = 3 ; $i < $num ; $i++){ # suppress header file
$cfg = $output[$i];
print FP $cfg;
}
if( -e "$backup_lastest_file"){
$diff = `$diff_cmd $backup_lastest_file $backup_file`;
if($diff){
$mail_data = $mail_data."#==============================================\n";
$mail_data = $mail_data."# Hostname: ".$ref->{'description'}." (".$ref->{'hostname'}.")\n";
$mail_data = $mail_data."#==============================================\n";
$mail_data = $mail_data."$diff\n";
$mail_diff_alert = 1;
$config_change_count++;
#print $mail_data;
}
}
copy($backup_file, $backup_lastest_file);
close(FP);
}


$session->close;
}else{
print "Can't remote telnet to $ref->{'hostname'}\n";
}
}
if($mail_diff_alert && $mail_diff_enable){
$msg = MIME::Lite->new(
From =>"$mail_from",
To =>"$mail_to",
Subject =>"Config has modified $config_change_count host(s) : $time",
Data =>"$mail_data");
$msg->send('smtp', "$mail_server", Timeout=>60);
}

$sth->finish();

$dbh->disconnect();

No comments: