#!/bin/bash
# this script helps with printing some values of your network setting
# and is designed for use in scripts.
# this script is based on linux program ifconfig.
# tested on debian, ubuntu and suse distributions..
# by root@proximasociale.cz aka nettezzaumana
# test if "ifconfig command is accessible by current user"
if [ -x "/sbin/ifconfig" ]; then
IFCFG="/sbin/ifconfig"; else
IFCFG=$(which ifconfig)
if [ ! -x "$IFCFG" ] || [ -z "$IFCFG" ]; then
echo "looks like you have no permissions to run ifconfig command or is not in your path"
exit 1
fi
fi
# help message
HLPMSG() {
echo "run this script with at least one parameter as alias of existing network interface"
echo "you could also specify in second parametr, what would you see in following format:"
echo "~$ ipshow.sh eth0 <keyword>"
echo "<keywords> separated by colons are:"
echo " mac | m -- for mac address"
echo " ip | i -- for ip address"
echo " bcast | b -- for bradcast"
echo "if specified only interface, output will be in order 'ip mac bcast'"
}
# test if is user input empty or not
if [ -z "$1" ]; then
HLPMSG
exit 1
fi
# test if user input is request for help message
case $1 in
-h | -? | --help )
HLPMSG
exit 0
;;
# test if all other case of user's input are usable with program
# if not, just print list of acceptable options and exit..
* )
if [ ! $($IFCFG | grep '^\w' | awk '{print $1}' | grep -x $1) ]; then
echo "can't find this interface, your interfaces are:"
$IFCFG | grep '^\w' | awk '{print $1}'
exit 1
fi
;;
esac
# test for second parameter, first if exists
if [ ! -z "$2" ]; then
# determine number of fields and print error message if detects something incorrect
NF=$(echo $2 | awk -F: '{print NF}')
if [ "$NF" -gt "3" ]; then
echo "too much requests.."
exit 1
fi
# cykle which makes right order of suggested output
# at first make cycle with equal rounds as number of fields in each round.
# in every round works with just one field of input, separated with ":" sign..
for ((x=1;x<$NF+1;x++)) {
# what to do in each round
current=$(echo $2 | cut -d: -f$x)
case $current in
ip | i )
myip=$($IFCFG "$1" | grep '\([0-9]\{1,3\}\.\)\{3\}' | awk '{print $2}' | awk -F: '{print $2}')
order="$order $myip";;
mac | m )
myhwadr=$($IFCFG | grep "$1 " | awk '{print $5}')
order="$order $myhwadr";;
bcast | b)
mybrd=$($IFCFG "$1" | grep '\([0-9]\{1,3\}\.\)\{3\}' | awk '{print $3}' | awk -F: '{print $2}')
order="$order $mybrd";;
# test if correct at all..
* )
HLPMSG
exit 1;;
esac
}
# default behave if no paremeter obtained
else
myip=$($IFCFG "$1" | grep '\([0-9]\{1,3\}\.\)\{3\}' | awk '{print $2}' | awk -F: '{print $2}')
myhwadr=$($IFCFG | grep "$1 " | awk '{print $5}')
mybrd=$($IFCFG "$1" | grep '\([0-9]\{1,3\}\.\)\{3\}' | awk '{print $3}' | awk -F: '{print $2}')
order="$myip $myhwadr $mybrd"
fi
# last adction. only this makes output
echo $order
exit 0
pred dlouhou dobou (kdyz jsem jeste neumel poradne regularni vyrazy, proto ten awk) jsem napsal tento skript. snad ti k necemu bude ..
Tvuj prikaz by pak vypadal takto ::
iptables -A povolene_mac -m mac --mac-source $(ipshow.sh eth0 m) -j ACCEPT
ps. priklad ::
./ipshow.sh eth0 m:i // vypise mac adresu a ip adresu pro eth0
./ipshow.sh eth0 i:m // udela to v obracenem poradi
./ipshow.sh eth0 ip // vypise pouze ip adresu daneho iface
./ipshow.sh eth0 mac // vypise pouze mac adresu daneho iface