Конфиг сваливается весь в одну длинющую строку
Код:
#!/usr/bin/perl
use Net::Telnet;
use DBI;
my $ip;
my $name = 'adm';
my $pass = '12345678';
my $qry = ("SELECT ip FROM computers WHERE unit_id='76' ORDER BY ip");
my $dbh = DBI -> connect ("DBI:Pg:dbname=info user=info_bckp_switch host=10.0.0.4 password=qwerty");
my $sth = $dbh -> prepare($qry);
my $rv = $sth -> execute();
if (!defined $rv) {
print "ERROR on while execute '$qry': " . $dbh->errstr . "n";
exit(0);
}
###################################################################################################
while (my @row = $sth -> fetchrow_array())
{
$ip = $row[0]; #get ip switch
foreach $ip(@row)
{
my $ping = "ping -c 1 -q $ip";
my @lines = `$ping`;
for my $line (@lines)
{
if ($line =~ /s+(d+)% packet loss/)
{
if ($1 eq '100')
{
print "$ip - FAIL!n"
}
else
{
print "$ip - OK!, then backup config $ip to continue...n";
&getconf();
}
}
}
}
}
#########################################################################################################
sub getconf {
$file_name = $number = substr($ip,7,10); #strip ip address
$file_name =~ s/$number/switch$number.cfg/; #rename config file
system "unlink /var/tftp/$file_name";
system "touch /var/tftp/$file_name";
system "chmod 0666 /var/tftp/$file_name";
$telnet = new Net::Telnet (Timeout=>50,Prompt=>'/#/'); #make object net
$telnet -> open($ip); #connect to switch
$telnet -> login($name, $pass); #enter to switch
$telnet -> cmd('dis cli'); #for full output
$cmd1="show switch";
$cmd2="upload cfg_toTFTP 10.0.107.45 $file_name";
$cmd3="upload cfg_toTFTP 10.0.107.45 dest_file $file_name"; #for HardVer C1
@lines = $telnet -> cmd($cmd1);
chomp (@lines);
foreach $line (@lines)
{
($val) = $line =~ /Hardware Versions+:s+([A-Z0-9]+)/;
next unless $val;
if ($val eq "C1")
{
$telnet -> cmd($cmd3);
}
else
{
$telnet -> cmd($cmd2);
}
}
$telnet -> print('$cmd');
$telnet -> waitfor('/#/');
$telnet -> print ('logout');
$telnet -> close;
open(IF, "< /var/tftp/$file_name") or die $!;
open(OF, "> /var/tftp/$file_name.tmp") or die $!;
while(<IF>)
{
chomp;
s/create account admin sa//g;
s/11388019//g;
print OF $_;
}
close (OF);
close (IF);
rename "/var/tftp/$file_name", "/var/tftp/$file_name.bkp";
rename "/var/tftp/$file_name.tmp", "/var/tftp/$file_name";
unlink "/var/tftp/$file_name.bkp";
$date = `date +%d-%m-%Y--%H-%M`;
open(IF, ">> /var/tftp/$file_name") or die $!;
print IF "n#$date";
close (IF);
system "chmod 0666 /var/tftp/$file_name";
}
$sth -> finish();
$dbh -> disconnect();
use Net::Telnet;
use DBI;
my $ip;
my $name = 'adm';
my $pass = '12345678';
my $qry = ("SELECT ip FROM computers WHERE unit_id='76' ORDER BY ip");
my $dbh = DBI -> connect ("DBI:Pg:dbname=info user=info_bckp_switch host=10.0.0.4 password=qwerty");
my $sth = $dbh -> prepare($qry);
my $rv = $sth -> execute();
if (!defined $rv) {
print "ERROR on while execute '$qry': " . $dbh->errstr . "n";
exit(0);
}
###################################################################################################
while (my @row = $sth -> fetchrow_array())
{
$ip = $row[0]; #get ip switch
foreach $ip(@row)
{
my $ping = "ping -c 1 -q $ip";
my @lines = `$ping`;
for my $line (@lines)
{
if ($line =~ /s+(d+)% packet loss/)
{
if ($1 eq '100')
{
print "$ip - FAIL!n"
}
else
{
print "$ip - OK!, then backup config $ip to continue...n";
&getconf();
}
}
}
}
}
#########################################################################################################
sub getconf {
$file_name = $number = substr($ip,7,10); #strip ip address
$file_name =~ s/$number/switch$number.cfg/; #rename config file
system "unlink /var/tftp/$file_name";
system "touch /var/tftp/$file_name";
system "chmod 0666 /var/tftp/$file_name";
$telnet = new Net::Telnet (Timeout=>50,Prompt=>'/#/'); #make object net
$telnet -> open($ip); #connect to switch
$telnet -> login($name, $pass); #enter to switch
$telnet -> cmd('dis cli'); #for full output
$cmd1="show switch";
$cmd2="upload cfg_toTFTP 10.0.107.45 $file_name";
$cmd3="upload cfg_toTFTP 10.0.107.45 dest_file $file_name"; #for HardVer C1
@lines = $telnet -> cmd($cmd1);
chomp (@lines);
foreach $line (@lines)
{
($val) = $line =~ /Hardware Versions+:s+([A-Z0-9]+)/;
next unless $val;
if ($val eq "C1")
{
$telnet -> cmd($cmd3);
}
else
{
$telnet -> cmd($cmd2);
}
}
$telnet -> print('$cmd');
$telnet -> waitfor('/#/');
$telnet -> print ('logout');
$telnet -> close;
open(IF, "< /var/tftp/$file_name") or die $!;
open(OF, "> /var/tftp/$file_name.tmp") or die $!;
while(<IF>)
{
chomp;
s/create account admin sa//g;
s/11388019//g;
print OF $_;
}
close (OF);
close (IF);
rename "/var/tftp/$file_name", "/var/tftp/$file_name.bkp";
rename "/var/tftp/$file_name.tmp", "/var/tftp/$file_name";
unlink "/var/tftp/$file_name.bkp";
$date = `date +%d-%m-%Y--%H-%M`;
open(IF, ">> /var/tftp/$file_name") or die $!;
print IF "n#$date";
close (IF);
system "chmod 0666 /var/tftp/$file_name";
}
$sth -> finish();
$dbh -> disconnect();
а в логах свитчей обнаруживается около 170 подключений этим скриптом, вместо одного.
Помогите пожалуйста найти ошибки!
Приветствуются любые замечания и советы по улучшению!
Код:
@lines = $telnet -> cmd($cmd1);
chomp (@lines);
foreach $line (@lines)
chomp (@lines);
foreach $line (@lines)
Код:
while(<IF>)
{
chomp;
s/create account admin sa//g;
{
chomp;
s/create account admin sa//g;
Код:
SELECT ip FROM computers WHERE unit_id='76' ORDER BY ip
Еще заметил, в конце запроса нет "точки с запятой", но насколько я помню, в mysql она необходима.
Запрос возвращает, как и ожидается cписок ip.
В конце запроса "точки с запятой" и не нужно. Это код, а не консоль.
Маленькое уточнение - база из которой берутся сведения Postgres.
Код:
while(<IF>)
{
#chomp;
s/create account admin sa//g;
{
#chomp;
s/create account admin sa//g;
Но проблема с 170 подключениями пока остаётся...
Код:
if ($1 eq '100')
{
print "$ip - FAIL!n"
}
else
{
print "$ip - FAIL!n"
}
else
СПАСИБО!
Код:
my @lines = `$ping`;
for my $line (@lines)
{
if ($line =~ /s+(d+)% packet loss/)
{
if ($1 eq '100')
{
print "$ip - FAIL!n"
}
else
{
print "$ip - OK!, then backup config $ip to continue...n";
&getconf();
}
}
}
for my $line (@lines)
{
if ($line =~ /s+(d+)% packet loss/)
{
if ($1 eq '100')
{
print "$ip - FAIL!n"
}
else
{
print "$ip - OK!, then backup config $ip to continue...n";
&getconf();
}
}
}
Код:
my $lines = `$ping`;
if ($lines =~ /(100% packet loss)/m)
{
print "$ip - FAIL!n"
}
else
{
print "$ip - OK!, then backup config $ip to continue...n";
&getconf();
}
if ($lines =~ /(100% packet loss)/m)
{
print "$ip - FAIL!n"
}
else
{
print "$ip - OK!, then backup config $ip to continue...n";
&getconf();
}
Спасибо большое за советы! Сегодня поправлю! Если ещё какие будут ремарки пишите, буду рад!
Код:
my $lines = `$ping`;
if ($lines =~ /(100% packet loss)/m)
{
print "$ip - FAIL!n"
}
else
{
print "$ip - OK!, then backup config $ip to continue...n";
&getconf();
}
if ($lines =~ /(100% packet loss)/m)
{
print "$ip - FAIL!n"
}
else
{
print "$ip - OK!, then backup config $ip to continue...n";
&getconf();
}
Код:
#!/usr/bin/perl
my $ip = "192.168.1.10";
my $ping = "ping -c 1 -W 1 -q $ip";
my $lines = `$ping`;
#print @lines;
if ($lines =~ /(100% packet loss)/m)
{
print "$ip - FAIL!/n";
}else{
print "$ip - OK!, then backup config $ip to continue.../n";
}
my $ip = "192.168.1.10";
my $ping = "ping -c 1 -W 1 -q $ip";
my $lines = `$ping`;
#print @lines;
if ($lines =~ /(100% packet loss)/m)
{
print "$ip - FAIL!/n";
}else{
print "$ip - OK!, then backup config $ip to continue.../n";
}