Set default printer according to IP
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
1
down vote
favorite
Setup:
I use my notebook in two locations. At each location, the computer receives an IP address via DHCP. This IP address depends on the location, but is constant for each location over time. Furthermore, at each location there is a printer.
Task:
I configured both printers and would like to set the default printer according to the IP. To this purpose, I wrote a script and put it under /etc/network/if-up.d/set-default-printer
, using lpoptions -d
to actually set the default printer.
Script:
#!/bin/sh
ip_A="1.1.1.1"
ip_B="2.2.2.2"
pr_A="Printer-A"
pr_B="Printer-B"
ip=$(hostname -I | cut -d " " -f 1)
if [ $ip = $ip_A ]; then
lpoptions -d $pr_A
touch /home/user/A
fi
if [ $ip = $ip_B ]; then
lpoptions -d $pr_B
touch /home/user/B
fi
Problem:
Unfortunately, the default printer is not changed by the script (can be checked via lpoptions
without further arguments).
Further diagnostics:
- The script works when I either paste the content to or just call it from the command line.
- The script is executed by the
if-up
-mechanism, which is verified by the creation of files/home/user/(A|B)
. - In particular, following from 1 or 2, the script file itself is executable.
command-line networking bash printing sh
add a comment |Â
up vote
1
down vote
favorite
Setup:
I use my notebook in two locations. At each location, the computer receives an IP address via DHCP. This IP address depends on the location, but is constant for each location over time. Furthermore, at each location there is a printer.
Task:
I configured both printers and would like to set the default printer according to the IP. To this purpose, I wrote a script and put it under /etc/network/if-up.d/set-default-printer
, using lpoptions -d
to actually set the default printer.
Script:
#!/bin/sh
ip_A="1.1.1.1"
ip_B="2.2.2.2"
pr_A="Printer-A"
pr_B="Printer-B"
ip=$(hostname -I | cut -d " " -f 1)
if [ $ip = $ip_A ]; then
lpoptions -d $pr_A
touch /home/user/A
fi
if [ $ip = $ip_B ]; then
lpoptions -d $pr_B
touch /home/user/B
fi
Problem:
Unfortunately, the default printer is not changed by the script (can be checked via lpoptions
without further arguments).
Further diagnostics:
- The script works when I either paste the content to or just call it from the command line.
- The script is executed by the
if-up
-mechanism, which is verified by the creation of files/home/user/(A|B)
. - In particular, following from 1 or 2, the script file itself is executable.
command-line networking bash printing sh
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Setup:
I use my notebook in two locations. At each location, the computer receives an IP address via DHCP. This IP address depends on the location, but is constant for each location over time. Furthermore, at each location there is a printer.
Task:
I configured both printers and would like to set the default printer according to the IP. To this purpose, I wrote a script and put it under /etc/network/if-up.d/set-default-printer
, using lpoptions -d
to actually set the default printer.
Script:
#!/bin/sh
ip_A="1.1.1.1"
ip_B="2.2.2.2"
pr_A="Printer-A"
pr_B="Printer-B"
ip=$(hostname -I | cut -d " " -f 1)
if [ $ip = $ip_A ]; then
lpoptions -d $pr_A
touch /home/user/A
fi
if [ $ip = $ip_B ]; then
lpoptions -d $pr_B
touch /home/user/B
fi
Problem:
Unfortunately, the default printer is not changed by the script (can be checked via lpoptions
without further arguments).
Further diagnostics:
- The script works when I either paste the content to or just call it from the command line.
- The script is executed by the
if-up
-mechanism, which is verified by the creation of files/home/user/(A|B)
. - In particular, following from 1 or 2, the script file itself is executable.
command-line networking bash printing sh
Setup:
I use my notebook in two locations. At each location, the computer receives an IP address via DHCP. This IP address depends on the location, but is constant for each location over time. Furthermore, at each location there is a printer.
Task:
I configured both printers and would like to set the default printer according to the IP. To this purpose, I wrote a script and put it under /etc/network/if-up.d/set-default-printer
, using lpoptions -d
to actually set the default printer.
Script:
#!/bin/sh
ip_A="1.1.1.1"
ip_B="2.2.2.2"
pr_A="Printer-A"
pr_B="Printer-B"
ip=$(hostname -I | cut -d " " -f 1)
if [ $ip = $ip_A ]; then
lpoptions -d $pr_A
touch /home/user/A
fi
if [ $ip = $ip_B ]; then
lpoptions -d $pr_B
touch /home/user/B
fi
Problem:
Unfortunately, the default printer is not changed by the script (can be checked via lpoptions
without further arguments).
Further diagnostics:
- The script works when I either paste the content to or just call it from the command line.
- The script is executed by the
if-up
-mechanism, which is verified by the creation of files/home/user/(A|B)
. - In particular, following from 1 or 2, the script file itself is executable.
command-line networking bash printing sh
asked May 15 at 11:24
jpmath
1365
1365
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Apparently, the lpoptions
needs to be executed as the local user. Hence, replacing
lpoptions -d $pr
by
sudo -H -u user lpoptions -d $pr
for both printers $pr
solves the problem.
Assuming, user
is your user name, the complete script /etc/network/if-up.d/set-default-printer
is now:
#!/bin/sh
ip_A="1.1.1.1"
ip_B="2.2.2.2"
pr_A="Printer-A"
pr_B="Printer-B"
ip=$(hostname -I | cut -d " " -f 1)
if [ $ip = $ip_A ]; then
sudo -H -u user lpoptions -d $pr_A
fi
if [ $ip = $ip_B ]; then
sudo -H -u user lpoptions -d $pr_B
fi
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Apparently, the lpoptions
needs to be executed as the local user. Hence, replacing
lpoptions -d $pr
by
sudo -H -u user lpoptions -d $pr
for both printers $pr
solves the problem.
Assuming, user
is your user name, the complete script /etc/network/if-up.d/set-default-printer
is now:
#!/bin/sh
ip_A="1.1.1.1"
ip_B="2.2.2.2"
pr_A="Printer-A"
pr_B="Printer-B"
ip=$(hostname -I | cut -d " " -f 1)
if [ $ip = $ip_A ]; then
sudo -H -u user lpoptions -d $pr_A
fi
if [ $ip = $ip_B ]; then
sudo -H -u user lpoptions -d $pr_B
fi
add a comment |Â
up vote
0
down vote
accepted
Apparently, the lpoptions
needs to be executed as the local user. Hence, replacing
lpoptions -d $pr
by
sudo -H -u user lpoptions -d $pr
for both printers $pr
solves the problem.
Assuming, user
is your user name, the complete script /etc/network/if-up.d/set-default-printer
is now:
#!/bin/sh
ip_A="1.1.1.1"
ip_B="2.2.2.2"
pr_A="Printer-A"
pr_B="Printer-B"
ip=$(hostname -I | cut -d " " -f 1)
if [ $ip = $ip_A ]; then
sudo -H -u user lpoptions -d $pr_A
fi
if [ $ip = $ip_B ]; then
sudo -H -u user lpoptions -d $pr_B
fi
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Apparently, the lpoptions
needs to be executed as the local user. Hence, replacing
lpoptions -d $pr
by
sudo -H -u user lpoptions -d $pr
for both printers $pr
solves the problem.
Assuming, user
is your user name, the complete script /etc/network/if-up.d/set-default-printer
is now:
#!/bin/sh
ip_A="1.1.1.1"
ip_B="2.2.2.2"
pr_A="Printer-A"
pr_B="Printer-B"
ip=$(hostname -I | cut -d " " -f 1)
if [ $ip = $ip_A ]; then
sudo -H -u user lpoptions -d $pr_A
fi
if [ $ip = $ip_B ]; then
sudo -H -u user lpoptions -d $pr_B
fi
Apparently, the lpoptions
needs to be executed as the local user. Hence, replacing
lpoptions -d $pr
by
sudo -H -u user lpoptions -d $pr
for both printers $pr
solves the problem.
Assuming, user
is your user name, the complete script /etc/network/if-up.d/set-default-printer
is now:
#!/bin/sh
ip_A="1.1.1.1"
ip_B="2.2.2.2"
pr_A="Printer-A"
pr_B="Printer-B"
ip=$(hostname -I | cut -d " " -f 1)
if [ $ip = $ip_A ]; then
sudo -H -u user lpoptions -d $pr_A
fi
if [ $ip = $ip_B ]; then
sudo -H -u user lpoptions -d $pr_B
fi
edited May 17 at 8:41
answered May 17 at 8:01
jpmath
1365
1365
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1036490%2fset-default-printer-according-to-ip%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password