Set default printer according to IP

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








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:



  1. The script works when I either paste the content to or just call it from the command line.

  2. The script is executed by the if-up-mechanism, which is verified by the creation of files /home/user/(A|B).

  3. In particular, following from 1 or 2, the script file itself is executable.






share|improve this question
























    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:



    1. The script works when I either paste the content to or just call it from the command line.

    2. The script is executed by the if-up-mechanism, which is verified by the creation of files /home/user/(A|B).

    3. In particular, following from 1 or 2, the script file itself is executable.






    share|improve this question






















      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:



      1. The script works when I either paste the content to or just call it from the command line.

      2. The script is executed by the if-up-mechanism, which is verified by the creation of files /home/user/(A|B).

      3. In particular, following from 1 or 2, the script file itself is executable.






      share|improve this question












      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:



      1. The script works when I either paste the content to or just call it from the command line.

      2. The script is executed by the if-up-mechanism, which is verified by the creation of files /home/user/(A|B).

      3. In particular, following from 1 or 2, the script file itself is executable.








      share|improve this question











      share|improve this question




      share|improve this question










      asked May 15 at 11:24









      jpmath

      1365




      1365




















          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





          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "89"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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





            share|improve this answer


























              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





              share|improve this answer
























                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





                share|improve this answer














                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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 17 at 8:41

























                answered May 17 at 8:01









                jpmath

                1365




                1365






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    pylint3 and pip3 broken

                    Missing snmpget and snmpwalk

                    How to enroll fingerprints to Ubuntu 17.10 with VFS491