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

1
  • 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 9:31
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.

5
  • 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 8: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
  • 5) 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
2

This answer needs 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 10-changedefaultprinter. You will need to add your username and the names of the WiFi and printers in the script to make it work.

The Script

#!/bin/bash
# File name: 10-changedefaultprinter 
# Purpose: Change the default printer to home or office based on WiFi
# Instructions: 
#   1.  sudo chown root:root 10-changedefaultprinter
#   2.  sudo chmod 744 10-changedefaultprinter
#   3.  sudo ln -s 10-changedefaultprinter' /etc/NetworkManager/dispatcher.d
# 
############################
# User personalization section

USERNAME="your_username_goes_here"

# Add WiFi and Printer pairs below:
WIFI1="HomeWiFi"
PRINTER1="HomePrinter"

WIFI2="OfficeWiFi"
PRINTER2="OfficePrinter"

# End of Personalization
############################

# Network Manager sets this value
ACTION=$2

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

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

How to use:

  1. Copy the script into a file 10-changedefaultprinter and save it in the location /home/$USER/. If you save it in another location such as /home/$USER/bin then use the command cd /home/$USER/bin to change the current directory in the terminal before using the the commands prefixed with sudo below.
  2. Edit the file and add your personal information. Replace your_username_goes_here with your user name. You can find user username by typing echo $USER in the terminal.
  3. 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.
  2. Change ownership, permissions, and create a symbolic link to its intended location.

Use the terminal and the following commands:

sudo chown root:root 10-changedefaultprinter
sudo chmod 744 10-changedefaultprinter
sudo ln -s 10-changedefaultprinter /etc/NetworkManager/dispatcher.d

You may copy using the cp command or move using the mv command instead of creating a symbolic link using ln -s above.

Try it out:

Try this at home or in office:

  1. Temporarily change the default printer to the "wrong" printer.
  2. Disconnect from WiFi and reconnect

If all goes well your default printer will change based on the WiFi you are connected to. From now on, your default printer will change based on the WiFi you are on.

If it does not work, check the syslog and look for lines with nm-dispatcher for errors.

Hope this helps

0
-1

Alessandro, I'm ashamed for how long I've been putting up with the same annoyance before I figured out how dead simple it really was...!

I've made a little project, which I think you might want to try out. Please let me know either way.

To install it, open up a terminal and enter the following (you don't need to type in the '#' and the number after it):

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

and you're all set. This is what you just did:

  1. Install 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. Create a folder for the program's local configuration, and navigate to it.
  3. Download my add-on that manages printer defaults. It is important that this is done inside of the ~/.config/autorandr folder.
  4. Store your current setup -- including the setting for default printer. Then, when you get to the office, or anywhere else with a particular monitor setup, you run this same command again, with a different name for each profile.

Henceforth, your laptop will automatically change the default printer for you.

2
  • While I appreciate your initiative and innovative solution, I don't think this answers the question. OP wants a solution based on different WiFi not different displays. – user68186 Nov 22 '20 at 15:18
  • 1
    I agree that it's not network based, and I hope to make it so, but I hope that you will agree that this is at least a little step towards a solution. – Noughtnaut Nov 22 '20 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.