Built-in Sudo for Ruby Command-Line Tools
A simple Ruby method to automatically re-run your script with sudo if it needs root access.
I was looking through my gists today on GitHub,
and decided I’d do a couple of posts on some of the pieces of code I’ve put up
there. The first of which is the sudome Ruby method.
Ever written a command-line tool in Ruby that requires root access for one
reason or another? The simplest way to achieve this is to have the end user call
the command via sudo. It’s not the most elegant solution there is, but it
works.
A more elegant solution might be what the
Fink Project is doing with their fink command.
It doesn’t need to be run via sudo, as it calls it within itself. Meaning that
when you run fink, you’ll be prompted to type your password, just as if you
had used sudo. Some might argue that this is not good practice, and they are
probably right. But it all depends on the details of what you’re doing.
A while back I was working on something which the best solution was to make sure the tool always runs as root. To get identical functionality as Fink, I wrote the very simple method shown below:
def sudome
if ENV["USER"] != "root"
exec("sudo #{ENV['_']} #{ARGV.join(' ')}")
end
endSimply call sudome as early as possible in your code. If needed it will re-run
your script with sudo, requiring the user to type his password, at which point
your script then has full root access to the system.
Source: Original Gist on GitHub