logging as root using python script

R

Raghul

Hi
Is it possible to login as a root in linux using python script?
What I need is when I execute a script it should login as root and
execute my command and logout from root to my existing account. IS is
possible?

Thanx in advance.
 
M

Martin Franklin

Luis said:
Raghul wrote:




I'm not sure of what you need, so I'll assume your *whole* .py script
needs root priviledges. In this case, you can configure sudo(8) or use
su(1).

For example, the script below does nothing special:

| #!/usr/bin/env python
|
| print "Hello world!"

You can run it with higher priviledges if you use sudo(8):

$ chmod 755 hello.py
$ sudo ./hello.py

Or you can use su(1):

$ su - root -c ./hello.py

You can configure sudo(8) to not prompt for any password, BTW.

Cheers!

another alternative would be setuid for more on this (and why it could
be a bad idea) google...

Martin
 
L

Luis Bruno

Raghul said:
What I need is when I execute a script it should login as root and
execute my command and logout from root to my existing account.

I'm not sure of what you need, so I'll assume your *whole* .py script
needs root priviledges. In this case, you can configure sudo(8) or use
su(1).

For example, the script below does nothing special:

| #!/usr/bin/env python
|
| print "Hello world!"

You can run it with higher priviledges if you use sudo(8):

$ chmod 755 hello.py
$ sudo ./hello.py

Or you can use su(1):

$ su - root -c ./hello.py

You can configure sudo(8) to not prompt for any password, BTW.

Cheers!
 
L

Luis Bruno

Martin said:
another alternative would be setuid

I also thought about making the script setuid root, but I'm under the
impression that Linux (at least) won't honor the suid bit on a script.
That's from memory though.

Cheers!
 
M

max.derkachev

Unixish system won't let You execute a setuid script with the setuid
privileges. Only real machine code can be executed so.
But of course, there are workarounds. When, say, you have a perl script
with setuid bit set, and sperl (setuid root perl) is installed, the
perl interpreter choses sperl to interpret your script (sperl makes it
impossible to execute a symlink attak, which is the reason why UNIX
does not leverage privileges for setuid scripts).
So, You may write a perl suid wrapper for your python script, and let
sperl execute it.
 
P

Paul Casteels

Raghul said:
Hi
Is it possible to login as a root in linux using python script?
What I need is when I execute a script it should login as root and
execute my command and logout from root to my existing account. IS is
possible?

Thanx in advance.
Hi,

You can compile the small .c program and setuid it's executable.
Change the path to your .py script of course.


#include <sys/types.h>
#include <unistd.h>

int main(int argc,char **argv)
{
char *c1[]={"/tmp/Chg.py\0",'\0'};

setuid(0);
seteuid(0);
execv(c1[0],c1);
return 1;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,178
Members
47,811
Latest member
Adisty

Latest Threads

Top