# Any way to launch network services based on gateway MAC?

## Zarhan

Hey,

  Since I have a laptop I'm usually moving it around. Anyway, is it possible to read your default gateway's (acquired via DHCP) MAC address from any variable that could be used in network scripts?

  Reason is that I'd like to e.g. start up Samba AND SSH AND xinetd *only* when I'm home (e.g. I can see my home router), Samba only when I'm at work networks (can see workplace's router), and no services when just connecting via random wireless AP?

  I'm mostly going for the same sort of feature that Windows Vista and 7 has where it can "identify" network you are in and set things (firewall policies etc) according to your preferences. 

  I know that for wireless networks, you can use ESSID. How about wired?

----------

## richard.scott

You could try something like this:

```
#!/bin/bash

ping -c 5 $(route -n | grep "^0" | awk '{ print $2}')

GW_MAC=$(arp | grep $(route -n | grep "^0" | awk '{ print $2}') | awk '{print $3}')

case ${GW_MAC} in

  00:08:74:18:b1:2a)

    /etc/init.d/samba start

  ;;

  00:08:74:18:b1:2b)

    /etc/init.d/sshd start

    /etc/init.d/xinetd start

  ;;

esac
```

I would create this as a script to test with and then get /etc/conf.d/local.start to call this script at boot time.

Rich

----------

## krinn

richard.scott: arp -n else it might fail  :Smile: 

edit: i forgot, what an classy solve!

----------

## Zarhan

Thanks!

I'm just put this in /etc/conf.d/net and the postup() section. I just have to use start_service call instead of directly calling initscripts. I have this now and it works - (I have udev rules to rename my wired interface name to 'lan' and wireless to 'wlan')

```
postup() {

   if [[ ${IFACE} == "lan" ]] ; then

     ping -c 2 $(route -n | grep "^0" | awk '{ print $2}')

     GW_MAC=$(arp -n | grep $(route -n | grep "^0" | awk '{ print $2}') | awk '{print $3}')

     case ${GW_MAC} in

     00:01:de:ad:be:ef)

       start_service sshd

       start_service netmount

       ;;

     esac

     start_service ntpd

   fi

   if [[ ${IFACE} == "wlan" ]] ; then

      start_service ntpd

   fi

   if [[ ${IFACE} == "ppp0" ]] ; then

      start_service ntpd

   fi

}

predown() {

   if [[ ${IFACE} == "lan" ]] ; then

      stop_service netmount

      stop_service sshd

   fi

}

```

----------

## Zarhan

Related to this - migrating to OpenRC broke this, unfortunately. What's the equivalent of "start_service" and "stop_service" in openrc?

----------

