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
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
sudo privilegesI 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:
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.
#!/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
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?
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.
OfficeWiFi and OfficePrinter in the script.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.
Open the application "Startup Application Preferences" and click on the button Add
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.
sudo privilegesI 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:
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.
#!/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
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.your_username_goes_here with your user name. You can find user username by typing echo $USER in the terminal.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.
OfficeWiFi and OfficePrinter in the script.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 this at home or in office:
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
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:
autorandr package, which automatically runs whenever you (un)plug a display. If you prefer, you can also do this via a graphical software manager.~/.config/autorandr folder.Henceforth, your laptop will automatically change the default printer for you.