switch_conf_backup/switch-backup
2021-01-15 18:44:52 +03:00

139 lines
3.4 KiB
Perl
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/perl
use Net::Telnet ();
use IO::File;
use Getopt::Long;
# мегахак - функция удаления пробельных символов
sub trim {
my($string)=@_;
for ($string) {
s/^\s+//;
s/\s+$//;
}
return $string;
}
# Архивируем конфиги
$date = `/bin/date "+%Y%m%d"`;
$date = trim($date);
$arch = "tar -czvf /var/srv/backup/conf/conf-".$date.".tgz /var/lib/tftpboot/";
my $result = `$arch`;
# удаляем архивы давностью в неделю
$del = '/bin/find /var/srv/backup/conf/ -ctime +7 -name conf-*.tgz -exec rm {} \; -print > /var/log/switch-conf_backup.log';
my $delResult = `$del`;
# Собсвенно сливаем конфиги на фтп
$dirETC = "/usr/local/etc/";
use DBI;
$dirETC = "./";
# конектимся к GLPI базе
my $dsn = 'DBI:mysql:glpi:192.168.123.222';
my $db_user_name = 'user';
my $db_password = 'password';
my ($id, $password);
my $dbh = DBI->connect($dsn, $db_user_name, $db_password);
# выбираем все EdgeCore со статусом "Включен"
my $sth = $dbh->prepare(qq{SELECT ip FROM glpi_networkequipments WHERE manufacturers_id=1 AND states_id=1});
$sth->execute();
while (my ($ip) =
$sth->fetchrow_array())
{
# print "$ip\n";
if ($ip eq "" || $ip =~ /^#+/) {
} else {
connectSwitchEdgeCore("$ip");
}
}
$sth->finish();
# выбираем все D-Link со статусом "Включен"
my $sth = $dbh->prepare(qq{SELECT ip FROM glpi_networkequipments WHERE manufacturers_id=3 AND states_id=1});
$sth->execute();
while (my ($ip) =
$sth->fetchrow_array())
{
# print "$ip\n";
if ($ip eq "" || $ip =~ /^#+/) {
} else {
connectSwitchDlink("$ip");
}
}
$sth->finish();
$dbh->disconnect();
exit;
# telnet-ом цепляемся к коммутаторам и сливаем конфиг на tftp сервер
# слив конфигурации с коммутатора EdgeCore
sub connectSwitchEdgeCore
{
# print "$_[0]\n";
#return;
$hostname = "$_[0]";
$username = "user";
$passwd = "password";
$date = `/bin/date "+%Y%m%d"`;
$str_fn=$hostname;
print $str_fn;
$tftpserver="172.22.22.252";
$conn = new Net::Telnet ( Timeout=>3, Errmode=>'return', Prompt => '/\#.?$/i');
$conn->open(Host => $hostname);
$conn->waitfor('/ame[: ]*$/');
$conn->print($username);
$conn->waitfor('/ord[: ]*$/');
$conn->print($passwd);
$conn->print("copy running-config tftp");
$conn->waitfor('/ess[: ]*$/');
$conn->print($tftpserver);
$conn->waitfor('/ame[: ]*$/');
$conn->print($str_fn);
$conn->print("exit");
$conn->print("logout");
}
# слив конфигурации с коммутатора D-Link
sub connectSwitchDlink
{
# print "$_[0]\n";
#return;
$hostname = "$_[0]";
$username = "user";
$passwd = "password";
$date = `/bin/date "+%Y%m%d"`;
$str_fn=$hostname;
print "$str_fn\n";
$tftpserver="172.22.22.252";
$conn = new Net::Telnet ( Timeout=>3, Errmode=>'return', Prompt => '/\#.?$/i');
$conn->open(Host => $hostname);
$conn->waitfor('/ame[: ]*$/');
$conn->print($username);
$conn->waitfor('/ord[: ]*$/');
$conn->print($passwd);
$conn->print("upload cfg_toTFTP $tftpserver dest_file $hostname");
$conn->waitfor('/Success/');
$conn->print("logout");
}