Created
June 3, 2011 06:29
-
-
Save rochacbruno/1005957 to your computer and use it in GitHub Desktop.
Executar comando do OS (unix) como root usando sudo no Python - run a command as root using sudo from Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
echo = subprocess.Popen(['echo','123'], | |
stdout=subprocess.PIPE, | |
) | |
sudo = subprocess.Popen(['sudo','-S','iptables','-L'], | |
stdin=echo.stdout, | |
stdout=subprocess.PIPE, | |
) | |
end_of_pipe = sudo.stdout | |
print "Iptables Chains:" | |
print end_of_pipe.read() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
echo = subprocess.Popen(['echo','123'], | |
stdout=subprocess.PIPE, | |
) | |
sudo = subprocess.Popen(['sudo','-S','iptables','-L'], | |
stdin=echo.stdout, | |
stdout=subprocess.PIPE, | |
) | |
end_of_pipe = sudo.stdout | |
print "Iptables Chains:" | |
print end_of_pipe.read() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def mypass(): | |
mypass = '123' | |
return mypass | |
def command(cmd): | |
text = os.popen( "echo %s | sudo -S %s" % (mypass(),cmd) ).read() | |
return text | |
print command('iptables -L') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def mypass(): | |
mypass = '123' | |
return mypass | |
def command(cmd): | |
os.popen( "echo %s | sudo -S %s" % (mypass(),cmd) ) | |
f = open("log.txt", "r") | |
text = f.read() | |
f.close() | |
return text | |
command('iptables -L > log.txt') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
def mypass(): | |
mypass = '123' #or get the password from anywhere | |
return mypass | |
echo = subprocess.Popen(['echo',mypass()], | |
stdout=subprocess.PIPE, | |
) | |
sudo = subprocess.Popen(['sudo','-S','iptables','-L'], | |
stdin=echo.stdout, | |
stdout=subprocess.PIPE, | |
) | |
end_of_pipe = sudo.stdout | |
print "Password ok \n Iptables Chains %s" % end_of_pipe.read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
waaa, is realy easy, ths;)