Skip to content

Instantly share code, notes, and snippets.

@jnikolak
Last active February 26, 2018 18:56
Show Gist options
  • Save jnikolak/bec426c82537aaa335acb12a089c9f73 to your computer and use it in GitHub Desktop.
Save jnikolak/bec426c82537aaa335acb12a089c9f73 to your computer and use it in GitHub Desktop.
Networking Topology
#!/bin/bash
#### Thank you once again Jamie Bainbridge for helping me with the script.
#### Requirements: yum install -y "graphviz eog" ; copy script into file and then execute, e.g: ./scriptname.sh
#### I've noticed it doesn't work too well in the new Fedora, but tested with Rhel 7
#set -x
echo "" > first.dot
InitPath=$(pwd)
EndPath="sos_commands/networking"
OutputFile="first.dot"
FirstNic=$(grep "scope global" ip_addr | awk '{print $2}' | head -n 1)
# Enter some info about the server
HostName=$(cat ./hostname)
# Start printing out lines in graphwiz format
echo "graph example1 {" >> $OutputFile
echo "rankdir=LR;" >> $OutputFile
declare -a IpAddr=($(egrep "scope global" sos_commands/networking/ip_address|awk '{print $2}'|sort ))
# Print the Inner and Outer Loop of variables FirstRow and SecondRow
for i in ${#IpAddr[@]}
do
for j in ${IpAddr[@]}
do
printf "\"$HostName\" -- " >> $OutputFile
printf "\"${j[*]}\"\n" >> $OutputFile
done
done
#Add the Main Ip address that points to the gateway
printf "\"$IpAddr\" -- " >> $OutputFile
egrep "default via" $EndPath/ip_route_show_table_all | awk '{print "\""$3"\"[label=Gateway]\n"}' >> $OutputFile
# Set a variable to a gateway
FirstRow=$(egrep "default via" $EndPath/ip_route_show_table_all | awk '{print ""$3"\"\n"}')
#Declare that variable into an ip neigh output
declare -a SecondRow=($(egrep REACH $EndPath/ip_neigh_show|awk '{print $1}'|sort ))
# Count the loop and have a loop that iterates through the variable SecondRow
# Print the Inner and Outer Loop of variables FirstRow and SecondRow
for i in ${#SecondRow[@]}
do
for j in ${SecondRow[@]}
do
printf "\"$FirstRow -- " >> $OutputFile
printf "\"${j[0]}\"[label= $FirstNic,shape=box];\n " >> $OutputFile
done
done
echo "}" >> $OutputFile
#This is to create the graphwiz and then to view it.
dot first.dot -Tpng -o example1.png
eog example1.png
# Helpful site with graphwiz examples https://www.ibm.com/developerworks/aix/library/au-aix-graphviz/
# http://graphs.grevian.org/example
# http://www.tonyballantyne.com/graphs.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment