<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1400" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Vivan,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> tenho um script que uso bastante
para telnet em roteadores cisco. Acho que pode te ajudar. </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> Não sou um expert em Perl, e sou
novo na lista... Gostaria de receber comentários sobre as funções. </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> Utilizei o expect para verificar
as respostas, mas estou implementando coisa melhor.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>#!/usr/bin/perl<BR>#<BR># Versao : 1.0
</FONT></DIV>
<DIV><FONT face="Courier New" size=2># Arquivo: telnet.pl<BR>#<BR># O formato
para uso deste script e:<BR># telnet.pl <Listagem_dos_hosts>
<Arquivo_de_Comandos> [Arquivo_Log]<BR># <> - Obrigatorio<BR># [] -
Opcional<BR>#</FONT></DIV>
<DIV><FONT face="Courier New" size=2># o arquivo de host deve ser no seguinte
formato:</FONT></DIV>
<DIV><FONT face="Courier New" size=2># IP |
HOSTNAME</FONT> <FONT face="Courier New" size=2>
| plataforma(7500, 2500 etc)</FONT></DIV>
<DIV><FONT face="Courier New" size=2>#</FONT></DIV>
<DIV><FONT face="Courier New" size=2># os comandos devem estar um em cada linha,
incluindo config terminal, end e tudo mais</FONT></DIV>
<DIV><FONT face="Courier New" size=2>#</FONT></DIV>
<DIV><FONT face="Courier New" size=2># </FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>if ( $ARGV[0] eq "" ) {<BR>print
<<ECHO;<BR>O formato para uso deste script e:<BR>telnet.pl
<Listagem_dos_hosts> <Arquivo_de_Comandos> [opcoes]<BR><> -
Obrigatorio<BR>[] - Opcional</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>As opcoes disponiveis sao:<BR>-log:<nome
do arquivo> -> gera um unico arquivo de log, conforme especificado
no nome do arquivo.<BR>-logrouter:<diretorio> -> gera um arquivo de log
para cada roteador, no formato
<plataforma>_hostname.log<BR>
no diretorio especificado. Se nao for especificado nenhum, sera gerado
no<BR>
diretorio corrente.</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV>
<DIV><FONT face="Courier New" size=2> o arquivo de host deve ser no
seguinte formato:</FONT></DIV>
<DIV><FONT face="Courier New" size=2> IP
| HOSTNAME</FONT> <FONT face="Courier New"
size=2> | plataforma(7500, 2500
etc)</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2> os comandos devem estar um em cada
linha, incluindo config terminal, end e tudo mais</FONT></DIV></DIV><FONT
size=2>
<DIV><FONT face=Arial></FONT><FONT face=Arial></FONT><FONT
face=Arial></FONT><BR><FONT
face="Courier New">ECHO<BR>exit(0);<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">use Net::Ping;<BR>use Term::ReadKey;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Lendo e processando os argumentos de
execucao.<BR>#<BR>$HOSTS=$ARGV[0];<BR>$CMDFILE=$ARGV[1];<BR>for
($m=2;$m<10;$m++) {<BR>
$n=$m-1;<BR> last if ($ARGV[$m] eq
"");<BR>
$OPCAO=$OPCAO.$ARGV[$m];<BR>
}<BR>$n--;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Somente duas opcoes podem ser executadas
simultaneamente.<BR>#<BR>if ($n > 2)
{<BR> print <<ECHO;<BR>Erro!
Muitas opcoes foram especificadas. Especifique no maximo duas
opcoes.</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><BR><FONT
face="Courier New">ECHO<BR> print
"$n\n";<BR>
exit(1);<BR> }</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Trantando os argumentos.<BR>#<BR>if ($OPCAO =~
"-log:") {<BR>
$LOG="1";<BR> ($cum,$DIR) = split
(/\:/,$OPCAO,2);<BR> }</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">if ($OPCAO =~ "-logrouter")
{<BR>
$LOGROUTER="1";<BR> ($cum,$LOGDIR) =
split (/\:/,$OPCAO,2);<BR>
}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">print "$LOG - $DIR\n";<BR>print "$LOGROUTER -
$LOGDIR\n";</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">#exit(0);</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># A primeira operacao a ser realizada e solicitar
o USERNAME e<BR># o PASSWORD para acessar os equipamentos da rede.</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">print "Enter your username: ";<BR>ReadMode
'normal';<BR>$username = ReadLine(0);<BR>chomp $username;<BR>print "Enter your
password: ";<BR>ReadMode 'noecho';<BR>$password = ReadLine 0;<BR>chomp
$password;<BR>print "\nEnable secret: ";<BR>$enable = ReadLine 0;<BR>chomp
$enable;<BR>ReadMode 'normal';</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><BR><FONT face="Courier New"># Lendo o arquivo de hosts especificado pela
variavel $HOSTS.<BR>$m=0;<BR>open (F,"$HOSTS") || die "Cade o arquivo $HOSTS
?";<BR>while (<F>) {<BR>
s/\s+$//g; #remove
"branco" no final da linha por nada<BR> s/\s/
/g; #substitui
"branco" por espaco<BR>
s/\n//;
#substitui <ENTER> por nada<BR>
s/\s//g;
#remove espacos entre os caracteres<BR> next if
/^\s*\!/; #ignora linhas de comentarios<BR> next
if /^\s*$/; #ignora linhas vazias<BR>
$TARGET[$m] = $_;<BR> $m++;<BR>}<BR>close (F);<BR>print "O arquivo
de alvos \"$HOSTS\" contem $m host(s).\n";</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><BR><FONT face="Courier New"># Lendo o arquivo de comandos especificado
pela variavel $CMDFILE.<BR>$n=0;<BR>open (F,"$CMDFILE") || die "Cade o arquivo
$CMDFILE ?";<BR>while (<F>) {<BR>
s/\s+$//g; #remove
"branco" no final da linha por nada<BR> s/\s/
/g; #substitui
"branco" por espaco<BR>
s/\n//;
#substitui <ENTER> por nada<BR> next if
/^\s*\!/; #ignora linhas de comentarios<BR> next
if /^\s*$/; #ignora linhas vazias<BR>
$COMANDOS[$n] = $_;<BR> $n++;<BR>}<BR>close (F);<BR>print "O
arquivo de comandos \"$CMDFILE\" contem $n linhas de
instrucoes(s).\n\n";</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Inicializando variaveis que necessitam de um
valor
inicial<BR>$TOTALCOMMMANDS=$m*$n;<BR>$TOTALERROS=0;<BR>$TOTALTIMEOUTS=0;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Gerando o cabecalho do log, se
especificado.<BR>if ($LOG) {<BR>open (LOG,">>$DIR");</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Obtem data e hora do inicio da
execucao.<BR>($mon,$mday,$year,$hour,$min) =
(localtime())[4,3,5,2,1];<BR>$year=$year+1900;<BR>$mon++;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">print LOG
"\n\n################################################################################\n\n"<BR>
"O arquivo de alvos \"$HOSTS\" contem $m
host(s).\n"<BR> ."O arquivo de
comandos \"$CMDFILE\" contem $n linhas de
instrucoes(s).\n"<BR>
"Iniciando execucao em $mday/$mon/$year - $hour:$min\n\n";<BR>close
(LOG);<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Executando o TELNET aos hosts especificados no
arquivo de alvos e executando os comandos<BR># contidos no arquivo de
comandos.<BR>foreach (@TARGET) {<BR>
&EXECUTA($_);<BR> }</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Gerando o rodape do log, se especificado.<BR>if
($LOG) {<BR>open (LOG,">>$DIR");<BR>print LOG
"\n\n################################################################################\n\n"<BR>
"
SUMARIO DA EXECUCAO\n\n"<BR>
"Numero total de hosts:
$m\n"<BR> ."Nomero de hosts que
nao responderam:
$TOTALTIMEOUTS\n\n"<BR> "Numero
de comandos executados:
$TOTALCOMMMANDS\n"<BR> "Numero
de erros durante execucao:
$TOTALERROS\n\n"<BR> "Listagem
de hosts que nao
responderam:\n"<BR>
"$HOSTSTIMEOUT\n\n"<BR>
"Listagem de erros ocorridos em cada host\n";<BR>for $HOST (sort keys
(%LOGERROS)) {<BR> print LOG "-
Ocorreram $QTDERROS{$HOST} em
$HOST:\n\n"<BR>
"$LOGERROS{$HOST}\n\n";<BR>
}<BR>print LOG
"\n\n################################################################################\n\n";<BR>close
(LOG);<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Sub-rotina de execucao condicional do TELNET a
um host. Checa se o host esta respondendo a<BR># ICMP ECHO (ping).<BR>sub
EXECUTA {<BR>($IP,$HOSTNAME,$PLATAFORM) = split (/\|/,$_[0],3);</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Obtem data e hora da execucao para cada
host.<BR>($mon,$mday,$year,$hour,$min) =
(localtime())[4,3,5,2,1];<BR>$year=$year+1900;<BR>$mon++;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Gera o log da operacao, se especificado.<BR>if
($LOG) {<BR>open (LOG,">>$DIR");<BR>print LOG "\n\nLote de comandos:
$CMDFILE\nHost: $HOSTNAME ($IP)\nData: $mday/$mon/$year -
$hour:$min\n\n";<BR>close (LOG);<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Primeiro testa se a porta 23 esta aberta no
dispositivo, enviando um pacote de 32 Bytes com 2 segundos<BR># de
timeout.<BR>$p = Net::Ping->new("tcp",10,32);<BR>$p->{port_num} =
getservbyname("telnet", "tcp");<BR>if ( $p->ping($IP, 2) )
{<BR> print "Executando os comandos em
$HOSTNAME ($IP)\n";;<BR>
&TELNET;<BR>
}<BR> else
{<BR> print "O host $IP nao esta
respondendo.\n";<BR>
$TOTALTIMEOUTS++;<BR>
$HOSTSTIMEOUT=$HOSTSTIMEOUT."- $HOSTNAME ($IP) : $mday/$mon/$year -
$hour:$min\n";<BR> # Gera o log da
operacao, se especificado.<BR> if
($LOG) {<BR> open
(LOG,">>$DIR");<BR> print LOG "O
Host nao responde.\n";<BR> close
(LOG);<BR>
}<BR>
}<BR>$p->close();<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><BR><FONT face="Courier New"># Sub-rotina para execucao do TELNET a um
host, utilizando EXPECT.<BR>sub TELNET {<BR>open
(F,">/tmp/telnet-router");<BR>print F "#!/usr/bin/expect
-f\n"<BR> ."spawn telnet
$IP\n"<BR> ."expect \"Username:
\"\n"<BR> ."send
\"$username\\r\"\n"<BR> ."expect \"Password:
\"\n"<BR> ."send
\"$password\\r\"\n"<BR> ."expect
\"*>\"\n"<BR> ."send
\"enable\\r\"\n"<BR> ."expect \"Password:
\"\n"<BR> ."send
\"$enable\\r\"\n"<BR> ."expect
\"*#\"\n"<BR> ."send \"term len
0\\r\"\n"<BR> ."expect \"*#\"\n";<BR>foreach
(@COMANDOS) {<BR> if ( $_ eq "<CR>" )
{<BR>
print F "send \"</FONT><A href='file://\\r\\r\"\n'><FONT
face="Courier New">\\r\\r\"\n</FONT></A><FONT
face="Courier New">"<BR>
"expect
\"$HOSTNAME#\"\n";<BR>
}<BR> else
{<BR>
print F "send
\"$_\\r\"\n"<BR>
"expect
\"$HOSTNAME#\"\n";<BR>
}<BR>}<BR>print F "send \"exit\\r\"\n";<BR>close (F);<BR>system("chmod 777
/tmp/telnet-router");<BR>$cum=`/tmp/telnet-router >
/tmp/lastlog`;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Alimentando o log unico.<BR>#<BR>if ($LOG)
{<BR> system ("cat /tmp/lastlog >> $DIR");<BR>
}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Analisando os logs dos comandos executados no
host em execucao.<BR>open (LOGTEMP,"/tmp/lastlog");<BR>$o=0;<BR>while
(<LOGTEMP>) {<BR> $DADOS[$o] =
$_;<BR> if ( ($_ =~ "% Invalid input
detected at") )
{<BR>
$LOGERROS{$HOSTNAME} =
$LOGERROS{$HOSTNAME}.$DADOS[$o-2];<BR>
$QTDERROS{$HOSTNAME}++;<BR>
$TOTALERROS++;<BR>
}<BR>
$TOTALLINHAS++;<BR>
$o++;<BR>}<BR>close (F);</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"># Gerando o LOG individual de cada
roteador.<BR>#<BR>if ($LOGROUTER)
{<BR>
$FILE="";<BR> $FILE=$LOGDIR."/" if
!($LOGDIR eq "");<BR>
$FILE=$FILE.$PLATAFORM."_".$HOSTNAME.".log";<BR>
system("cp /tmp/lastlog $FILE");<BR>}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">unlink ("/tmp/telnet-router");<BR>unlink
("/tmp/lastlog");<BR>}</FONT></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV></BODY></HTML>