(1) report generation and,
(2) system administration automation .
but really developer may also need it in some other cases
Let us start with report generation:
Bridge Utility readable report with macs:-
Introduction :
------------------
If you used brctl-utils before you will recognize that it's output does not include the MAC addresses of the interfaces connected to the bridge ,and you may also cover that it's output if you have many bridges each has many interfaces connected to it will be confusing because of sake of delimiters between every bridge connected interfaces
take a look :
# brctl show
LAN0_SWITCH0 8000.00ff0a757068 no user1
user2
vyatta_eth2
bridge0 8000.00ff8201167a no isp2_eth0
vyatta_eth0
bridge1 8000.00ff710b5344 no isp1_eth0
vyatta_eth1
bridge2 8000.00ff1b98ba93 no isp1_eth1
bridge3 8000.00ffe7f6b70b no isp2_eth1
virbr0 8000.000000000000 yes
so I decide to create this script
#!/bin/sh
printf "\n\nThe system Bridges report just Do like brctl show but show each {interface : mac}\n"
echo "------------------------------------------------------------------------------------"
br_name=`brctl show | cut -f1 | grep -v 'bridge name'`
ALL_IF=`brctl show | grep -v 'bridge name' | tr '\t' ':' | sed 's/:\{2,\}/\t\t/' | sed -r 's/:no::/\n\t\t/' | sed 's/[0-9]\{1,\}.[0-9]\{1,\}.[a-f0-9]\{1,\}/ /' | awk '{print $1}'`
show_all()
{
for ifs in $ALL_IF
do
if [ ! -z "$(echo $br_name |grep $ifs )" ]
then printf "============\n%-11s:\n============\n" $ifs
macs=$(brctl showmacs $ifs | grep -v port | cut -f2)
i=$( brctl showmacs $ifs | grep -v port | wc -l)
else
if [ $i -gt 0 ]
then
printf "\t%-15s: %-20s\n" $ifs $( echo $macs | cut -d' ' -f${i})
fi
i=$[$i-1]
fi
done
}
if [ "$1" == "all" ]
then show_all
else
desired_bridge=`brctl show | cut -f1 | grep $1 `
if [ -z $desired_bridge ] ;then echo "bad bridge name" ;exit 111 ;fi
limit_bridge=`for i in $br_name ;do echo $i ;done | grep -A 1 $1 | tail -1`
CMD1=`echo "awk /$desired_bridge/,/$limit_bridge/{print}"`
# NOTE : I hash this line
#CMD2=`echo "sed 's/$limit_bridge/ /'"`
show_all | $CMD1 #|CMD2 "and this also commented"
fi
echo " ===========DONE============"
echo
echo
It seem cryptic due to the heterogeneous delimiters between fields
this script output is some thing like that
# sh show_bridge.sh all
The system Bridges report just Do like brctl show but show each {interface : mac}
------------------------------------------------------------------------------------
============
LAN0_SWITCH0:
============
user1 : 00:ff:7d:13:4e:e7
user2 : 00:ff:66:97:c3:f8
vyatta_eth2 : 00:ff:0a:75:70:68
============
bridge0 :
============
isp2_eth0 : 00:ff:d1:bc:23:ff
vyatta_eth0 : 00:ff:82:01:16:7a
============
bridge1 :
============
isp1_eth0 : 00:ff:96:bc:51:7f
vyatta_eth1 : 00:ff:71:0b:53:44
============
bridge2 :
============
isp1_eth1 : 00:ff:1b:98:ba:93
============
bridge3 :
============
isp2_eth1 : 00:ff:e7:f6:b7:0b
============
virbr0 :
============
===========DONE============
Draw back of using this shell script:-
---------------------------------------------------
1- Variable is not interpreted inside the shell commands like awk and sed. this is the reason that make me cast formated string with to create a command with it variable (!)but really it is not always work read the code I show this in comment in the end of the script
2- Shell is the command line interpreter and it call some sub-programs to do the task " This is really performance impact if the script is call frequently it may not be acceptable "
3- The readability of the code is so bad
4- Shell is not a general porpose programming language so you may use other programming language to do tasks for you .