- Actually I'm concerning how to log multiple switches listed from /etc/hosts and run on each few commands and store the output to one log file on main linux host.
- Second question may ask how to connect the IP address with the switch name and create separated catalog for it and put there each command log.
I saw topic: How to automate connecting to multiple network devices, running a command and saving the output to a file?
But how it could work with my needs?
SCRIPT:
#!/bin/bash
#=================================================================
# Variables
#=================================================================
DATE=$(date +"%Y%m%d")
DIR=/logs/cisco/$DATE
INPUT=/etc/hosts
count=0
IP=$addr
#=================================================================
# Check if log directory exists
#=================================================================
if [ ! -d $DIR ]; then
mkdir -p /logs/cisco/$DATE;
fi;
#================================================================
# Main
#================================================================
while read IP
do
sshpass -p ****** ssh admin@$IP 'sho int fa 1/1' > $DIR/int.log
done < <( sed -e '1,/#ETHERNET SWITCHES/d' -e '/#END SWITCHES/,9999d' $INPUT | awk '{print $1}' )
For ex. Read from /etc/hosts IP+Name (SW1 192.168.0.4) -> run_command -> /logs/cisco/$SW_name/$command_output
– Mac Jun 29 '16 at 12:36