How to make python check if running as root?

Need help with C, C++, perl, python, etc?

How to make python check if running as root?

Postby frenchninja » 2007-01-26 01:52

Hi,

Wondering whether there's a string of code for python that checks if the script has been launched as root or not. I know in bash one can do something like this:

Code: Select all
        if [ `id -u` != "0" ]; then
   echo "Sorry, you are not root."
   exit 0


but is there an equivalent in Python? Cheers
User avatar
frenchninja
 
Posts: 193
Joined: 2006-12-30 12:17

Postby ajdlinux » 2007-01-26 02:08

Code: Select all
import os,sys
if os.geteuid != 0:
    print "sorry, you need to run this as root"
    sys.exit(1)
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby frenchninja » 2007-01-26 02:19

Perfect! I had imported os but not sys and I didn't know the 'os.geteuid' command.

Thanks heaps!

Edit: I had to edit the 'geteuid' line and add brackets otherwise it wouldn't even open for root, as follows:

Code: Select all
if os.geteuid() != 0:


Cheers again!
User avatar
frenchninja
 
Posts: 193
Joined: 2006-12-30 12:17

Postby ajdlinux » 2007-01-26 02:41

frenchninja wrote:Perfect! I had imported os but not sys and I didn't know the 'os.geteuid' command.

Thanks heaps!

Edit: I had to edit the 'geteuid' line and add brackets otherwise it wouldn't even open for root, as follows:

Code: Select all
if os.geteuid() != 0:


Cheers again!


Ah, sorry, I forgot that geteuid is a function and not an attribute (ie. a variable), so the parentheses are needed to actually call it.
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby Lavene » 2007-01-26 05:44

Just for the sake of diversity: I use the output of "whoami"
Code: Select all
currentUser = commands.getoutput("whoami")
if currentUser != 'root':
    print "You need to be root!"
    sys.exit()

I like it better because you can use check which user are running the script in clear text so it's more versatile than checking the uid...

Tina
Lavene
Site admin
 
Posts: 5096
Joined: 2006-01-04 04:26
Location: Oslo, Norway

Postby frenchninja » 2007-01-26 07:20

Thanks Tina!
User avatar
frenchninja
 
Posts: 193
Joined: 2006-12-30 12:17

Postby thamarok » 2007-01-26 13:29

Lavene wrote:Just for the sake of diversity: I use the output of "whoami"
Code: Select all
currentUser = commands.getoutput("whoami")
if currentUser != 'root':
    print "You need to be root!"
    sys.exit()

I like it better because you can use check which user are running the script in clear text so it's more versatile than checking the uid...

Tina


And if the user was so crazy to "rename" the root account? Checking "root" wouldn't help here.
thamarok
 

Postby ajdlinux » 2007-01-26 22:35

thamarok wrote:
Lavene wrote:Just for the sake of diversity: I use the output of "whoami"
Code: Select all
currentUser = commands.getoutput("whoami")
if currentUser != 'root':
    print "You need to be root!"
    sys.exit()

I like it better because you can use check which user are running the script in clear text so it's more versatile than checking the uid...

Tina


And if the user was so crazy to "rename" the root account? Checking "root" wouldn't help here.


Crazy users like who? ;)

Using the output of whoami would however be useful if checking for a user account that could have any UID, so anything other than root.
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby Lavene » 2007-01-27 05:53

thamarok wrote:And if the user was so crazy to "rename" the root account? Checking "root" wouldn't help here.

He he... I know of exactly *one* person who have done that. Guess who? ;)

I would say it come in under the same category as renaming directories and then complain about being unable to install stuff. Renaming root is so unusual that I couldn't care less about making my program/ scripts handle it...

Tina
Lavene
Site admin
 
Posts: 5096
Joined: 2006-01-04 04:26
Location: Oslo, Norway

Postby thamarok » 2007-01-27 11:07

Lavene wrote:..I know of exactly *one* person who have done that. Guess who? ;) [..]

Who are you watching at?! :lol:
thamarok
 


Return to Programming

Who is online

Users browsing this forum: No registered users and 5 guests

fashionable