ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to set up some scripts to modify a certain command alias in the sudoers file. My basic setup will have a certain set of commands. I want to be able to add additional ones based on certain conditions. For example, if this server is one of my hadoop systems, I want to add access to hadoop. Another example is if this system is an HP system, I want to add access to hpacucli. My basic command alias is:
I want to use sed or perl to search for the BB_C command alias, find the last line, and then add the additional command. Any suggestions would be greatly appreciated.
I have to say that editing sudoers like that may not be a good idea. One syntax error and you lose all the other settings and you break sudo. That's why the visudo command parses it for errors before saving it.
But, it's your machine of course so you can do as you wish.
It may be much easier to have all the entries on one line rather than breaking into separate lines (it would certainly be easier to sed).
Hi Sorry for the late response. My code is at work.
I agree in general about sudoers, but I am doing this as part of my kickstarting a new server. If I manage to "mess it up," I'll find that out, change the script to remove the error, and re-kickstart the server as needed. That being said, here is what I have in perl so far:
Code:
#!/usr/bin/perl -w
use strict;
open(SUDO, "/etc/sudoers") or die("Could not open sudoers file!\n");
while(<SUDO>) {
chomp($_);
if ($_ =~ /Cmnd_Alias\tBB_C/) {
if ($_ =~ /\\$/) {
print $_\n";
}
}
}
close(SUDO);
What I want to do when I find the line with BB_C and ending with a "\" is check the next line. I want to see if it also ends with a "\." If not, add my code to that line. If so, check the next line. I know my base installation script will create a command alias for BB_C with at least one continuation line. So, that is what I am keying on. I'm just not sure how to search for the "next" line once I find the BB_C line. I guess I could introduce another variable, $found?
The print SUDO lines don't actually produce any new output in the sudoers file. If I remove "SUDO" from the line it prints correctly to the standard output. I think I need to figure out how to replace the current line with the contents of $line and/or add a new line after the current position.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.