How to find sudo rights in shell script












0














I am struggling to work out how to achieve this, as it does not support -a parameter on CentOS/RHEL.



My question is : I will get all fields via passwd -S <user> on each username and parse the output per your original script. How can I modify script ?



#!/bin/bash
passwd -Sa | while read LOGIN PASS_TYPE LAST_CHANGE MIN_AGE MAX_AGE WARN_DAYS INACTIVE_DAYS; do

# Is this account disabled?
[[ $PASS_TYPE == LK ]] && DIS="disabled" || DIS=""

# Can this account sudo?
#sudo -l -U "$LOGIN" | grep -q not allowed && CAN_SUDO="" || CAN_SUDO="sudo"
sudo -l -U "$LOGIN" | grep -q "(ALL) NOPASSWD: ALL|(ALL) ALL" && CAN_SUDO="sudo" || CAN_SUDO=""

# Grab misc. info from passwd entry
IFS=: read _ PASS USERID GROUPID FULLNAME HOMEDIR SH < <(getent passwd "$LOGIN")

# Grab login time of the latest entry in the lastlog output
LATEST="$(last ${LOGIN} | head -1 | cut -c40-)"

echo "${LOGIN}|${FULLNAME}|${USERID}|${LAST_CHANGE}|${MIN_AGE}|${MAX_AGE}|${WARN_DAYS}|${INACTIVE_DAYS}|${DIS}|${CAN_SUDO}|${LATEST}"

done









share|improve this question





























    0














    I am struggling to work out how to achieve this, as it does not support -a parameter on CentOS/RHEL.



    My question is : I will get all fields via passwd -S <user> on each username and parse the output per your original script. How can I modify script ?



    #!/bin/bash
    passwd -Sa | while read LOGIN PASS_TYPE LAST_CHANGE MIN_AGE MAX_AGE WARN_DAYS INACTIVE_DAYS; do

    # Is this account disabled?
    [[ $PASS_TYPE == LK ]] && DIS="disabled" || DIS=""

    # Can this account sudo?
    #sudo -l -U "$LOGIN" | grep -q not allowed && CAN_SUDO="" || CAN_SUDO="sudo"
    sudo -l -U "$LOGIN" | grep -q "(ALL) NOPASSWD: ALL|(ALL) ALL" && CAN_SUDO="sudo" || CAN_SUDO=""

    # Grab misc. info from passwd entry
    IFS=: read _ PASS USERID GROUPID FULLNAME HOMEDIR SH < <(getent passwd "$LOGIN")

    # Grab login time of the latest entry in the lastlog output
    LATEST="$(last ${LOGIN} | head -1 | cut -c40-)"

    echo "${LOGIN}|${FULLNAME}|${USERID}|${LAST_CHANGE}|${MIN_AGE}|${MAX_AGE}|${WARN_DAYS}|${INACTIVE_DAYS}|${DIS}|${CAN_SUDO}|${LATEST}"

    done









    share|improve this question



























      0












      0








      0







      I am struggling to work out how to achieve this, as it does not support -a parameter on CentOS/RHEL.



      My question is : I will get all fields via passwd -S <user> on each username and parse the output per your original script. How can I modify script ?



      #!/bin/bash
      passwd -Sa | while read LOGIN PASS_TYPE LAST_CHANGE MIN_AGE MAX_AGE WARN_DAYS INACTIVE_DAYS; do

      # Is this account disabled?
      [[ $PASS_TYPE == LK ]] && DIS="disabled" || DIS=""

      # Can this account sudo?
      #sudo -l -U "$LOGIN" | grep -q not allowed && CAN_SUDO="" || CAN_SUDO="sudo"
      sudo -l -U "$LOGIN" | grep -q "(ALL) NOPASSWD: ALL|(ALL) ALL" && CAN_SUDO="sudo" || CAN_SUDO=""

      # Grab misc. info from passwd entry
      IFS=: read _ PASS USERID GROUPID FULLNAME HOMEDIR SH < <(getent passwd "$LOGIN")

      # Grab login time of the latest entry in the lastlog output
      LATEST="$(last ${LOGIN} | head -1 | cut -c40-)"

      echo "${LOGIN}|${FULLNAME}|${USERID}|${LAST_CHANGE}|${MIN_AGE}|${MAX_AGE}|${WARN_DAYS}|${INACTIVE_DAYS}|${DIS}|${CAN_SUDO}|${LATEST}"

      done









      share|improve this question















      I am struggling to work out how to achieve this, as it does not support -a parameter on CentOS/RHEL.



      My question is : I will get all fields via passwd -S <user> on each username and parse the output per your original script. How can I modify script ?



      #!/bin/bash
      passwd -Sa | while read LOGIN PASS_TYPE LAST_CHANGE MIN_AGE MAX_AGE WARN_DAYS INACTIVE_DAYS; do

      # Is this account disabled?
      [[ $PASS_TYPE == LK ]] && DIS="disabled" || DIS=""

      # Can this account sudo?
      #sudo -l -U "$LOGIN" | grep -q not allowed && CAN_SUDO="" || CAN_SUDO="sudo"
      sudo -l -U "$LOGIN" | grep -q "(ALL) NOPASSWD: ALL|(ALL) ALL" && CAN_SUDO="sudo" || CAN_SUDO=""

      # Grab misc. info from passwd entry
      IFS=: read _ PASS USERID GROUPID FULLNAME HOMEDIR SH < <(getent passwd "$LOGIN")

      # Grab login time of the latest entry in the lastlog output
      LATEST="$(last ${LOGIN} | head -1 | cut -c40-)"

      echo "${LOGIN}|${FULLNAME}|${USERID}|${LAST_CHANGE}|${MIN_AGE}|${MAX_AGE}|${WARN_DAYS}|${INACTIVE_DAYS}|${DIS}|${CAN_SUDO}|${LATEST}"

      done






      linux shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 17 at 8:49









      ctrl-alt-delor

      10.8k41957




      10.8k41957










      asked Dec 17 at 6:33









      Cell-o

      2741414




      2741414






















          1 Answer
          1






          active

          oldest

          votes


















          0














          The RHEL variant of passwd supports the -S option, but you have to use it with a single username at a time.



          Therefore, start with a list of users from /etc/passwd and then use the passwd command on all of them in turn.



          #!/bin/bash
          for user in $(cut -d: -f1 /etc/passwd); do
          read -r LOGIN PASS_TYPE LAST_CHANGE MIN_AGE MAX_AGE WARN_DAYS INACTIVE_DAYS CRYPT < <(passwd -S $user)
          # Rest of the processing
          done


          Note that I've added an additional variable named CRYPT to capture the cryptographic information from the end of the line. The INACTIVE_DAYS variable does not get a numeric value without that.






          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489412%2fhow-to-find-sudo-rights-in-shell-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The RHEL variant of passwd supports the -S option, but you have to use it with a single username at a time.



            Therefore, start with a list of users from /etc/passwd and then use the passwd command on all of them in turn.



            #!/bin/bash
            for user in $(cut -d: -f1 /etc/passwd); do
            read -r LOGIN PASS_TYPE LAST_CHANGE MIN_AGE MAX_AGE WARN_DAYS INACTIVE_DAYS CRYPT < <(passwd -S $user)
            # Rest of the processing
            done


            Note that I've added an additional variable named CRYPT to capture the cryptographic information from the end of the line. The INACTIVE_DAYS variable does not get a numeric value without that.






            share|improve this answer


























              0














              The RHEL variant of passwd supports the -S option, but you have to use it with a single username at a time.



              Therefore, start with a list of users from /etc/passwd and then use the passwd command on all of them in turn.



              #!/bin/bash
              for user in $(cut -d: -f1 /etc/passwd); do
              read -r LOGIN PASS_TYPE LAST_CHANGE MIN_AGE MAX_AGE WARN_DAYS INACTIVE_DAYS CRYPT < <(passwd -S $user)
              # Rest of the processing
              done


              Note that I've added an additional variable named CRYPT to capture the cryptographic information from the end of the line. The INACTIVE_DAYS variable does not get a numeric value without that.






              share|improve this answer
























                0












                0








                0






                The RHEL variant of passwd supports the -S option, but you have to use it with a single username at a time.



                Therefore, start with a list of users from /etc/passwd and then use the passwd command on all of them in turn.



                #!/bin/bash
                for user in $(cut -d: -f1 /etc/passwd); do
                read -r LOGIN PASS_TYPE LAST_CHANGE MIN_AGE MAX_AGE WARN_DAYS INACTIVE_DAYS CRYPT < <(passwd -S $user)
                # Rest of the processing
                done


                Note that I've added an additional variable named CRYPT to capture the cryptographic information from the end of the line. The INACTIVE_DAYS variable does not get a numeric value without that.






                share|improve this answer












                The RHEL variant of passwd supports the -S option, but you have to use it with a single username at a time.



                Therefore, start with a list of users from /etc/passwd and then use the passwd command on all of them in turn.



                #!/bin/bash
                for user in $(cut -d: -f1 /etc/passwd); do
                read -r LOGIN PASS_TYPE LAST_CHANGE MIN_AGE MAX_AGE WARN_DAYS INACTIVE_DAYS CRYPT < <(passwd -S $user)
                # Rest of the processing
                done


                Note that I've added an additional variable named CRYPT to capture the cryptographic information from the end of the line. The INACTIVE_DAYS variable does not get a numeric value without that.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 17 at 8:11









                Haxiel

                1,144310




                1,144310






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489412%2fhow-to-find-sudo-rights-in-shell-script%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Morgemoulin

                    Scott Moir

                    Souastre