0
  1. 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.
  2. 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}' )
  • I would not use /etc/hosts, but a dedicated file. Also have a look at rancid for backups, and Ansible. – Rui F Ribeiro Jun 28 '16 at 10:40
  • Yes it's some solution for monitoring, but I like to run few commands on the all switches and it results put to the separated files and parse those file for example by logstash, Kibana. Or present on the different web engine to see whole configuration what is more important for me. – Mac Jun 29 '16 at 12:23
  • I used expect in the past, nowadays Ansible. – Rui F Ribeiro Jun 29 '16 at 12:24
  • Ok, but Ansible is totaly free? to use in the company? – Mac Jun 29 '16 at 12:26
  • Yeah, only the web front end is paid. It is a nice to have, but not strictly necessary. Your choices ran from scripting/bash with ssh, to expect, Ansible and/or Rundeck unix.stackexchange.com/questions/269617/… – Rui F Ribeiro Jun 29 '16 at 12:30
0

Below I'm sending lite update which is almost working, but output files are empty, we getting commands output only on the screen. If somebody could help how to get outputs inside the each file.

#!/bin/bash
#=================================================================
# Variables
#=================================================================
DATE=$(date +"%Y%m%d")
DIR=/logs/cisco/$DATE
INPUT=/etc/hosts
SW=$(sed -e '1,/#ETHERNET SWITCHES/d' -e '/#END SWITCHES/,$d' $INPUT | awk '{print $2}')
#=================================================================
# Check if log directory exists
#=================================================================
if [ ! -d "$DIR" ]; then
   mkdir -p /logs/cisco/"$DATE";
fi
echo "$SW" | while IFS= read -r line
 do
  mkdir -p -- "$DIR/$line"
done <<<"$SW"
#================================================================
# Main
#================================================================
for ip in $(cat $INPUT | sed -e '1,/#ETHERNET SWITCHES/d' -e '/#END SWITCHES/,$d' $INPUT | awk '{print $1}')
   do
   for line in $(cat $INPUT | sed -e '1,/#ETHERNET SWITCHES/d' -e '/#END SWITCHES/,$d' $INPUT | awk '{print $2}')
   do
    sshpass -p kkk ssh admin@"$ip" 'sho int fo1/1' > "$DIR/$line"/port.log
   done
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.