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}' )
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Mac
  • 53
  • 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 http://unix.stackexchange.com/questions/269617/linux-equivalent-to-powershells-one-to-many-remoting/269626#269626 – Rui F Ribeiro Jun 29 '16 at 12:30
  • I will consider to experiment with that tool. But if someone could assist me with that script how read this array and save multiple command output to proper catalog:

    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

1 Answers1

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
Mac
  • 53