Linux – How to automatically change the default printer based on the connected network

cinnamonlinux-mintnetworkingprinter

I have a laptop I use at home and at work. When at work, I generally use one printer, and at home I use my home printer. Is there a way to automatically set the default printer to a specific printer based on the network I'm connected to?

Linux Mint lets me automatically connect to a specific VPN if I connect to a certain wifi network. Is there some sort of hook in linux that fires when a specific network connected is activated?

I would prefer an application or similar that already if it exists, but I'm open to writing a script if need be.

If there is no current functionality like this, what events/hooks would I need to use to detect the change in network connectivity, and what command would I then use to change the default printer?

Best Answer

I've made a little project using Bash that creates different printer profiles based on the connected monitor. My system does work, albeit for the wrong reason: rather than looking at the available network or printers, it triggers if you plug in a different monitor at work and at home.

It is based on this Python rewrite of autorandr which selects the appropriate display configuration based on the connected devices.

To install my project, open up a terminal and enter the following:

sudo apt install autorandr
mkdir -p ~/.config/autorandr && cd ~/.config/autorandr
git clone https://github.com/Noughtnaut/noughty-autorandr.git
autorandr -s some_name_here

This is what the above four commands do:

  1. Installs the autorandr package, which automatically runs whenever you (un)plug a display. If you prefer, you can also do this via a graphical software manager.
  2. Creates a folder for the program's local configuration and navigates to it.
  3. Downloads my add-on that manages printer defaults. It is important that this is done inside of the ~/.config/autorandr folder.
  4. Stores your current setup -- including the setting for the default printer.

Note: Run the last command again at the office, or anywhere else with a particular monitor setup using a different name for each profile.

Henceforth, your laptop will automatically change the default printer for you based on what monitor you are connected to.

Related Question