# Networking scripts (still need help with this)

## curmudgeon

I would like to see some more examples of (non-trivial) scripts that go in /etc/conf.d/net.

First of all, I would like to run an action only when connected via wlan0 to a specific AP (I need to use the mac address and NOT the SSID). How do I do that?

Similarly, I want to run an action when connected to a specific wired network. How do I check for that? Is there some way of getting the mac address of the DHCP server or router?

Finally (mostly just for curiosity) is there some way of when encountering a specific DHCP server, to abort DHCP and set up a static address?Last edited by curmudgeon on Sun May 27, 2012 7:47 am; edited 1 time in total

----------

## curmudgeon

Bumping this. I still would like (generally trying to to this in /etc/conf.d/net):

1. an example of running a command when connected to a specific wireless access point

2. some way of preventing /etc/resolv.conf from getting overwritten when accessing a specific wireless access point

----------

## fogpipe

 *curmudgeon wrote:*   

> Bumping this. I still would like (generally trying to to this in /etc/conf.d/net):
> 
> 1. an example of running a command when connected to a specific wireless access point
> 
> 2. some way of preventing /etc/resolv.conf from getting overwritten when accessing a specific wireless access point

 

You might glean something from /usr/share/doc/openrc-0.9.9.3/net.example If you havent alread read it. I have just dealt with the second issue myself. Openrc apparently overwrites resolv.conf with what it finds in /etc/conf.d/net. Behaviour that seems to me to border on negligent.

----------

## khayyam

 *curmudgeon wrote:*   

> I would like to see some more examples of (non-trivial) scripts that go in /etc/conf.d/net.

 

Here is mine for your perusal:

```
preup() {

if [[ ${IFACE} != "lo" ]] ; then

    /usr/sbin/postconf 'defer_transports =' && 

    [[ -n $(/etc/init.d/postfix status | awk '$3 ~/started/') ]] &&

    /etc/init.d/postfix reload

    

fi

return 0

}

postup() {

if [[ ${IFACE} != "lo" ]] ; then

    if [[ -z $(/usr/bin/mailq | awk '$4 ~/empty/') ]] ; then

        TIMEOUT="10"

        while [[ ${TIMEOUT} -gt 0 && -z "$(ip route show 0.0.0.0/0)" ]] ; do

            let TIMEOUT=${TIMEOUT}-1 ; sleep 1

        done 

        if [[ ${TIMEOUT} -eq 0 ]] ; then

            echo "" | mail -s "Mail flush deferred: $(date)" postmaster

            return 1

        else

            /usr/sbin/postfix flush

        fi

    fi        

fi

return 0

}

postdown() {

if [[ ${IFACE} != "lo" ]] ; then

    /usr/sbin/postconf 'defer_transports = smtp' &&

    [[ -n $(/etc/init.d/postfix status | awk '$3 ~/started/') ]] &&

    /etc/init.d/postfix reload

fi

return 0

}
```

These, basically, handle offline mail ...

 *curmudgeon wrote:*   

> First of all, I would like to run an action only when connected via wlan0 to a specific AP (I need to use the mac address and NOT the SSID). How do I do that?

 

Something like the following (untested):

```
postup() {

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

        TIMEOUT="10"

        while [[ ${TIMEOUT} -gt 0 && -z "$(ip route show 0.0.0.0/0)" ]] ; do

            let TIMEOUT=${TIMEOUT}-1 ; sleep 1

        done

        

        if [[ ${TIMEOUT} != 0 ]] ; then

            if [[ $(awk '/Access Point/{print $6}' <(/sbin/iwconfig wlan0)) = "F1:82:FA:94:02:B1" ]] ; then

               echo "hello mum!"

            else

               :

            fi

        fi

    fi

return 0

}
```

Here we test to make sure we've associated with an AP, and have an IP asgined (the timeout being a means to give the negociation a chance to occur). We then check the BSSID (which is the MAC) of the AP, sending mum a hello if we're successful.

 *curmudgeon wrote:*   

> Similarly, I want to run an action when connected to a specific wired network. How do I check for that? Is there some way of getting the mac address of the DHCP server or router?

 

Yes ... you could use ip, or netstat, to get the address, and arp to get the MAC

```
GATEWAY=$(/bin/netstat -nr | /usr/bin/awk '$1 ~ /0.0.0.0/{print $2}')

/sbin/arp -an ${GATEWAY} | /usr/bin/awk '{print $4}'
```

 *curmudgeon wrote:*   

> Finally (mostly just for curiosity) is there some way of when encountering a specific DHCP server, to abort DHCP and set up a static address?

 

Well, no, how would you know prior to being asigned an IP? In theory its possible, using arp, to have a link and listen to arp traffic for certain MAC addresses, but this would need to be done prior to any attempt at connection, plus it may fail, some routers will detect your card is in promiscious mode and drop your link. You might be able to use 'preup()' for this, but you would then need to capture packets (say, using tcpdump or tshark) and then parse it, etc, all of this would have to happen fairly quickly as I imagine openrc will timeout if the 'preup()' seems to hang (untested).

HTH ... and best

khay

edit: added a missing 'return 0' in the BSSID test example

----------

## khayyam

 *khayyam wrote:*   

>  *curmudgeon wrote:*   Finally (mostly just for curiosity) is there some way of when encountering a specific DHCP server, to abort DHCP and set up a static address? 
> 
> Well, no, how would you know prior to being asigned an IP? In theory its possible, using arp, to have a link and listen to arp traffic for certain MAC addresses, but this would need to be done prior to any attempt at connection, plus it may fail, some routers will detect your card is in promiscious mode and drop your link. You might be able to use 'preup()' for this, but you would then need to capture packets (say, using tcpdump or tshark) and then parse it, etc, all of this would have to happen fairly quickly as I imagine openrc will timeout if the 'preup()' seems to hang (untested).

 

Actually, I may be wrong in this regard, I just happened to have a look at the dhcpcd.conf manpage and saw the following:

```
arping address [address]

        dhcpcd will arping each address in order before attempting DHCP. If an address is found, we will select the

        replying hardware address as the profile, otherwise the ip address.

        Example:

          interface bge0

          arping 192.168.0.1

          profile 192.168.0.1

          static ip_address=192.168.0.10/24
```

This obvious won't check the MAC, but as it calls arping I assume other applications/scripts could be called. This could then be provided as the configuration for dhcpcd in /etc/conf.d/net (dhcpcd_wlan0="")

HTH ... khay

----------

## khayyam

 *curmudgeon wrote:*   

> Finally (mostly just for curiosity) is there some way of when encountering a specific DHCP server, to abort DHCP and set up a static address?

 

On further investigation this should be fairly trivial ... openrc provides a means to have the network configured based on the BSSID/MAC. 

This example is from /usr/share/doc/openrc-0.9.8.4/net.example.bz2

```
config_001122334455="dhcp"

dhcpcd_001122334455="-t 10"

dns_servers_001122334455="192.168.0.1 192.168.0.2"
```

You would, of course, reverse the use of dhcp, and provide seperate configurations for each network (BSSID/MAC).

best ... khay

----------

