[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_filen”;
#print “backup lastest file: $backup_lastest_filen”;
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.”$diffn”;
$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();

[MySQL] Mysql replicate DB

mysql สามารถ replicate DB ได้ A<=====>B
Server A:
vi /etc/my.cnf
[mysqld]
log-bin
server-id=1
master-host=192.168.1.1
master-user=replicate
master-password=replicatepass1
replicate-do-db=DB
slave-skip-errors=1050 <<< skip error table exist Server B:
vi /etc/my.cnf
[mysqld]
log-bin
server-id=2
master-host=192.168.1.2
master-user=replicate
master-password=replicatepass1
replicate-do-db=DB
slave-skip-errors=1050
ทั้ง 2 server ต้องมี tables เหมือนกันโดยจะต้อง create database ก่อน และจะต้อง grant สิทธ์ของ user สามารถ update ข้อมูลกันได้ mysql>create database DB;

mysql>user DB;
mysql>create table ‘test’ (
`id` int(10) NOT NULL auto_increment,
‘name’ varchar(32) default ”,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

mysql>load data from master;

command ต่างๆ
mysql>show slave statusG;
mysql>show master statusG;
mysql>start slave;
mysql>stop slave;
mysql>reset slave;