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'

相關文章
相關標籤/搜索