run a command as root using sudo from Python:

 1 import subprocess
2
3 def mypass():
4 mypass = '123' #or get the password from anywhere
5 return mypass
6
7 echo = subprocess.Popen(['echo',mypass()],
8 stdout=subprocess.PIPE,
9 )
10
11 sudo = subprocess.Popen(['sudo','-S','iptables','-L'],
12 stdin=echo.stdout,
13 stdout=subprocess.PIPE,
14 )
15
16 end_of_pipe = sudo.stdout
17
18 print "Password ok \n Iptables Chains %s" % end_of_pipe.read()

[via]

相关文章:

  • 2022-01-11
  • 2021-04-23
  • 2021-06-15
  • 2022-02-12
  • 2021-12-03
  • 2021-12-03
  • 2021-11-28
猜你喜欢
  • 2021-09-28
  • 2022-12-23
  • 2022-01-28
  • 2022-01-18
  • 2022-12-23
  • 2022-02-17
  • 2021-06-11
相关资源
相似解决方案