On Wed, 15 Sep 2010, Nathan W wrote:

> i've managed to piece one together myself (warnings, bash amateur), 
> and thought i'd share in case others have similar questions, and ask 
> for feedback on possible improvements/better methodologies; though 
> it works solidly, it feels kludgy to me, especially in the first few 
> lines. thanks in advance for your time.
>
> gateway=`route -n | grep 'UG[ \t]' | awk '{print $2}'`

If you ever end up using a VPN or other nonstandard interface, using 
the route utility to identify your gateway might be problematic since 
it'll return more than one value.

The ip utility, however, will reliably show your default route without 
relying on grep:

   /sbin/ip route show 0.0.0.0/0 | awk '{print $3}'

> mactest=`arp -a | grep -i "$gateway)" | grep -i -o '[0-9A-F]\{2
> \}\(:[0-9A-F]\{2\}\)\{5\}'`

Again, the ip utility will handle this with a little more aplomb:

   /sbin/ip neighbor show $gateway | awk '{print $5}'

> targetmac="XX:XX:XX:XX:XX:XX"

In fact, I suspect you could do away with asking for the default route 
entirely and instead just ask if your target MAC is reachable:

   MAC=$(ip neighbor show | grep 'XX:XX:XX:XX:XX:XX REACHABLE' | wc -l)

   if [ "$MAC" -eq 1 ]; then
     ...
   fi

> upcommand="mount home-net:/media/disk /media/net"
> downcommand="umount home-net:/media/disk"

You can still have these entries in /etc/fstab, just use the "noauto" 
option to keep it from mounting at boot time:

   home-net:/media/disk  /media/net  nfs  hard,intr,rw,noauto  0 0

Then you can replace $upcommand with a simple "mount /media/net" and 
$downcommand with "umount /media/net".

So, assuming you added my suggested /etc/fstab entry, my short version 
of your script would be

#!/bin/sh
MAC=$(ip neighbor show | grep 'XX:XX:XX:XX:XX:XX REACHABLE' | wc -l)
if [ "$MAC" -eq 1 ]; then
   case "$2" in
     up)
       mount /media/net
       ;;
     down)
       umount /media/net
       ;;
     esac
fi

-- 
Paul Heinlein <> [email protected] <> http://www.madboa.com/
_______________________________________________
PLUG mailing list
[email protected]
http://lists.pdxlinux.org/mailman/listinfo/plug

Reply via email to