Saturday, April 3, 2010

Mikrotik Backup through perl script

I don't know how many people are still using telnet but suppose if you are using this will help. Script uses export command to take the backup and save it. I thought better to share it. I'll check the ssh variation and upload when possible.


#Date - 31st April 2010
#Gobinath
#To retrive the backup from the mikrotik router.
#!/usr/bin/perl
use Net::Telnet;
use POSIX ;

#Input
# routerlist contains ipaddress and name that save the backupfile seprated by tab # used for comments.
#Backup Path.
$path = "/mikrotik/backups";
#router List
$filename = "routerlist.txt";

#Mikrotik Username Password +ct used to remove the color codes
@username_list = ('admin+ct','admin');
@password_list = ('######','######');
#Read the router List
open (FILE,$filename) ;

#Time Stamp.
my $datestamp = strftime("%Y%m%d",localtime);
chomp($datestamp);



sub retriver {
undef @output ;
my($username, $pass,$hostname) = @_;
$telnet = new Net::Telnet ( Timeout,5, Errmode=>'return');
$telnet->binmode(0);
$telnet->open($hostname);
$telnet->login($username,$pass);
$msg = $telnet->errmsg;
if ($msg =~ /ogin failed/ )
{
$output[0] = "failed";
return @output;
}
else
{
#Retrive the export .
$telnet->waitfor('/] >/i');
@output = $telnet->cmd('/export');
return @output;
}
}


while ()
{
if (lcfirst $_ != '#')
{
undef @output ;
my($hostname,$filename)= split('\t', $_);
chomp($filename);
my $count = 0;
foreach(@username_list)
{

my $username = $_;
my $pass = $password_list[$count];
@output = &retriver($username,$pass,$hostname);
if ( $output[0] !~ /failed/ )
{

$filepath = $path."/".$filename."_".$datestamp.".txt";
open(OUT,">$filepath");
print OUT @output;
last;
}
$count = $count +1;
}
}
}

close(FILE);


issue noted with mikrotik version 3.0 and related OS. still couldn't fix the problem.

No comments: