maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   General (https://talk.maemo.org/forumdisplay.php?f=7)
-   -   Need help with this command (https://talk.maemo.org/showthread.php?t=61693)

maxximuscool 2010-09-04 07:36

Need help with this command
 
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?

maxximuscool 2010-09-04 08:00

Re: Need help with this command
 
Anyone know where I did wrong?

aeol 2010-09-04 08:11

Re: Need help with this command
 
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

maxximuscool 2010-09-04 09:09

Re: Need help with this command
 
Quote:

Originally Posted by aeol (Post 806919)
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.

maxximuscool 2010-09-04 09:31

Re: Need help with this command
 
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

RobbieThe1st 2010-09-04 10:08

Re: Need help with this command
 
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.

SubCore 2010-09-04 10:38

Re: Need help with this command
 
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.)

ossipena 2010-09-04 10:48

Re: Need help with this command
 
sh ./script.sh is faster to type than python ./script.py anyway ;)

SubCore 2010-09-04 12:19

Re: Need help with this command
 
Quote:

Originally Posted by ossipena (Post 806997)
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 ;)

maxximuscool 2010-09-04 14:44

Re: Need help with this command
 
Quote:

Originally Posted by SubCore (Post 806988)
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.

maxximuscool 2010-09-04 14:47

Re: Need help with this command
 
#!/usr/bin/python

import gtk
import gobject
import hildondesktop
import os
import sys
import stat

class Swappiness(hildondesktop.StatusMenuItem):
def __init__(self):

hildondesktop.StatusMenuItem.__init__(self)

#os.system('echo /usr/lib/hildon-desktop/Swappiness.py | root')
os.system('sudo chmod +x /usr/lib/hildon-desktop/Swappiness.py')

os.system('echo 30> /proc/sys/vm/swappiness')
#os.system('sudo chmod -R echo 30 > /proc/sys/vm/swappiness')
#swap_file = open('/proc/sys/vm/swappiness', 'w').write('30')
icon_theme = gtk.icon_theme_get_default()
pixbuf = icon_theme.load_icon("Swappiness", 22, gtk.ICON_LOOKUP_NO_SVG)
# hide icon after 1 sec
#gobject.timeout_add(1000, self.set_status_area_icon, None)
self.set_status_area_icon(pixbuf)

#label = gtk.Label("Show Me")
#self.add(label)
#self.show_all()



hd_plugin_type = Swappiness

This is my little simple script but nothing worked since I can't gain root access from the command os.system

maxximuscool 2010-09-04 15:48

Re: Need help with this command
 
I want to make this work at reboot. And also place an Icon as well.

RobbieThe1st 2010-09-05 04:21

Re: Need help with this command
 
As said before, you can't gain root access inside the script. The script has to be run as root!

maxximuscool 2010-09-05 08:11

Re: Need help with this command
 
Quote:

Originally Posted by RobbieThe1st (Post 807584)
As said before, you can't gain root access inside the script. The script has to be run as root!

Show me how to do that? from shell batch or something automatically without going through the xterminal and run it manually


All times are GMT. The time now is 22:19.

vBulletin® Version 3.8.8