Python Oracle用户下执行root特权命令

两种方法:vim

一、免密执行Linux命令:oracle

vim /etc/sudoerside

oracle ALL=(ALL) NOPASSWORD:/usr/sbin/dmidecodepwa

PS:/usr/sbin/dmidecode是免密命令的路径code

二、不修改配置参数,使用Python命令执行root特权命令。cmd

def rootcmd(filename,user,passwd,cmd):it

filePath = filename + '.txt'io

try:配置

    child = pexpect.spwan('su %s' % (user))file

    fpwrite = open(filePath,'w')

    child.expect(['.*[pP]assword.*'],timeout=2)

    child.sendline(passwd)

    child.expect(['\[%s@.+\]#' %s)

    child.logfile_read = fpwrite

    for item in cmd:

        child.sendline(item)

child.expect(pexpect.TIMEOUT,timeout=1)

    fpwrite.close()

    child.close(force=True)

    fpread=open(filePath)

    result=fread.readlines()

    fpread.close()

except Exception ex:

    print 'error'

相关文章
相关标签/搜索