[Tutor] Check if root

James jtp at nc.rr.com
Sat Sep 29 19:57:57 CEST 2007


I've used something like this successfully in the past:

import commands
if commands.getoutput( "whoami" ) != "root":
	sys.exit( "Must be root!" )

Hope this helps.

.james

On Sep 28, 2007, at 7:31 PM, Robert Jackson wrote:

> I'm trying to write a function that checks to see if the user that
> is running the python script is 'root' (I'm obviously running this
> Python program on Linux).
>
>
>
> Using os.system(), I have done something like this:
>
>
>>>> import os
>
>>>> os.system("whoami")
>
> robert
>
> 0
>
>>>>
>
>
>
> If I try to assign the output of this snippet of code to a variable,
> the variable ultimately ends up holding "0" and not the username.
>
>
>
> I have seen some examples on Google where some individuals have  
> suggested something like this:
>
> user=os.system("whoami")
> if user is not "root":
>     print "You aren't root.  Goodbye."
>     sys.exit()
>
> But that isn't going to work, for obvious reasons (user holds 0,  
> and not the username).  How do I get around this problem?
>
> Robert
>
>
>
>
>
>
> ______________________________________________________________________ 
> ______________
> Moody friends. Drama queens. Your life? Nope! - their life, your  
> story. Play Sims Stories at Yahoo! Games.
> http://sims.yahoo.com/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list