0

I've tried a dozen different searches, but nothing has turned up exactly what I need. Here's my use case:

[Minimally at boot, bonus if dynamically]

  1. If Ethernet has (or obtains) link, configure Ethernet up and wifi down (so no bonding, no prioritizing Ethernet over wifi that requires wifi to also be up):
    1. Stop openvpn-server@myvpn .
    2. Bring wlan0 down, and disable it.
    3. Bring eth0 up.
  2. If Ethernet does not have (or loses) link:
    1. Enable wlan0 and bring it up.
    2. Start openvpn-server@myvpn .

I think the default solution (Raspbian based on Debian Jessie on my Pi 2) is mostly there; it runs ifplugd, and doesn't appear to have systemd-networkd installed or running. ifplugd exits, though, instead of running as a daemon. So I might be able to add a script in /etc/ifplugd/action.d/ that would take care of the VPN shutdown. I just need to be sure it's disabled so it doesn't start up while the Ethernet is connected.

TIA for any help!

1

You can create a bash script with this code content and create a systemd unit file where you call the bash file in ExecStart, enabling the unit at boot and using systemctl start yourunit and systemctl stop yourunit commands whenever. See the commented lines :

#!/bin/bash 

# Configure the variables
ETHIF="eth0"
WLANIF="wlan0"
# check ETHIF link every 5 seconds
NEXTCHECK=5

while : 
do

   ETHIP=$(ip -f inet -4 a show dev $ETHIF|grep inet|awk -F ' ' '{print $2}'|awk -F '/' '{print $1}')
   ETHISUP=$(ip -f link a show $ETHIF | grep -o "state UP")
   ETHHAVEROUTE=$(ip r show dev $ETHIF)

   WLANIP=$(ip -f inet -4 a show dev $WLANIF|grep inet|awk -F ' ' '{print $2}'|awk -F '/' '{print $1}')
   WLANISUP=$(ip -f link a show $WLANIF | grep -o "state UP")
   WLANHAVEROUTE=$(ip r show dev $WLANIF)

   # 1 If Ethernet has (or obtains) link
   if [ ! -z "${ETHIP}" ]&&[ ! -z "${ETHHAVEROUTE}" ];then

      # 1-a Stop openvpn-server@myvpn .
      systemctl stop openvpn-server

      # 1-b Bring wlan0 down, and disable it.
      if [ ! -z "$WLANISUP" ];then
         ip link set $WLANIF down
      fi;

      # 1-c Bring eth0 up.
      # If you have a route and an IP address on the interface ,the ETHIF is already UP          
      #if [ -z "$ETHISUP" ];then
      #   ip link set $ETHIF up
      #fi;

   # 2 If Ethernet does not have (or loses) link
   else

      # 2-a Enable wlan0 and bring it up.
      if [ -z "$WLANISUP" ];then
         ip link set $WLANIF up
      fi;

      # 2-b Start openvpn-server@myvpn .
      systemctl start openvpn-server

   fi;

   sleep $NEXTCHECK
done
1
  • 1
    Thank you! I'll give that a try and see how it works. Since something is configuring the interfaces now, I need to make sure it gets disabled so this is the only "network manager" running. – M Hill Dec 20 '19 at 6:03
1

I would say ifplugd is the right daemon to solve your problem. But the Debian package for it uses old style ifupdown managed with /etc/network/interfaces. Since Raspbian Stretch dhcpcd is used for networking instead of ifupdown so support for ifplugd is also "deprecated". I think to use a complete outdated Raspbian version Jessie only for this issue is not a good idea. With an unsupported operating system you will run into trouble more and more with the time. You should better modify ifplugd to run on current Raspbian versions. How to do it you can look at Make ifplugd available again since Raspbian Stretch. With some scripting knowledge it should not be a problem to modify /etc/ifplugs/ifplugs.action shown in that answer to meet your requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.