Set Default printer based on physical location (preferably) vs Wifi MAC address

Hi,

I would like my Mac to automatically assign Default printer based on physical location (preferably) vs Wifi MAC address. I currently use Control Plane for this mission, but it has not been updated in a while and glitches a lot. I can not use Wifi name as in different locations in our organization, Wifi name remains the same. What would be the best way to do it?

Thank you,
Boris

You're right that setting printer defaults based on wifi SSID would not work, but I think the wifi MAC address should be different in different locations as it's tied to the access point. I'm not sure if you can access it easily from KM or not though. Perhaps Apple Script could find it.

That may work, and if it does, great! but I'm afraid that it won't, because that's going to tell you where your ISP is, not necessarily where you are. For example, I would get the same result for my home or work, because we both use the same ISP.

I have a potential solution that should be very reliable, but it's a little geeky.

Ok, it's a lot geeky.

What Ethernet device are you using?

The first thing you need to figure out is what Ethernet device you are using.

Chances are that it is either 'en0' or 'en1'.

Open Terminal.app and type ifconfig and hit return. Look for the IP address that matches what you are using for the network. (Open System Preferences » Network to see your IP.)

Get your Router's IP Address

I'm going to assume you are using 'en0' but if you are using 'en1' or something else, change that in the next line:

netstat -nr | awk -F' ' '/default.*en0/{print $2}'

That's going to give you the IP Address of your router. Let's assume you got '192.168.1.1' for that.

Get the MAC Address of your Router

The MAC address of your router should be unique to that device. I think it is a safe assumption that if you are connected to that particular router, you will always be in the location.

(I'm not a networking expert, so it's possible that I'm wrong about that, but let's assume it's true for now.)

Once you have the IP address of the router, you can get the MAC address like this with this command (remember to replace '192.168.1.1' with the actual IP address of your router, from above):

arp 192.168.1.1

which should give you output that looks something like this:

? (192.168.1.1) at 12:12:db:17:5e:4d on en0 ifscope [ethernet]

The MAC address is the '12:12:db:17:5e:4d' part. So we need to narrow the arp output to just that field.

We can do that in (at least) two ways.

A) We can take the 4th field of the list: "?" is the first, "(192.168.1.1)" is the second, "at" is the 3rd, so the MAC address is the 4th. So if we replace the arp command with this:

arp 192.168.1.1 | awk '{print $4}'

then we should just get

12:12:db:17:5e:4d

B) We can split up the arp output by replacing each space with a 'newline' and then picking out just the line which has 5 colon ":" characters:

arp 192.168.1.1 | tr ' ' '\012' | egrep ".*:.*:.*:.*:.*:.*"

(the 'tr' command says "replace every ' ' with '\012' and '\012' is a newline. The egrep line says 'find 5 colons')

The awk way is simpler, so let's use that. (There are no doubt other ways to do it too.)

Time For Variables!

We want to save the output of this command:

netstat -nr | awk -F' ' '/default.*en0/{print $2}'

as a variable. Let's go with $ROUTER_IP because it's nice and obvious.

ROUTER_IP=$(netstat -nr | awk -F' ' '/default.*en0/{print $2}')

{Aside: note that you do not use a '$' before 'ROUTER_IP=' That is important.}

Now that we have the IP address in a variable, we can use it with arp:

arp "$ROUTER_IP" | awk '{print $4}'

Note that we did use '$' before 'ROUTER_IP' this time.

We can use that to output the MAC address and save it to a Keyboard Maestro variable (which I'll refer to as 'ROUTER_MAC_ADDRESS'

Now we're all set for the fun part.

"Now What?"

OK, so now we have the MAC address. How do we use that?

Answer: Keyboard Maestro, of course, with "if / else" statements.

Take-Actions-By-Location.kmmacros (10.1 KB)

I put together the first part of this, including the shell script that saves to a variable, but you'll have to enter the ROUTER_MAC_ADDRESS of your router to the IF / ELSE parts of the macro.

To facilitate this, the macro will put the ROUTER_MAC_ADDRESS on your clipboard after it runs if it doesn't match any of the previous IF / ELSE statements.

Is this too nerdy to work? If you need help / clarification, I'd be happy to help.

I have just set this up for myself too, so I'll be able to test it tomorrow when I go into the office.

2 Likes

tjluoma,

Just modified your script with my MAC addresses. Works fantastic! Exactly what I needed. Thank you very much. Your script also makes it very easy adding new locations or new routers/MAC addresses of the same location. Really appreciate your help.

bs

In most cases, you can follow this procedure to locate your MAC address : Select Settings > About Device > Status. A WiFi Address or WiFi MAC Address displays. This is your device's MAC address . Check here to check more information related to this.

Thank you for a nice script.
Unfortunately I quit programming sometime mid -90 and have never managed to catch the macro-language...

Some short questions if you have time:

  • I've changed the MAC adress in the script to my routers MAC-adress.
  • I've found out my printers name and the command ("lpstat -d MY_PRINTER_NAME")
    But where do I put it? At what line in the script?

And where (and how) do I make my computer to run the script each time I log-in?
(Probably basic questions so please forgive me for asking)

At the end I found that using IP address did not work for me - due to inconsistencies between different sites in the organization I work for.

I changed that script to work based on location. The script runs 10 seconds after computer wakes up, at login or on connection to a specific WiFi.

You need to download and install Location helper from Mac App Store (https://apps.apple.com/us/app/location-helper/id488536386?mt=12) and also Split Text KBM plug-in (Split Text - Plugin Action).

To make the script work, change WiFi name to the one you use. Run lpstat -p in terminal and change all the printer names I used to the printer names you use. Run Location helper in each one of the locations you use your laptop and replace my coordinates with yours (You can run this part of the script separately to get the truncated numbers:

DEFAULT PRINTER (Location Helper) .kmmacros (15.5 KB)