#!/usr/bin/perl use Net::Telnet (); use IO::File; use Getopt::Long; use DBI;
# 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`;
# 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);
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()) { if ($ip eq "" || $ip =~ /^#+/) { } else { connectSwitchDlink("$ip"); } } $sth->finish(); $dbh->disconnect();
# telnet- tftp # EdgeCore sub connectSwitchEdgeCore { $hostname = "$_[0]"; $username = "user"; $passwd = "password"; $date = `/bin/date "+%Y%m%d"`; $str_fn=$hostname; print $str_fn; $tftpserver="20.20.20.20"; $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 { $hostname = "$_[0]"; $username = "user"; $passwd = "password"; $date = `/bin/date "+%Y%m%d"`; $str_fn=$hostname; print "$str_fn\n"; $tftpserver="20.20.20.20"; $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"); }
Source: https://habr.com/ru/post/336850/
All Articles