SCRIPT TELNET BACKUP EM LOTE
Esse script tem a função de realizar backup de configurações de equipamentos de rede através do programa Telnet:
São dois scripts, o primeiro em bash e o outro em expect:
Segue script bash:
#!/bin/bash -x
########################### ARMAZENAMENTO DE LOGS ############################
#LOG="/root/scripts/backup/logs/backup-ativos-$(date "+%Y%m%d").log"
#exec 1>> >(tee -a "$LOG")
#exec 2>&1
##########################################################
echo "`date` - Iniciando backup de ativos."
for enderecos in `cat /root/scripts/backup/cianet`;
do
#Executa somente se houve conectividade:
ping -c 5 $enderecos > /dev/null
if [ $? -eq 0 ];
then
/root/scripts/backup/telnetconn.exp $enderecos
wait
mv /root/scripts/backup/$enderecos.log /root/scripts/backup/arquivos/$enderecos\_$( date +"%Y%m%d%H%M%S").txt
fi
done
Segue arquivo expect:
##############estrutura expect ###
#!/usr/bin/expect -f
#Usage sshsudologin.expect <host> <ssh user> <ssh password> <su user> <su password>
#exec rm output.txt
log_user 1
set timeout 10
set data [clock format [clock seconds] -format {%A %Y/%m/%d %H:%M:%S}]
set ip_loopback [lindex $argv 0]
#set concentrador [lindex $argv 1]
set name_file [lindex $argv 0]_[clock format [clock seconds] -format %Y%m%d%H%M%S]
log_file -a $ip_loopback.log
spawn telnet $ip_loopback
expect "*?sername: " { send "admin\r" }
expect "*?assword" { send "admin\r" }
expect "*>" { send "ena\r" }
expect "*#" { send "show running\r" }
## Essa sessão "More--" serve para os casos que a função de terminal lenght 0 nao exista na configuração:
expect "*?More-- " { send -- " "; exp_continue; }
sleep 3
expect "*#" { send "\r" }
expect "*#" { send "quit\r" }
expect "*>" { send "quit\r" }
interact
#################################################
Comentários
Postar um comentário