LostJedi
LostJedi

Reputation: 31

Insert NOPASSWD string at right place in sudoers whereever it is not present

I am novice in scripts. I need to schedule a Job on multiple servers and insert NOPASSWD at right place in sudoers files. Conditions are skip the lines where PASSWD is present and wheel is present.

I am successfull with below awk to extract the lines which match my expression, but with sed, I am not able to insert or replace the required.

awk '($1 ~ "%" && !/PASSWD/ && !/wheel/) {print}' sudoers

Sed:

sed '/NOPASSWD/b; s/ALL\=\(ALL\)[[:space:]].* ALL/ALL\=\(ALL\)[[:space:]].*NOPASSWD\: ALL/g' sudoers

Not working and also does not help if the commands are different from ALL.

Existing:-

%<<group 1>>       ALL=(ALL)      /bin/su - oracle12
%<<group 2>>    ALL=(ALL)       ALL

Expected:-

%<<group 1>>       ALL=(ALL)      NOPASSWD: /bin/su - oracle12
%<<group 2>>    ALL=(ALL)       NOPASSWD: ALL

Any Help is highly appreciated. If there is any other thread which resolved similar issue, please redirect me.

Upvotes: 3

Views: 231

Answers (2)

LostJedi
LostJedi

Reputation: 31

Thanks to Gupta... The issue is resolved now.

below is the scriptlet that acheives the Job. Let me know if anyone needs explanation.

awk '($1 ~ "%" && !/PASSWD/ && !/wheel/) {$2 = $2 FS "NOPASSWD:"; print }' /etc/sudoers > /tmp/temp_sudoers

while read Line do First=echo $Line | cut -d' ' -f1; sed -i "s?^${First}.*?${Line}?g" /etc/sudoers done < /tmp/temp_sudoers

@gupta thank you again.

Upvotes: 0

Gautam
Gautam

Reputation: 1932

Your existing awk command with slight modification seems to work :

$ awk '($1 ~ "%" && !/PASSWD/ && !/wheel/) {$2 = $2 FS "NOPASSWD:"; print }' sudoers

Upvotes: 1

Related Questions