Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old November 19th, 2003, 07:21 AM
Tank's Avatar
Tank Tank is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 Tank User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Changing users in Python

I'm writing a program (gui, PyGTK), and I need to execute a 'date' command from that program. Of course, 'date' must be ran as root.

So, I guess I'll need to ask the user for the root password (easy enough). But, how do pass that to an su command? Or, is there an easier way to switch users in a python script? I don't want to use sudo, cause I'm not sure that it will be setup on the client PCs. And, I'd prefer not to use xsu (again, cause I'm not sure it will be on the client pcs).

Any help will be appreciated.

Thanks,

Reply With Quote
  #2  
Old November 19th, 2003, 07:43 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
We'll i'm a little noob boy when it comes to *nix but as for passing your commands all you need is the os.system('command') function to execute simple commands.

I say simple because as soon as you want to get output back from the command line it's much easier to use the commands module!

http://www.python.org/doc/current/l...e-commands.html

Just out of interest what does 'date' do?

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old November 19th, 2003, 07:52 AM
Tank's Avatar
Tank Tank is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 Tank User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Re: Changing users in Python

It sets the system date and time. Like I said, you need to be root to do it, so simply calling 'date' using os.system won't work.

Reply With Quote
  #4  
Old November 19th, 2003, 08:11 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
So, I guess I'll need to ask the user for the root password (easy enough).


Sorry i got the impression you could pass the root password to the command line to give your user access?

Like i said i'm new to *nix but i'm sure there must be some command that lets the user temp log in as root to preform a restricted comamnd.

On Mandrake linux for example you can access certain restricted things if you enter the root password when prompted too

Mark.

Reply With Quote
  #5  
Old November 19th, 2003, 09:31 AM
Hartmann's Avatar
Hartmann Hartmann is offline
chown python:users\ /world
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Texas Tech
Posts: 95 Hartmann User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
I am fairly sure you can put the 'su' command can be used in the os.command function.

I am looking at some other ways to do this. I'll post back soon.

Reply With Quote
  #6  
Old November 19th, 2003, 10:27 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
You could use "su -c command" but then you'd need a way of feeding the root password into su on STDIN. There may be a way to do this with more raw access to the shell than system allows, but I don't know of it. That or you could use sudo to simply change the access to date on the user's system.

Reply With Quote
  #7  
Old November 20th, 2003, 09:34 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
I don't know if you've fond a better way, or already discarded/discovered the method I'm going to propose, but this is how I'm doing it, when I want to execute some python code as root:
Code:
if not os.geteuid() == 0:
  #os.execlp("sudo", "sudo", *sys.argv)
  os.execlp("su", "su", "-c", " ".join(sys.argv))

You said you didn't know if 'sudo' would be okay, but that's how I use it. 'su' will work as well, though.

Reply With Quote
  #8  
Old November 20th, 2003, 09:58 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
But surely that code requires user input when su is executed before it can complete, for the user to put in the root password?

Reply With Quote
  #9  
Old November 20th, 2003, 05:19 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Oh, didn't read that part.

Reply With Quote
  #10  
Old November 21st, 2003, 05:14 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
So for that reason, I think you either need a way of being more interactive with external commands (there must be a way, surely), or you need to use sudo, which requires doing a little bit of setup work on the user's machine.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Changing users in Python


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway