-1

I currently have a script that checks the MAC address, tt looks like this

  "m"|"-m" )
    if [ ! -n "$2" ]
    then
        echo "Enter MAC address"
        exit 0
    fi

     out1=`cat $dhcp_files/* | grep -i "$2" 2>/dev/null`
     ip=`cat $dhcp_files/* | grep -i "$2" | awk '{print $8}' | sed s/\;//g  2>/dev/null`
     out=` grep -i $ip $shapy/*  | awk '{print $8}'  2>/dev/null`
     echo -en ''$out1'  on  '$out'  \n'

this is done like that: ./mac_check -m 00:00:00:00:00:00

The result of this script:

host Jon_Rosewelt_6_5 { hardware ethernet 00:1d:4e:b5:d4:10; fixed-address 192.168.101.19; }  on  eth4  

but I would like to do to the script received any form of mac address

aka. ./check_mac -m 001d.4eb5.d410 **or** 001d4eb5d410 **or** 00-1d-4e-b5-d4-10

How can I do that?

Najkon
  • 85
  • 1
  • 9

1 Answers1

1

With GNU sed and Solaris sed:

echo 001d.4eb5.d410 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 001d4eb5d410 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 00-1d-4e-b5-d4-10 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 00:1d:4e:b5:d4:10 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'

Output:

00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10

With your code:

"m"|"-m" )
  mac="$2"                                                       # added
  if [ ! -n "$mac" ]                                             # changed
  then
    echo "Enter MAC address"
    exit 0
  fi

   mac="$(echo "$mac" | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//')"  # added
   out1=`cat $dhcp_files/* | grep -i "$mac" 2>/dev/null`         # changed
   ip=`cat $dhcp_files/* | grep -i "$mac" | awk '{print $8}' | sed s/\;//g  2>/dev/null` # changed
   out=` grep -i $ip $shapy/*  | awk '{print $8}'  2>/dev/null`
   echo -en ''$out1'  on  '$out'  \n'
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • I think you could save an extra process by adding another bit to your sed. You could replace the `tr` with `s/[^0-9a-f]//g;` in your sed command before the other substitutions – Eric Renouf Aug 22 '15 at 14:29
  • excuse me but how it can be implemented in script? – Najkon Aug 22 '15 at 14:57