#!/bin/bash ############################################################################################ # Script exports MAC and ARP tables from switches in file 'iplist.txt' using SNMP protocol # It creates file switch_$ip.csv with the fields: $port $mac $ipaddress # Script removes ports that have more than 1 MAC associated with the port. # MAC number can be adjusted with variable allowed_mac. ################################## MIBS Explanation ################################################## # # ARP MIB - 1.3.6.1.2.1.4.22.1.2 -> IP and MAC address # .1.3.6.1.2.1.4.22.1.2.28.172.17.100.2 = Hex-STRING: 00 30 88 17 9D DC # MAc address '00 30 88 17 9D DC' has IP 172.17.100.2 # MAC MIB - 1.3.6.1.2.1.17.4.3.1.1 -> Instance and its corresponding MAC address # .1.3.6.1.2.1.17.4.3.1.1.0.48.136.23.157.220 = Hex-STRING: 00 30 88 17 9D DC # 0.48.136.23.157 has MAC address 00 30 88 17 9D DC # Integer port MIB - 1.3.6.1.2.1.17.4.3.1.2 -> Instance and Integer value of port # .1.3.6.1.2.1.17.4.3.1.2.0.48.136.23.157.220 = INTEGER: 22 # 0.48.136.23.157.220 has value 22 # Inger to Integer Insance MIB - 1.3.6.1.2.1.17.1.4.1.2 # .1.3.6.1.2.1.17.1.4.1.2.22 = INTEGER: 18 # Instance 22 has integer value 18 # String port MIB - 1.3.6.1.2.1.31.1.1.1.1 -> Integer valune and its corresponding string value of port # .1.3.6.1.2.1.31.1.1.1.1.18 = STRING: Fa0/22 # Integer instance 18 is port Fa0/22 # RESULT: MAC address '00 30 88 17 9D DC' is on port Fa0/22 ########################################################################################################### function usage { echo -e "Usage: $0 [OPTIONS]" echo "OPTIONS: " echo -e " -d path to directory where you want to store result" echo -e " -f path to file that contains IP adresses of switches" echo -e " and SNMP creadentials" echo -e " -g omit debugging output do display" echo -e " -n maximum number number of MAC addresses expectected" echo -e " on single switchport, if number of MACs connected to" echo -e " switchport is higher than 'n', port is marked as trunk" echo -e " and will be deleted" echo -e " -v display version" echo -e " -h display this help and exit\n" } function version { echo "getmac.sh 1.2.2" echo "Copyright (C) 2015 Radovan Brezula 'brezular'" echo "License GPLv3+: GNU GPL version 3 or later ." echo "This is free software: you are free to change and redistribute it." echo "There is NO WARRANTY, to the extent permitted by law." exit } function read_arguments { while getopts "d:f:n:gvh" arg; do case "$arg" in d) resultdir="$OPTARG";; f) ipfile="$OPTARG";; g) deb=false;; n) allowed_mac="$OPTARG" allowed_mac=$(echo $allowed_mac);; # remove \r from variable thus number becomes integer v) version;; h) usage exit;; esac done } function check_arguments { [ ! "$(type -p snmpwalk)" ] && echo "Utility 'snmpwalk' not found, install it" && exit [ ! -f "$ipfile" ] && echo "Can't find file with list of IP addresses of switches, use $0 -h" && exit if [ ! -d "$resultdir" ]; then echo "Can't find directory where results will be stored, use $0 -h" exit elif [ -d "$resultdir" ]; then rm "$resultdir"/switch_*.csv 2>/dev/null fi [[ ! "$allowed_mac" =~ ^[[:digit:]]+$ ]] && echo "Can't read number of allowed MAC adresses for single switchport, use $0 -h" && exit for line in $(cat "$ipfile"); do ip=$(echo $line | cut -d "," -f1) echo "$ip" | grep -w "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" | grep -v grep &>/dev/null; ip_val=$? [ "$ip_val" != 0 ] && echo "IP address: '$ip' in file '$ipfile' is not valid IP address, exiting" && exit vlan=$(echo $line | cut -d "," -f4) if [[ ! "$vlan" =~ ^[[:digit:]]+$ ]]; then echo "Switch: $ip, VLAN: '$vlan' in file '$ipfile' is not valid VLAN, exiting" exit elif ( [ "$vlan" -gt 4094 ] || [ "$vlan" -eq 0 ] ); then echo "Switch: $ip, VLAN: '$vlan' in file '$ipfile' is not valid VLAN, exiting" exit fi version=$(echo $line | cut -d "," -f2) if ( [ "$version" != 1 ] && [ "$version" != 2c ] && [ "$version" != 3 ] ); then echo "Switch: $ip, SNMP version: '$version' in file '$ipfile' is not valid version, exiting" exit fi done } ##################################### GET ARP TABLES ############################################### function get_arp_tables { # Function creates file 'search_ip_mac.txt' that contains pair ip:mac from MIBS - ARP, IP DHCP binding [ -d ARP-Tables ] || mkdir ARP-Tables echo -e "\nDownloading ARP tables from switches in '$(pwd)/$ipfile'" for line in $(cat $ipfile); do # Get ARP tables of each switch ip=$(echo "$line" | cut -d "," -f1) snmpver=$(echo "$line" | cut -d "," -f2) string=$(echo "$line" | cut -d "," -f3) vlan=$(echo "$line" | cut -d "," -f4) echo "Connecting to switch: '$ip', vlan: '$vlan'" if ( [ "$snmpver" == 2c ] || [ "$snmpver" == 1 ] ); then arp_v2 & elif [ "$snmpver" == 3 ]; then # Read SNMP v3 security level (noAuthNoPriv, authNoPriv, level=$(echo "$line" | cut -d "," -f3) # authPriv user=$(echo "$line" | cut -d "," -f5) # Read SNMPv3 username if [ "$level" == "noAuthNoPriv" ]; then # Get ARP table with SNMP without both athentication arp_v3_noAuthNoPriv & # and encryption elif [ "$level" == "authNoPriv" ]; then auth=$(echo "$line" | cut -d "," -f6) # Read authentication protocol - md5/sha authpass=$(echo "$line" | cut -d "," -f7) # and password if [[ "$auth" != md5 && "$auth" != sha ]]; then # Exit if wrong authentication protocol is read echo -e "\nWarning! Authentication protocol '$auth' is not valid for switch: '$ip', exiting" exit fi arp_v3_authNoPriv & elif [ "$level" == "authPriv" ]; then auth=$(echo "$line" | cut -d "," -f6) # Read authentication protocol - md5/sh authpass=$(echo "$line" | cut -d "," -f7) # and password enc=$(echo "$line" | cut -d "," -f8) # Read encryption protocol des/aes encpass=$(echo "$line" | cut -d "," -f9) # and password if [[ "$auth" != md5 && "$auth" != sha ]]; then # Exit if wrong authentication protocol is read echo -e "\nWarning! Authentication protocol '$auth' is not valid for switch: '$ip', exiting" exit elif [[ "$enc" != des && "$enc" != aes ]]; then # Exit if wrong encryption protocol is read echo -e "\nWarning! Encryption protocol '$enc' is not valid for switch: '$ip', exiting" exit fi arp_v3_authPriv & else echo -e "\nWarning! SNMP security level: '$level' is not valid, exiting" # Exit if security level is not recognized exit fi fi done check_pid_snmpwalk # Check if they're snmp pids running cp "$ipfile" "$ipfile_extracted" # Backup original file for file in ARP-Tables/*; do # Delete empty files in directory ARP-Tables and delete lines with IPs that # are not reacheable using command 'snmpwalk' if [ ! -s "$file" ]; then ip=$(echo $file | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}") echo "Warning! File '$file' is empty, IP '$ip' skipping SNMP checking " sed -i "/$ip/d" "$ipfile_extracted" # Delete rows that contain unreachable IPs rm "$file" else cat "$file" >> ip_mac.txt # Create single file containing all ARP entries include duplicated entries fi done if [ ! -s ip_mac.txt ]; then # If file 'ip_mac.txt is empty, we can't connect to any switch echo -e "\nWarning! File 'ip_mac.txt is empty, check your connection or SNMP credentials, exiting" exit fi cat ip_mac.txt | sort | uniq > search_ip_mac.txt # Remove duplicate entries rm ip_mac.txt } ############################################# Check PID ########################################################### function check_pid_snmpwalk { echo -e "\nChecking if 'snmpwalk' is running" while [ "$(ps -ef | grep snmpwalk | grep -v grep)" ]; do echo -n "!" sleep 1 done echo -e "\nDone!" } ############################################# ARP functions ######################################################## function arp_v2 { snmpwalk -v $snmpver -On -c $string $ip 1.3.6.1.2.1.4.22.1.2 2>/dev/null | sed -e 's/\.1\.3\.6\.1\.2\.1\.4\.22\.1\.2\.[0-9]\{1,4\}\.//g' | sed -e 's/ = Hex-STRING: /:/g' > ARP-Tables/arp_$ip.txt } function arp_v3_noAuthNoPriv { snmpwalk -v $snmpver -On -l $level -u $user $ip 1.3.6.1.2.1.4.22.1.2 2>/dev/null | sed -e 's/\.1\.3\.6\.1\.2\.1\.4\.22\.1\.2\.[0-9]\{1,4\}\.//g' | sed -e 's/ = Hex-STRING: /:/g' > ARP-Tables/arp_$ip.txt } function arp_v3_authNoPriv { snmpwalk -v $snmpver -On -l $level -u $user -a $auth -A $authpass $ip 1.3.6.1.2.1.4.22.1.2 2>/dev/null | sed -e 's/\.1\.3\.6\.1\.2\.1\.4\.22\.1\.2\.[0-9]\{1,4\}\.//g' | sed -e 's/ = Hex-STRING: /:/g' > ARP-Tables/arp_$ip.txt } function arp_v3_authPriv { snmpwalk -v $snmpver -On -l $level -u $user -a $auth -A $authpass -x $enc -X $encpass $ip 1.3.6.1.2.1.4.22.1.2 2>/dev/null | sed -e 's/\.1\.3\.6\.1\.2\.1\.4\.22\.1\.2\.[0-9]\{1,4\}\.//g' | sed -e 's/ = Hex-STRING: /:/g' > ARP-Tables/arp_$ip.txt } ############################################# Get SNMP DATA ######################################################## function get_snmp_data { [ -d MIB-Tables ] || mkdir MIB-Tables echo -e "\nDownloading MAC nad port tables from switches in '$(pwd)/$ipfile'" for line1 in $(cat $ipfile_extracted); do ip=$(echo "$line1" | cut -d "," -f1) snmpver=$(echo "$line1" | cut -d "," -f2) string=$(echo "$line1" | cut -d "," -f3) # community string without vlan ID vlan=$(echo "$line1" | cut -d "," -f4) # vlan echo "Connecting to switch: '$ip', vlan: '$vlan'" # MAC and ports from different VLANs are added to same file except of 'mib_port_string_$ip' that is the same for each VLAN # thus we have to replace it if ( [ "$snmpver" == 2c ] || [ "$snmpver" == 1 ] ); then [ "$vlan" != '1' ] && string="$string@$vlan" # community string + vlan ID e.g public@30 for community string public and vlan 30 # if vlan id is not equal @1 we have to add @1 to community string mac_v2 & elif [ "$snmpver" == 3 ]; then level=$(echo "$line1" | cut -d "," -f3) user=$(echo "$line1" | cut -d "," -f5) [ "$vlan" == '1' ] && vlan="" || vlan="vlan-$vlan" if [ "$level" == "noAuthNoPriv" ]; then # Get ARP table with SNMP without both athentication mac_v3_noAuthNoPriv & elif [ "$level" == "authNoPriv" ]; then auth=$(echo "$line1" | cut -d "," -f6) # Read authentication protocol - md5/sha authpass=$(echo "$line1" | cut -d "," -f7) # and password mac_v3_authNoPriv & elif [ "$level" == "authPriv" ]; then auth=$(echo "$line1" | cut -d "," -f6) # Read athentication protocol - md5/sha authpass=$(echo "$line1" | cut -d "," -f7) # and password enc=$(echo "$line1" | cut -d "," -f8) # Read encryption protocol des/aes encpass=$(echo "$line1" | cut -d "," -f9) # and password mac_v3_authPriv & fi fi done check_pid_snmpwalk # Check if they're snmp pids running } ############################################# MAC functions ######################################################## # We must stored MAC tables from different VLANs to unigue files for each VLAN ID otherwise some entries would be stored # incorrectly when several snmpwalk instances are running in background # Those unique files will be merged together to single file according to their IP address by function 'merge_mac_tables; function mac_v2 { sufix="vlan-$vlan" snmpwalk -v $snmpver -On -c $string $ip 1.3.6.1.2.1.17.4.3.1.1 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.4\.3\.1\.1\.//g' >> MIB-Tables/mib_mac_$ip-$sufix snmpwalk -v $snmpver -On -c $string $ip 1.3.6.1.2.1.17.4.3.1.2 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.4\.3\.1\.2\.//g'>> MIB-Tables/mib_port_int_$ip-$sufix snmpwalk -v $snmpver -On -c $string $ip 1.3.6.1.2.1.17.1.4.1.2 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.1\.4\.1\.2\.//g'>> MIB-Tables/mib_port_int_int_$ip-$sufix snmpwalk -v $snmpver -On -c $string $ip 1.3.6.1.2.1.31.1.1.1.1 | sed -e 's/\.1\.3\.6\.1\.2\.1\.31\.1\.1\.1\.1\.//g'> MIB-Tables/mib_port_string_$ip } function mac_v3_noAuthNoPriv { [ -z "$vlan" ] && sufix='vlan-1' || sufix=$vlan snmpwalk -v $snmpver -On -n "$vlan" -l $level -u $user $ip 1.3.6.1.2.1.17.4.3.1.1 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.4\.3\.1\.1\.//g' >> MIB-Tables/mib_mac_$ip-$sufix snmpwalk -v $snmpver -On -n "$vlan" -l $level -u $user $ip 1.3.6.1.2.1.17.4.3.1.2 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.4\.3\.1\.2\.//g'>> MIB-Tables/mib_port_int_$ip-$sufix snmpwalk -v $snmpver -On -n "$vlan" -l $level -u $user $ip 1.3.6.1.2.1.17.1.4.1.2 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.1\.4\.1\.2\.//g'>> MIB-Tables/mib_port_int_int_$ip-$sufix snmpwalk -v $snmpver -On -n "" -l $level -u $user $ip 1.3.6.1.2.1.31.1.1.1.1 | sed -e 's/\.1\.3\.6\.1\.2\.1\.31\.1\.1\.1\.1\.//g'> MIB-Tables/mib_port_string_$ip } function mac_v3_authNoPriv { [ -z "$vlan" ] && sufix='vlan-1' || sufix=$vlan snmpwalk -v $snmpver -On -n "$vlan" -l $level -u $user -a $auth -A $authpass $ip 1.3.6.1.2.1.17.4.3.1.1 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.4\.3\.1\.1\.//g' >> MIB-Tables/mib_mac_$ip-$sufix snmpwalk -v $snmpver -On -n "$vlan" -l $level -u $user -a $auth -A $authpass $ip 1.3.6.1.2.1.17.4.3.1.2 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.4\.3\.1\.2\.//g'>> MIB-Tables/mib_port_int_$ip-$sufix snmpwalk -v $snmpver -On -n "$vlan" -l $level -u $user -a $auth -A $authpass $ip 1.3.6.1.2.1.17.1.4.1.2 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.1\.4\.1\.2\.//g'>> MIB-Tables/mib_port_int_int_$ip-$sufix snmpwalk -v $snmpver -On -n "" -l $level -u $user -a $auth -A $authpass $ip 1.3.6.1.2.1.31.1.1.1.1 | sed -e 's/\.1\.3\.6\.1\.2\.1\.31\.1\.1\.1\.1\.//g'> MIB-Tables/mib_port_string_$ip } function mac_v3_authPriv { [ -z "$vlan" ] && sufix='vlan-1' || sufix=$vlan snmpwalk -v $snmpver -On -n "$vlan" -l $level -u $user -a $auth -A $authpass -x $enc -X $encpass $ip 1.3.6.1.2.1.17.4.3.1.1 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.4\.3\.1\.1\.//g' >> MIB-Tables/mib_mac_$ip-$sufix snmpwalk -v $snmpver -On -n "$vlan" -l $level -u $user -a $auth -A $authpass -x $enc -X $encpass $ip 1.3.6.1.2.1.17.4.3.1.2 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.4\.3\.1\.2\.//g'>> MIB-Tables/mib_port_int_$ip-$sufix snmpwalk -v $snmpver -On -n "$vlan" -l $level -u $user -a $auth -A $authpass -x $enc -X $encpass $ip 1.3.6.1.2.1.17.1.4.1.2 | sed -e 's/\.1\.3\.6\.1\.2\.1\.17\.1\.4\.1\.2\.//g'>> MIB-Tables/mib_port_int_int_$ip-$sufix snmpwalk -v $snmpver -On -n "" -l $level -u $user -a $auth -A $authpass -x $enc -X $encpass $ip 1.3.6.1.2.1.31.1.1.1.1 | sed -e 's/\.1\.3\.6\.1\.2\.1\.31\.1\.1\.1\.1\.//g'> MIB-Tables/mib_port_string_$ip } ###################################### Merge MAC tables for different VLANs ### ################################################ function merge_mac_tables { cat $ipfile_extracted | cut -d "," -f1 | sort | uniq > search_ip_switches.txt # Create file that contains unique IP of switches echo -e "\nPlease wait, merging MAC tables from different VLANs found in directory '$(pwd)/MIB-Tables' to single MAC table takes time" for ip in $(cat search_ip_switches.txt); do cat MIB-Tables/mib_mac_$ip-vlan-* > MIB-Tables/mib_mac_$ip # Merge mib_mac_$ip from different VLANs to single mib_mac_$ip cat MIB-Tables/mib_port_int_$ip-vlan-* > MIB-Tables/mib_port_int_$ip # Merge mib_port_int_$ip from different vlans to single # mib_port_int_$ip cat MIB-Tables/mib_port_int_int_$ip-vlan-* > MIB-Tables/mib_port_int_int_$ip # Merge mib_port_int_int_$ip from different VLANs to # single mib_port_int_int_$ip done rm MIB-Tables/mib_mac_*-vlan-* MIB-Tables/mib_port_int*-vlan-* # Delete MACs tables for different VLANs we only keep single } # MAC table for all VLANs ############################################# Parse MAC and ARP tables ######################################################## function parse_mac_ip { [ -d Logs ] || mkdir Logs echo -e "\nPlease wait, parsing MACs in directory '$(pwd)/MIB-Tables' and ARP tables in '$(pwd)/ARP-Tables' takes time" for switch in $(cat "$ipfile_extracted"); do ip=$(echo "$switch" | cut -d "," -f1) vlan=$(echo "$switch" | cut -d "," -f4) if [ ! -f "$resultdir"/switch_$ip.csv ]; then # If file switch_$ip.csv exists, switch IP is duplicated in ipfile_extracted # so we have to skip the line to avoid duplication [ "$deb" != 'false' ] && echo -e "\n+++ Switch: '$ip' including trunk port(s) +++" while read line2; do mac=$(echo "$line2" | cut -d ":" -f2); mac=$(echo $mac) # Exports single MAC address from mib_mac_$ip # but we must be sure that loaded MAC is correct # otherwise we aren't going to put it to switch_$ip.csv if [[ $mac =~ ^[0-9A-F]{2}[[:space:]][0-9A-F]{2}[[:space:]][0-9A-F]{2}[[:space:]][0-9A-F]{2}[[:space:]][0-9A-F]{2}[[:space:]][0-9A-F]{2}$ ]]; then id=${line2%=*} # Export ID of instance e.g 0.48.136.23.157.220 # from mib_mac_$ip port_int=$(grep -m 1 "$id" MIB-Tables/mib_port_int_$ip | cut -d ":" -f2) # Exports integer value of port e.g. 22 port_int=$(echo $port_int) # for instance 0.48.136.23.157.220 from mib_port_int if [[ ! $port_int =~ ^[[:digit:]]+$ ]]; then # Show warning if integer value of $port_int is not number # and debugging is enabled but always create log entry if if [ "$deb" != 'false' ]; then # port_in is not number echo "Warning! Port value: '$port_int' is not number, skipping parsing for MAC address: '$mac'" | tee -a Logs/switchlog_parse_$ip.txt else echo "Warning! Port value: '$port_int' is not number, skipping parsing for MAC address: '$mac'" >> Logs/switchlog_parse_$ip.txt fi fi if ( [[ $port_int =~ ^[[:digit:]]+$ ]] && [[ "$port_int" != 0 ]] ); then # Only if instance is found in 'mib_port_int_$ip' # it is not empty, contains digits only and doesn't equal zero # we export port id 18 for port number 22 port_int_int=$(grep -w ""$port_int"[[:space:]]\=[[:space:]]INTEGER:" MIB-Tables/mib_port_int_int_$ip | awk '{print $4}') port=$(grep -w "^$port_int_int" MIB-Tables/mib_port_string_$ip | cut -d ':' -f2) # Exports string value of port Fa0/22 for port # id 18 integer value 3 from mib_port_string_$i port=$(echo $port) iphost=$(grep -m 1 "$mac" search_ip_mac.txt | cut -d ":" -f1) if [ "$deb" != 'false' ]; then echo "$port,$mac,$iphost" | tee -a "$resultdir"/switch_$ip.csv # Display result including trunk ports and save it to log else echo "$port,$mac,$iphost" >> "$resultdir"/switch_$ip.csv # Only save result to log if debugginf is disabled fi fi else # Display warning and store it to log if MAC is not valid if debbuging is enabled, if not save warning to log only [ "$deb" != 'false' ] && echo "Warning! MAC address: '$mac' is not valid, skipping parsing" | tee -a Logs/switchlog_parse_$ip.txt [ "$deb" == 'false' ] && echo "Warning! MAC address: '$mac' is not valid, skipping parsing" >> Logs/switchlog_parse_$ip.txt fi done < MIB-Tables/mib_mac_$ip fi done } #################################### Remove Trunks ################################ function remove_trunks { [ -d Iface-Count ] || mkdir Iface-Count [ "$deb" != 'false' ] && echo -e "\nDeleting trunk ports from files located in directory '$resultdir'" for file in "$resultdir"/switch_[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.csv; do ip=$(echo "$file" | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}") # Grep IP from name of file cat "$file" | awk -F ',' '{print $1}' | sort | uniq -c > Iface-Count/iface_count_$ip # Count occurence of interfaces for each switch while read line; do count=$(echo $line | awk '{print $1}') port=$(echo $line | awk '{print $2}') if [ "$count" -gt "$allowed_mac" ]; then # We don't need trunk ports that has more MACs than 'allowed_mac' [ "$deb" != 'false' ] && echo "Note! Port '$port' on switch '$ip' is trunk and it has '$count' MAC addresses connected, removing port '$port' from '$file'" # Add deleted trunk port and MACs to log file echo "+++ Switch: '$ip' with deleted trunk port: '$port' and total deleted MACs count: '$count' +++" >> Logs/switchlog_trunk_$ip.txt grep "$port" "$resultdir"/switch_$ip.csv >> Logs/switchlog_trunk_$ip.txt sed -i "\^\<$port\>^d" "$resultdir"/switch_$ip.csv # Remove redundant ports fi done < Iface-Count/iface_count_$ip done echo -e "\nDisplaying content of files stored in directory '$(pwd)/$resultdir'\n" for file in "$resultdir"/switch_[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.csv; do if [ -f "$file" ]; then linecount=$(wc -l "$file" | cut -d ' ' -f1) ip=$(echo "$file" | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}") echo "+++ Switch: '$ip' with '$linecount' host(s) connected ++++" cat $file echo "" fi done echo -e "\nNote! Script finished, check result in directory '$(pwd)/$resultdir'\n" } ############################### Delete Temporary Files ######################################## function del_temp_files { rm -rf ARP-Tables Iface-Count iplist-extracted.txt MIB-Tables search_ip_mac.txt search_ip_switches.txt 2>/dev/null Logs } ##################################### BODY ######################################## read_arguments $@ check_arguments del_temp_files ipfile_extracted='iplist-extracted.txt' get_arp_tables get_snmp_data merge_mac_tables parse_mac_ip remove_trunks