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

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
  • 37,461
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

user68186
  • 37,461
-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.