Active Topics

 


Reply
Thread Tools
Posts: 3,664 | Thanked: 1,530 times | Joined on Sep 2009 @ Hamilton, New Zealand
#1
I need some help with a little of python scripting. I'm trying to make my script to do this following.

Xterminal:

root
echo 78> /abc/abc/bca


i've tried this but nothing work.

If I run this from Xterminal as root:
root
python
import os
os.system('echo 78> /abc/abc/bca')

it works!

But if I run from script it doesnt. How can i get it to work?

The file is not changing.
How do I achieve this same effect with python?

Last edited by maxximuscool; 2010-09-04 at 08:00.
 

The Following User Says Thank You to maxximuscool For This Useful Post:
Posts: 3,664 | Thanked: 1,530 times | Joined on Sep 2009 @ Hamilton, New Zealand
#2
 

The Following User Says Thank You to maxximuscool For This Useful Post:
Posts: 11 | Thanked: 1 time | Joined on Mar 2010
#3
You can't get root privileges inside the script: it is very insecure. You should write instead
Code:
import os
os.system('echo 78 > /abc/abc/bca')
or even better
Code:
open('/abc/abc/bca', 'w').write('78')
and run this script with root privileges
 
Posts: 3,664 | Thanked: 1,530 times | Joined on Sep 2009 @ Hamilton, New Zealand
#4
Originally Posted by aeol View Post
You can't get root privileges inside the script: it is very insecure. You should write instead
Code:
import os
os.system('echo 78 > /abc/abc/bca')
or even better
Code:
open('/abc/abc/bca', 'w').write('78')
and run this script with root privileges

I've tried that, I'm trying to echo to swappiness


os.system('echo 60> /proc/sys/vm/swappiness')

You can't write to a file that only root can access.
but result is not working via python. But it working via xterminal python as root.

Last edited by maxximuscool; 2010-09-04 at 09:12.
 

The Following User Says Thank You to maxximuscool For This Useful Post:
Posts: 3,664 | Thanked: 1,530 times | Joined on Sep 2009 @ Hamilton, New Zealand
#5
How do I echo this:

echo 60> /proc/sys/vm/swappiness

in python as root?

I've tried
os.system('sudo echo 60> blah')
os.system('root ech blah')

but nothing worked
 

The Following User Says Thank You to maxximuscool For This Useful Post:
Posts: 842 | Thanked: 1,197 times | Joined on May 2010
#6
Well, you are going to have to gainroot before launching the python script; if the python script is launched with root privs, then it should work.
Try:
sudo gainroot
python myscript.py


edit:
Then, if you need to be able to launch it from a non-root terminal; something you can't run "sudo gainroot" from first, you will need to
add:
ALL ALL= NOPASSWD: /path/to/python /path/to/myscript.py

to the sudoers file. Then you can run "sudo python /path/to/myscript.py" with root privs, without having to enter your password.

Last edited by RobbieThe1st; 2010-09-04 at 10:13.
 
SubCore's Avatar
Posts: 850 | Thanked: 626 times | Joined on Sep 2009 @ Vienna, Austria
#7
to start a script as root, you could use
Code:
echo /path/to/script | sudo gainroot

or

/bin/busybox sh -c 'echo /path/to/script | sudo gainroot'
that script can be a shell script, python or anything else, as long as the executable flag (+x) is set.

(and just to mention it - there really is no point in using python if all you're doing are calls to os.system, better just use a shell script for that.)
__________________
"What we perceive is not nature itself, but nature exposed to our method of questioning."
-- Werner Karl Heisenberg
 

The Following 2 Users Say Thank You to SubCore For This Useful Post:
ossipena's Avatar
Posts: 3,159 | Thanked: 2,023 times | Joined on Feb 2008 @ Finland
#8
sh ./script.sh is faster to type than python ./script.py anyway
__________________
Want to know something?
K.I.S.S. approach:
wiki category:beginners. Browse it through and you'll be much wiser!
If the link doesn't help, just use
Google Custom Search
 
SubCore's Avatar
Posts: 850 | Thanked: 626 times | Joined on Sep 2009 @ Vienna, Austria
#9
Originally Posted by ossipena View Post
sh ./script.sh is faster to type than python ./script.py anyway
if you put
Code:
#!/bin/sh
or
Code:
#!/usr/bin/python
as first line and chmod +x the file, you have to type neither
__________________
"What we perceive is not nature itself, but nature exposed to our method of questioning."
-- Werner Karl Heisenberg
 
Posts: 3,664 | Thanked: 1,530 times | Joined on Sep 2009 @ Hamilton, New Zealand
#10
Originally Posted by SubCore View Post
to start a script as root, you could use
Code:
echo /path/to/script | sudo gainroot

or

/bin/busybox sh -c 'echo /path/to/script | sudo gainroot'
that script can be a shell script, python or anything else, as long as the executable flag (+x) is set.

(and just to mention it - there really is no point in using python if all you're doing are calls to os.system, better just use a shell script for that.)
I know about shell but I want to call out the status-menu icon as well so shell script can't do that. And I'm wanting it to just work from python because it is easier to manage later on.

Also knowing how to achieve this would also help my next script.
 
Reply


 
Forum Jump


All times are GMT. The time now is 11:02.