скрипт снятия конфигов работает неправильно
Код:
use Net::Telnet;
use Pg;
$DB = Pg::connectdb("host=10.0.0.4 dbname=info user=info_bckp password=ofra");
$qry = "SELECT ip FROM computers WHERE unit_id='76'";
#$qry = "SELECT ip FROM computers WHERE name='switch150'";
$res = $DB -> exec($qry);
while (@row = $res -> fetchrow)
{
$ip = $row[0]; #get ip switch
$telnet = new Net::Telnet (Timeout=>25,Errmode=>'die',Prompt=>'/#/'); #make object net
$telnet -> open(Host=>$ip); #connect to switch
$telnet -> login('name', '1234'); #enter to switch
$file_name = $number = substr($ip,7,10); #strip ip address
$file_name =~ s/$number/switch$number.cfg/; #rename config file
if (-e "/var/www/webhome/local.net/d-link/$file_name")
{
unlink ("var/www/webhome/local.net/d-link/$file_name");
}
else
{
system "touch /var/www/webhome/local.net/d-link/$file_name";
system "chmod 0666 /var/www/webhome/local.net/d-link/$file_name";
}
$cmd="upload cfg_toTFTP 10.0.0.1 d-link/$file_name";
$telnet -> cmd(String=>$cmd);
@lines = $telnet -> cmd(String=>$cmd);
print "@lines\n";
$telnet -> print('$cmd');
$telnet -> waitfor('/#/');
$telnet -> print ('logout');
$telnet -> close;
open(IF, "< /var/www/webhome/local.net/d-link/$file_name") or die $!;
open(OF, "> /var/www/webhome/local.net/d-link/$file_name.tmp")or die $!;
while(<IF>)
{
chomp;
s/create account admin name//g;
s/1234//g;
print OF $_;
}
close (OF);
close (IF);
rename "/var/www/webhome/local.net/d-link/$file_name", "/var/www/webhome/local.net/d-link/$file_name.bkp";
rename "/var/www/webhome/local.net/d-link/$file_name.tmp", "/var/www/webhome/local.net/d-link/$file_name";
unlink "/var/www/webhome/local.net/d-link/$file_name.bkp";
$date = `date +%d-%m-%Y--%H-%M`;
open(IF, ">> /var/www/webhome/local.net/d-link/$file_name") or die $!;
print IF "\n#$date";
close (IF);
system "chmod 0666 /var/www/webhome/local.net/d-link/$file_name";
}
__END__
use Pg;
$DB = Pg::connectdb("host=10.0.0.4 dbname=info user=info_bckp password=ofra");
$qry = "SELECT ip FROM computers WHERE unit_id='76'";
#$qry = "SELECT ip FROM computers WHERE name='switch150'";
$res = $DB -> exec($qry);
while (@row = $res -> fetchrow)
{
$ip = $row[0]; #get ip switch
$telnet = new Net::Telnet (Timeout=>25,Errmode=>'die',Prompt=>'/#/'); #make object net
$telnet -> open(Host=>$ip); #connect to switch
$telnet -> login('name', '1234'); #enter to switch
$file_name = $number = substr($ip,7,10); #strip ip address
$file_name =~ s/$number/switch$number.cfg/; #rename config file
if (-e "/var/www/webhome/local.net/d-link/$file_name")
{
unlink ("var/www/webhome/local.net/d-link/$file_name");
}
else
{
system "touch /var/www/webhome/local.net/d-link/$file_name";
system "chmod 0666 /var/www/webhome/local.net/d-link/$file_name";
}
$cmd="upload cfg_toTFTP 10.0.0.1 d-link/$file_name";
$telnet -> cmd(String=>$cmd);
@lines = $telnet -> cmd(String=>$cmd);
print "@lines\n";
$telnet -> print('$cmd');
$telnet -> waitfor('/#/');
$telnet -> print ('logout');
$telnet -> close;
open(IF, "< /var/www/webhome/local.net/d-link/$file_name") or die $!;
open(OF, "> /var/www/webhome/local.net/d-link/$file_name.tmp")or die $!;
while(<IF>)
{
chomp;
s/create account admin name//g;
s/1234//g;
print OF $_;
}
close (OF);
close (IF);
rename "/var/www/webhome/local.net/d-link/$file_name", "/var/www/webhome/local.net/d-link/$file_name.bkp";
rename "/var/www/webhome/local.net/d-link/$file_name.tmp", "/var/www/webhome/local.net/d-link/$file_name";
unlink "/var/www/webhome/local.net/d-link/$file_name.bkp";
$date = `date +%d-%m-%Y--%H-%M`;
open(IF, ">> /var/www/webhome/local.net/d-link/$file_name") or die $!;
print IF "\n#$date";
close (IF);
system "chmod 0666 /var/www/webhome/local.net/d-link/$file_name";
}
__END__
,
т.е. на строчке
$telnet -> login('name', '1234')
В чём может быть дело?
The methods login() and cmd() use the prompt setting in the object to determine when a login or remote command is complete. Those methods will fail with a time-out if you don't set the prompt correctly.
Вообще, прочитай про параметр prompt в документации.
Хорошо, тогда почему первая итерация цикла проходит всегда?
Потому что первый свич всегда выдаёт нормальный prompt?
Проверено на отдельных экземплярах - работает (и на 3526 и на 3200), в цикле - только с одним, первым, а потом указанное сообщение.