在python命令行执行sudo命令

def test():    
    sudoPassword = 'test'
    command = '/opt/lampp/lampp stopmysql'
    str = os.system('echo %s|sudo -S %s' % (sudoPassword, command))   
    print str

 

或:

(status, result)=commands.getstatusoutput('echo %s| sudo -S %s' %(PASSWORD,cmd))

 

转载于:https://www.cnblogs.com/xiyuan2016/p/10143963.html

你可能感兴趣的:(在python命令行执行sudo命令)