6

I wonder if is it possible to automatically switch the default printer according to the wifi network to which the computer is connected.

Apparently, windows has this feature

  • 2
    I think you can do this automatically, but not with just a few configuration clicks, as far as I know. You need to setup a monitoring program/script of yours to detect the WiFi you are connected to, change or switch the cups configuration files and then ask cups to reload the configuration. That's not rocket science, but also not a trivial task. – EnzoR Apr 16 '18 at 09:31

3 Answers3

2

This answer does not need sudo privileges

I assume you have two places with WiFi connections and two different Network Printers. Your laptop is setup to connect to these two WiFi connections. You have also setup your laptop's printer setup to the two printers and you can print to these two printers when connected to the respective WiFi networks.

For simplicity I will call the:

  1. First WiFi network the "HomeWiFi" and the associated printer the "HomePrinter".
  2. Second WiFi network the "OfficeWiFi" and the associated printer the "OfficePrinter".

I have written a bash script I call changedefaultprinter.sh you can name the file what you want and edit it as you wish. You will need to names of the WiFi and printers in the script to make it work.

The Script

#!/bin/bash
# File name: changedefaultprinter.sh 
# Purpose: Change the default printer to home or office based on WiFi

############################

User modification section

Add WiFi and Printer pairs below:

WIFI1="HomeWiFi" PRINTER1="HomePrinter"

WIFI2="OfficeWiFi" PRINTER2="OfficePrinter"

End of user modification section

############################

CURRENTWIFI=$(iwgetid -r) # Get the name of the current WiFi

if [[ ! $CURRENTWIFI ]]; then # Not on WiFi echo "Could not find any WiFi, exiting..." exit 0 elif [[ $CURRENTWIFI == $WIFI1 ]]; then echo "Found $WIFI1, Setting $PRINTER1 as the default..." lpoptions -d $PRINTER1 elif [[ $CURRENTWIFI == $WIFI2 ]]; then echo "Found $WIFI2, Setting $PRINTER2 as the default..." lpoptions -d $PRINTER2 else echo "On unknown WiFi, exiting..." exit 0 fi

How to use:

  1. Copy the script into a file changedefaultprinter.sh and save it in the location /home/$USER/bin. If you don't have the folder bin create it first. Make the file "executable". See How to make a file executable?

  2. Find out the names of your WiFi and the default printers you have already setup. You can do this in many ways. I use the terminal and the commands below:

Make sure you are connected to your Home WiFi and and the home printer is set as the default. Open a terminal by pressing Ctrl+Alt+T and enter the command:

iwgetid -r

This should show you the name of your WiFi. Copy this name from the terminal and paste it into the file above where it says HomeWiFi.

Now to copy the name of the home printer use the command:

lpstat -t

You will see something like:

scheduler is running
system default destination: HomePrinter 

Copy the name of the printer from the terminal and paste it into the file above where it says HomePrinter.

  1. Now take the laptop to the office and repeat the same process to get the names of your office WiFi and Office default printer into the script. Replace the OfficeWiFi and OfficePrinter in the script.

Try it out:

In the terminal enter the command while connected to the home WiFi:

changedefaultprinter.sh

If all goes well you will see some text telling you your default printer is now set to your home printer. Do the same thing in the office to make sure it works there as well.

Make the script run when you login to the laptop

Open the application "Startup Application Preferences" and click on the button Add

enter image description here

Add a sensible name of the script so that you know what it does, and the location of the script as:

/home/$USER/bin/changedefaultprinter.sh

Where $USER is your username.

Click the Add to save the changes and close the "Startup Application Preferences" window. From now on every time you login to your laptop this script will run. It will determine which WiFi you are on and change the default printer.

user68186
  • 33,360
  • This is very well described. Make it run on network connectivity change and you might have yourself a winning solution. :-) – noughtnaut Nov 23 '20 at 08:38
  • Your new answer is excellent! Although:
    1. I did make a few tweaks: I had to use lpadmin rather than lpoptions for it to work.
    2. I used ln -s rather than cp so I could keep fiddling with it from my homedir.
    3. I used notify-send -i printer rather than echo to get nicer UI (don't know if that is cross-platform though).
    4. That if block could be changed to a case iterating over an array; that would make it a lot easier for the user to personalise. (I can do this myself; I'm just giving you my thoughts.)
    – noughtnaut Nov 24 '20 at 20:32
  • I cannot for the life of me figure out if the NetworkManager dispatcher scripts are even running. Where do they log to? Why is the service constantly listed as inactive (dead)? These are matters for another question...
  • – noughtnaut Nov 24 '20 at 20:33
  • if echo goes to syslog then definitely something is broken on my machine. A shame, I just reinstalled a few days ago. Ah well. – noughtnaut Nov 24 '20 at 21:31
  • @Noughtnaut it just occurred to me if you don't have the correct permissions (or ownership by root) the script won't run. Since you are not getting any output in syslog the script may not be running for some reason. – user68186 Nov 27 '20 at 16:16