MACRO: Online status checker—are you connected?

Some site I was browsing through had posted a review of a $4 app that reported whether or not you were connected to the internet. And while I'm sure the app has some useful features, that seemed like the kind of task that Keyboard Maestro could handle with ease.

After a bit of testing to find a simple way to check the internet connection, I wrote a very basic four-action macro that does just that, and only that: It tests your internet connection once a minute. If everything's good, you'll see a ✓ in the menu bar. If it's not good, you'll see a big red X, like this: :x:.

Download Macro(s): Check online status.kmmacros (5.8 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.7
  • Keyboard Maestro v11.0.3

You can change how often it runs, of course, but it's a very fast, minimal overhead macro—it's got a timeout set to two seconds on the action that checks the connection, so that's the longest it will ever be running (well, very slightly longer to process the next two actions).

How does it work? It simply tries to ping a host that should be up 99.999% of the time—the public DNS that Cloudflare provides at 1.1.1.1. The specific command is this:

ping -c 2 -o 1.1.1.1

That will ping the address twice, and exit on the first successful ping. If it's not successful, the action will time out after two seconds (because the ping won't be completed in that time).

The macro then just checks the ActionResult token. If it's not OK, that means the ping failed, and the connection is probably dead. As such, it then loads the :x: into the variable that the macro group displays in the menu bar.

If it is OK, then all is good, and the ✓ is loaded into the variable.

Keyboard Maestro's ability to use a variable as the item to show in the menu bar for the macro group is incredibly useful here, allowing easy communication of the state of the internet connection by simply changing a variable. Very cool.

Anyway, some might find this useful, if nothing else as a basic building block to use in a more-advanced version of the macro—this one cares nothing about how you connect, doesn't try to check the health of Wi-Fi and Ethernet separately, etc. It just uses whatever connection is active to see if it can reach the net.

-rob.

6 Likes

Shaving an action off:

Check online status v2.kmmacros (3.5 KB)

Getting it down to only one action is an "exercise for the reader" -- plus it seems a bit cheaty to do it by using a more complicated shell script!

Very efficient, but I have one (minor) issue with it: This version has to run ping until it finishes, and if the ping is failing, that can take 10 seconds or so. If you change the action timeout on the IF, then the ping doesn't technically fail and the condition isn't met, so the script would never meet its failure condition.

As for doing it in one step, hmm ... change the shell script into an 'if' structure that returns the proper character, and set the result of the script to that character?

-rob.

Sorry, hadn't noticed that bit of goodness in yours. But how about

ping -c 2 -t 2 -o 1.1.1.1

...if you want a 2-second timeout?

1 Like

Doh, missed that option! Yep, that'd do it. Very nice!

-rob.

man ping shows ~35 options for me, so missing that one tree in a forest full of the b*ggers is very understandable!

1 Like

I do not understand how rg_InternetStatus symbol gets mapped into the menu bar.

It's in the settings for the group, not the macro:

-rob.

2 Likes

And here's the simplified version using @Nige_S' single IF statement.

Download Macro(s): Check online status.kmmacros (3.5 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.7
  • Keyboard Maestro v11.0.3

-rob.

2 Likes

Never knew you could display icons in menu bar
....

Thank you @griffman and @Nige_S, that's really useful!

Hey Rob (@griffman):wave:

Thanks for posting this …

You’re indicating a second IP for usage when having a bad connection… may I ask why it’s not used and why you’re suggesting it ?!

Greetings from Germany :de:

Tobias

It's for testing -- to simulate a failed connection. Much less disruptive than turning your wireless/ethernet off!

1 Like

Exactly. If you were using this simple macro as a base for doing something more advanced when the connection was down, it gives you an easy way to test those changes. Maybe you want it to (for example) display a huge onscreen textbox in red and white that you can't miss? Or switch from your current network method to another to see if it works?

By changing the IP address to one that fails to ping successfully, you can develop the macro without having to actually disconnect from the net.

-rob.

1 Like

Thank you Nige (@Nige_S),
Thank you Rob (@griffman)

Its nice to have something like that … it will definitely help me out when developing and debugging my macros that are based on internet connection…

Very useful :+1:

Greetings from Germany :de:

Tobias

Yep, @griffman thinks of everything. Me, I'd have told the user to turn off their router and try it!

1 Like

Our internet was actually out last evening for maintenance, and I thought it'd be interesting to know how long it was offline. Hence, this new version of the macro, which adds "connection lost" and "connection restored" log entries. Had this existed before last night's outage, I would've found something like this in the log today:

2024-11-01 04:30:02 Log: Internet connection: Lost
...
...other stuff gets logged...
...
2024-11-01 06:22:10 Log: Internet connection: Restored

Replace the existing macro with this version if you want to add logging.

Download Macro(s): Check online status.kmmacros (5.0 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.7
  • Keyboard Maestro v11.0.3

-rob.

1 Like