0

I am using ansible in order to read /etc/sudoers file to display which users have root permission on related server.

This is the .sh file

    #!/bin/bash

    username=`sudo cat /etc/sudoers | grep "NOPASSWD: ALL" | grep -v ^#`
    hostname=`sudo hostname`

    echo "xyz | $username | $hostname"

This is the another .sh file that creates output I execute this one first.

 #!/bin/bash

 /bin/ansible-playbook /etc/ansible/scripts/sudoerscheck.yaml >| 
 /root/script/sudoers.dat
 /bin/cat /root/script/sudoers.dat | grep xyz | awk -F '|' '{ print $2":"$3 
 }' | awk -F '\"' '{ print $1 }' >| /root/script/sudoers.txt

ansible output down below;

 ok: [test1] => {
"changed": false, 
"shell_result.stdout_lines": [
    "xyz | oracle ALL=(ALL)       NOPASSWD: ALL", 
    "cuser ALL=(ALL) NOPASSWD: ALL", 
    "dogfish        ALL=(ALL)       NOPASSWD: ALL | test1"
]
}


ok: [test2] => {
  "changed": false,
  "shell_result.stdout_lines": [
    "xyz |  | test2"
   ]
 }
  ok: [test3] => {
  "changed": false,
  "shell_result.stdout_lines": [
    "xyz | ADMINS \tALL=(ALL)\tNOPASSWD: ALL",
    "oracle        ALL=(ALL)       NOPASSWD: ALL | test3

Could you please help how do I get the output down below; And the user names are changeable

 test1 oracle cuser dogfish
 test2 NULL
 test3 oracle ADMINS

3 Answers 3

1

This is working.

#!/bin/bash
OUTPUT=$(cat /etc/passwd | grep -v nologin | grep -v root | awk -F: '{ print 
 $1 }')
block1=`hostname`
while read -r line
do
if [[ `sudo -l -U $line | grep NOPASSWD` ]];then
    echo $block1 $line 
else
    echo $block1 $line 
fi
done <<< "$OUTPUT"
0

You can do it like below:-

  1. first line of script will print line 1 and sed will extract out string under [] and stored in param1

  2. Second line of script will print 4th line and awk will print column 3 and stored in variable param2

  3. 3rd Line of script will print 5th line and 4th line of script will print 6th line and awk will print column 1 and sed will remove the " from it

       param1=$(head -1 sudoers.txt | sed  's/ok: \[//g;s/] => {//g')
       param2=$(head -4 sudoers.txt | tail -1 | awk '{print $3}')
       param3=$(head -5 sudoers.txt | tail -1 | awk '{print $1}' | sed 's/"//g')
       param4=$(head -6 sudoers.txt | tail -1 | awk '{print $1}' | sed 's/"//g')
    
       echo '$param1  $param2  $param3  $param4' > sudoers.txt 
    
1
  • Sorry @Abhiijt, I edited the question because I forgot to share some informations, I get multiple different output from servers some of output contain 3 user some of them 2 some of them is null so it should be in a loop statement I guess. Because some of the sudo file has 6 users in it. Apr 9, 2018 at 21:00
0

If the output elements which you have mentioned is static(if you are confirmed about it wont change) means . You can use below command directly

Here file.txt is ansible output

 egrep -o "test1|oracle|cuser|dogfish" file.txt | sort | uniq | tac | tr  "\n" " " | awk '{$5=$3;$3=$4;$4="";print $0}'| sed -r "s/\s+/ /g"

output

test1 oracle cuser dogfish
1
  • no it is not static it can be differs base on server. Apr 9, 2018 at 21:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .