On Ethernet or VPN connected/disconnected

Hey all,

I have some scenarios where I want to interact with an app when I'm connected or disconnected from ethernet.

Now I see there is a wireless network trigger, but this won't suffice for me. And I can look at USB devices (on attach/detach) but sometimes I have to unplug my ethernet cable (from my dock), and it wouldn't catch those instances.

Lastly, I know I could create a script but my fear here is that there's no way to trigger off a script unless we're polling the script periodically which doesn't sound as efficient (or reliable depending on how it's coded).

Would it be too much work to add an ethernet trigger?

Lastly, having a VPN trigger would be nice. If I connect to my home VPN, then I can kick off macros that access my internal homelab sites. This is a secondary priority for me, but would be nice to see included.

1 Like

I agree that polling is rarely the best option, but sometimes it's the only option. Keyboard Maestro doesn't want to poll the operating system (or apps) either, which is why, in general, KM can (generally) provide a trigger only when the operating system has an API that allows your program to register with the operating system so that the OS itself does the triggering (or polling) which is much more efficient than the KM Engine doing polling. So when you ask "can KM add an Ethernet trigger" the answer really depends whether macOS has an API that lets you register for notification for a change to that part of the OS's configuration. And I don't know if it does.

Polling may be bad, but if you're polling once per second on a computer that can perform a trillion operations per second, then polling is not a burden on that computer. Not at all. It's roughly like a 0.0000000001% CPU usage to poll something once per second. Why are we afraid to poll? If you are polling every millisecond, that could be a performance issue. Maybe.

Even so, I might have an answer for you. MacOS stores most of its configuration data in a special file that is accessed through a command line program called "defaults." I have a macro that compares the file before and after you (or some software that you use, like turning a VPN off or on) do something. I've seen a lot of people asking for advice (even today) that my macro could probably solve. That is, my macro would tell you exactly what key in the defaults application needs to be polled.

So I'll tell you what... I'll upload the macro now into a new post. Keep an eye out for it. It's a short macro, just four actions (well, I'm adding some extra actions now to make it more robust and documented.)

Hey Airy! Apologies for not replying, I didn't get a notification and figured no one replied.

looking at the defaults would be a nice work around for sure, I'll take a look to see if you uploaded your macro (or create it if not). Thanks for the suggestion!

With that said, it would be totally awesome if they could add a trigger. I can't imagine network change events wouldn't have an API they can use.

You are right that macOS might provide a trigger for apps to know about VPN changes. I really don't know enough to know if macOS actually can do that. I'm not an API programmer. So you can either wait for someone who knows APIs to investigate that for you, or you can try another approach that I've helped someone else with over the last week.

I was helping someone about detecting (and changing) a camera's availability. Detecting a VPN could probably be done the same way. So I suggest you read the following post and let me know if that helps, or if you want me to help you more directly.

I took a quick look and it seems like SCNetworkReachability has what I need:

Specifically:

  • Create a trigger for a given target network
  • With that trigger, set a callback to the target network using SCNetworkReachabilitySetCallback
  • The callback will provide a callback object which contains a flag object
  • Using the flags object, you can check to ensure that the flag contains .reachable and .connectionRequired.

So the trigger will be tailored to a network (which is preferred), and then run when a flag condition is met. For me, I need to run when I'm connected (.reachable && !.connectionRequired) or disconnected.

I see a similar thread here...

If KM doesn't use this macOS trigger, then you may have to solve this by polling in KM. I should have deferred to that thread earlier, if I had known about it.