Иногда приходится применять большое количество команд на коммутаторах , автоматизация данного процесса значительно облегчает жизнь.
Данный скрипт не претендует на оптимальность, но зато полностью логирует все действия производимые на устройстве.
В скрипте предусмотрен ввод логина и пароля для авторизации на устройстве - легко меняется, например можно сразу указать логин и пароль.
В папке со скриптом должен находится файл с командами и файл с IP адресами устройств.
#!/bin/bash
#Files with ip address and commands must be in
#the same directory with this script
#
echo " "
echo " "
DATA_EXEC=`date +%y%m%d%H%M%S`
echo "Script configuration started at $DATA_EXEC"
echo " "
echo "Created by noc"
mkdir ./$DATA_EXEC
# Step0.ScriptName
echo " "
echo "Enter Scypt short name"
read SCRIPTNAME
# Step1.Login Pass entering
echo " "
echo "Attention! Use with caution!"
echo " "
echo "Enter your RADIUS Login and Password:"
echo " "
read -p "login: " Radius_login
read -s -p "password: " Rasius_password
echo " "
echo " "
# Step2.File with commands
echo "Enter a file name with commands:"
echo "File must be in the same dir"
read commands_to_execute
# Step3.File with Ip address
echo "Enter a file name with IP address:"
echo "File must be in the same dir"
read ip_address_to_execute
# Step4.email for report
echo "Enter an email to send the report:"
read email_for_report
# Step5.decide
echo "--------------------------------------------------"
echo "|======== Summary ========|"
echo "This configuration:"
cat ./$commands_to_execute
echo "Will be execute on this number of devices:"
cat ./$ip_address_to_execute | wc -l
echo "by user $Radius_login"
echo "Is this correct?(enter Y or N)"
echo "--------------------------------------------------"
read selected_chois
case $selected_chois in
N )
DATA_ABORT=`date +%y%m%d%H%M%S`
echo "Your chois is NO.... sucker"
echo "Report sent"
echo "User $Radius_login canceled script at $DATA_ABORT" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "Parameters of Cancel" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "File with commands: $commands_to_execute" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "File with IP address: $ip_address_to_execute" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "Commands:" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "`cat $commands_to_execute`" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "IP address:" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "`cat $ip_address_to_execute`" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
cat ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log | mail -s "Script autoconfig Aborted" example@example.com
;;
Y )
DATA_START=`date +%y%m%d%H%M%S`
echo "Script STARTED at $DATA_START"
echo "Script STARTED at $DATA_START" >> ./$DATA_EXEC/REPORT-$DATA_START
echo "Script STARTED by $Radius_login" >> ./$DATA_EXEC/REPORT-$DATA_START
echo "Execution commands:" >> ./$DATA_EXEC/REPORT-$DATA_START
echo "`cat $commands_to_execute`" >> ./$DATA_EXEC/REPORT-$DATA_START
ip_device=`cat $ip_address_to_execute | sort -u`
for IP in $ip_device
do
DATA=`date +%y%m%d%H%M%S`
echo "$DATA $IP"
ping $IP -c 3 >> ./$DATA_EXEC/$DATA-$IP.log
if [[ $? = 0 ]]
then
echo "Host $IP is UP"
echo "Host $IP is UP" >> ./$DATA_EXEC/$DATA-$IP.log
(
sleep 1
echo $Radius_login
echo $Rasius_password
sleep 1
while read ip_commands ; do echo "$ip_commands" ; sleep 1; done < $commands_to_execute
#sleep 1
#echo save
#sleep 2
#echo logo
) | telnet $IP >> ./$DATA_EXEC/$DATA-$IP.log
echo "" >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
echo "============= New Device $IP =================" >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
echo "`date +%y-%m-%d-%H:%M:%S`" >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
cat ./$DATA_EXEC/$DATA-$IP.log >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
echo -e \n >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
echo "============= End Device $IP =================" >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
SUCCESS=`cat ./$DATA_EXEC/$DATA-$IP.log | grep Succes | wc -l`
FAIL=`cat ./$DATA_EXEC/$DATA-$IP.log | grep Fail | wc -l`
AUTH_ERROR=`cat ./$DATA_EXEC/$DATA-$IP.log | grep "Timeout or some unexpected error" | wc -l`
echo "$IP host UP">> ./$DATA_EXEC/REPORT-$DATA_START
echo "$IP Success rate: $SUCCESS">> ./$DATA_EXEC/REPORT-$DATA_START
echo "$IP Fail rate: $FAIL">> ./$DATA_EXEC/REPORT-$DATA_START
echo "$IP Login Error rate: $AUTH_ERROR">> ./$DATA_EXEC/REPORT-$DATA_START
else
echo "$IP host DOWN"
echo "$IP host DOWN" >> ./$DATA_EXEC/$DATA-$IP.log
echo "$IP host DOWN" >> ./$DATA_EXEC/REPORT-$DATA_START
fi
done
DATA_FINISH=`date +%y%m%d%H%M%S`
echo "Script FINISHED at $DATA_FINISH"
#$DATA_RUN=$DATA_FINISH-$DATA_START
#echo "Script execution took $DATA_RUN"
echo "Script FINISHED at $DATA_FINISH" >> ./$DATA_EXEC/REPORT-$DATA_START
cat ./$DATA_EXEC/REPORT-$DATA_START | mail -s "Script Finished $DATA_FINISH $SCRIPTNAME" $email_for_report
#echo "Script FINISHED at $DATA_FINISH" >> ./$DATA_EXEC/REPORT-$DATA_START
;;
* )
echo "Try again"
;;
esac
Данный скрипт не претендует на оптимальность, но зато полностью логирует все действия производимые на устройстве.
В скрипте предусмотрен ввод логина и пароля для авторизации на устройстве - легко меняется, например можно сразу указать логин и пароль.
В папке со скриптом должен находится файл с командами и файл с IP адресами устройств.
#!/bin/bash
#Files with ip address and commands must be in
#the same directory with this script
#
echo " "
echo " "
DATA_EXEC=`date +%y%m%d%H%M%S`
echo "Script configuration started at $DATA_EXEC"
echo " "
echo "Created by noc"
mkdir ./$DATA_EXEC
# Step0.ScriptName
echo " "
echo "Enter Scypt short name"
read SCRIPTNAME
# Step1.Login Pass entering
echo " "
echo "Attention! Use with caution!"
echo " "
echo "Enter your RADIUS Login and Password:"
echo " "
read -p "login: " Radius_login
read -s -p "password: " Rasius_password
echo " "
echo " "
# Step2.File with commands
echo "Enter a file name with commands:"
echo "File must be in the same dir"
read commands_to_execute
# Step3.File with Ip address
echo "Enter a file name with IP address:"
echo "File must be in the same dir"
read ip_address_to_execute
# Step4.email for report
echo "Enter an email to send the report:"
read email_for_report
# Step5.decide
echo "--------------------------------------------------"
echo "|======== Summary ========|"
echo "This configuration:"
cat ./$commands_to_execute
echo "Will be execute on this number of devices:"
cat ./$ip_address_to_execute | wc -l
echo "by user $Radius_login"
echo "Is this correct?(enter Y or N)"
echo "--------------------------------------------------"
read selected_chois
case $selected_chois in
N )
DATA_ABORT=`date +%y%m%d%H%M%S`
echo "Your chois is NO.... sucker"
echo "Report sent"
echo "User $Radius_login canceled script at $DATA_ABORT" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "Parameters of Cancel" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "File with commands: $commands_to_execute" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "File with IP address: $ip_address_to_execute" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "Commands:" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "`cat $commands_to_execute`" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "IP address:" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
echo "`cat $ip_address_to_execute`" >> ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log
cat ./$DATA_EXEC/$DATA_ABORT-ABORT-$Radius_login.log | mail -s "Script autoconfig Aborted" example@example.com
;;
Y )
DATA_START=`date +%y%m%d%H%M%S`
echo "Script STARTED at $DATA_START"
echo "Script STARTED at $DATA_START" >> ./$DATA_EXEC/REPORT-$DATA_START
echo "Script STARTED by $Radius_login" >> ./$DATA_EXEC/REPORT-$DATA_START
echo "Execution commands:" >> ./$DATA_EXEC/REPORT-$DATA_START
echo "`cat $commands_to_execute`" >> ./$DATA_EXEC/REPORT-$DATA_START
ip_device=`cat $ip_address_to_execute | sort -u`
for IP in $ip_device
do
DATA=`date +%y%m%d%H%M%S`
echo "$DATA $IP"
ping $IP -c 3 >> ./$DATA_EXEC/$DATA-$IP.log
if [[ $? = 0 ]]
then
echo "Host $IP is UP"
echo "Host $IP is UP" >> ./$DATA_EXEC/$DATA-$IP.log
(
sleep 1
echo $Radius_login
echo $Rasius_password
sleep 1
while read ip_commands ; do echo "$ip_commands" ; sleep 1; done < $commands_to_execute
#sleep 1
#echo save
#sleep 2
#echo logo
) | telnet $IP >> ./$DATA_EXEC/$DATA-$IP.log
echo "" >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
echo "============= New Device $IP =================" >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
echo "`date +%y-%m-%d-%H:%M:%S`" >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
cat ./$DATA_EXEC/$DATA-$IP.log >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
echo -e \n >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
echo "============= End Device $IP =================" >> ./$DATA_EXEC/REPORT-$DATA_START-fulldevlog
SUCCESS=`cat ./$DATA_EXEC/$DATA-$IP.log | grep Succes | wc -l`
FAIL=`cat ./$DATA_EXEC/$DATA-$IP.log | grep Fail | wc -l`
AUTH_ERROR=`cat ./$DATA_EXEC/$DATA-$IP.log | grep "Timeout or some unexpected error" | wc -l`
echo "$IP host UP">> ./$DATA_EXEC/REPORT-$DATA_START
echo "$IP Success rate: $SUCCESS">> ./$DATA_EXEC/REPORT-$DATA_START
echo "$IP Fail rate: $FAIL">> ./$DATA_EXEC/REPORT-$DATA_START
echo "$IP Login Error rate: $AUTH_ERROR">> ./$DATA_EXEC/REPORT-$DATA_START
else
echo "$IP host DOWN"
echo "$IP host DOWN" >> ./$DATA_EXEC/$DATA-$IP.log
echo "$IP host DOWN" >> ./$DATA_EXEC/REPORT-$DATA_START
fi
done
DATA_FINISH=`date +%y%m%d%H%M%S`
echo "Script FINISHED at $DATA_FINISH"
#$DATA_RUN=$DATA_FINISH-$DATA_START
#echo "Script execution took $DATA_RUN"
echo "Script FINISHED at $DATA_FINISH" >> ./$DATA_EXEC/REPORT-$DATA_START
cat ./$DATA_EXEC/REPORT-$DATA_START | mail -s "Script Finished $DATA_FINISH $SCRIPTNAME" $email_for_report
#echo "Script FINISHED at $DATA_FINISH" >> ./$DATA_EXEC/REPORT-$DATA_START
;;
* )
echo "Try again"
;;
esac
В результате в директории где выполняется скрипт будет создана папка с логами. В папке будут логи под каждое устройство и общий лог.
Комментариев нет:
Отправить комментарий