Обновление скрипта https://ip-connection.blogspot.ru/2016/09/telnet.html
Оптимизировал слегка скрипт.
Строка для запуска :
./script.sh ipaddr.lst commands.lst
Где :
script.sh - сам скрипт
ipaddr.lst - файл содержащий IP адреса устройств на которых необходимо выполнить команды.
commands.lst - файл содержащий перечень команд для выполнения на устройствах.
Содержимое файла script.sh
#!/bin/bash
# This is remote telnet exec script.
#
ReportMail=<Укажи свою почту для отчета>
RLogin=<укажи логин>
RPassword=<укажи пароль>
DeviceList=$1
CommandsList=$2
ExecDate=`date +%Y%m%d%H%M%S`
# Log Prepare
LogDir=/tmp/$ExecDate
mkdir $LogDir
mkdir $LogDir/DeviceLogs
cat $CommandsList > $LogDir/CommandsList.log
cat $DeviceList | sort | uniq > $LogDir/DeviceList.log
ReportFile=$LogDir/REPORT-$ExecDate
FullDevLog=$LogDir/REPORT-FullDevLog-$ExecDate
touch $ReportFile
touch $FullDevLog
echo "Configuration Parametrs:" > $ReportFile
echo "" >> $ReportFile
echo "Commands to execute:" >> $ReportFile
cat $CommandsList >> $ReportFile
echo "" >> $ReportFile
echo "Total targets to execute:" >> $ReportFile
cat $DeviceList | sort -u | wc -l >> $ReportFile
echo "" >> $ReportFile
echo "Configuration script started at $ExecDate" && echo "Configuration script started at $ExecDate" >> $ReportFile
echo "" && echo "" >> $ReportFile
#
f_getIP(){
IPList=`cat $DeviceList | sort -u`
TotalDevices=`cat $DeviceList | sort -u | wc -l`
}
f_remote_exec(){
(
sleep 1
echo $RLogin
sleep 0.2
echo $RPassword
sleep 0.2
while read ip_commands ; do echo "$ip_commands" ; sleep 1; done < $CommandsList
) | telnet $1 >> $LogDir/DeviceLogs/$DATA-$1.log
grep -iE "(login|UserName)" $LogDir/DeviceLogs/$DATA-$1.log >> /dev/null
if [[ $? == 0 ]]
then echo "$DATA $2 of $TotalDevices. $IP : Telnet Success. Log file is $LogDir/DeviceLogs/$DATA-$1.log" && echo "$DATA $2 of $TotalDevices. $IP : Telnet Success. Log file is $LogDir/DeviceLogs/$DATA-$1.log" >> $ReportFile
else echo "$DATA $2 of $TotalDevices. $IP : Telnet Unsuccess. Log file is $LogDir/DeviceLogs/$DATA-$1.log" && echo "$DATA $2 of $TotalDevices. $IP : Telnet Unsuccess. Log file is $LogDir/DeviceLogs/$DATA-$1.log" >> $ReportFile
fi
cat $LogDir/DeviceLogs/$DATA-$1.log >> $FullDevLog
}
f_getIP
i=1
for IP in $IPList
do
DATA=`date +%y%m%d%H%M%S`
ping $IP -c 1 -W 1 >> /dev/null
if [[ $? = 0 ]]
then echo "$DATA $i of $TotalDevices. $IP is ALIVE" && echo "$DATA $i of $TotalDevices. $IP is ALIVE" >> $ReportFile
f_remote_exec $IP $i
else echo "$DATA $i of $TotalDevices. $IP is DEAD" && echo "$DATA $i of $TotalDevices. $IP is DEAD" >> $ReportFile
sleep 0.2
fi
let i=i+1
done
echo "###########" && echo "###########" >> $ReportFile
echo "$0 finished" && echo "$0 finished" >> $ReportFile
cat $ReportFile | mail -s "Script Finished $ExecDate" $ReportMail
exit 0
Комментариев нет:
Отправить комментарий