使用p.stdout.read()stty的Python脚本:标准输入:无效参数

时间:2018-10-10 17:46:01

标签: python

尝试运行使用SUDO SU的python脚本-并将控制权传递给shell以运行ksh脚本。它工作正常,但stdout给我一个我要解决的错误

通过ANSI塔或直接调用此函数:python test.py NEWPASS

#!/usr/bin/env python

'''
Python script for Oracle
Called by the following
        SYS_SYSTEM_PASSWORD_CHANGE {{PASSWD}}
'''

import os
import sys
import re
import argparse
import textwrap
import subprocess
#import commands

# RUNPROC process.
def runproc():
    print('RUNNING PASSWORD CHANGE')
    cmds = ['sudo su - ' +runas +'/orasoft/scripts/password_change.ksh ' +npass]
    p = subprocess.Popen('/bin/bash', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
    for cmd in cmds:
      print(cmd)
      p.stdin.write(cmd)
    p.stdin.close()
    print(p.stdout.read())

def get_command():

    parser = argparse.ArgumentParser(prog='PROG',formatter_class=argparse.RawDescriptionHelpFormatter,
           description=textwrap.dedent('''\
              '''))
    parser.add_argument('passwd')
    args = parser.parse_args()
    global npass
    npass = args.passwd
    print('NEW PASS:  ' +npass)

    proc = subprocess.Popen("ls -latr /etc/oratab | tr -s ' ' | cut -f3 -d' '",stdout=subprocess.PIPE, shell=True)
    global runas
    (runas, err) = proc.communicate()
    #runas = runas.decode("utf-8")
    print('RUNNING AS:  ' +runas)
    #print(runas.decode("utf-8"))

    runproc()

# -- begin fetching commands
get_command()

一切都正确传递并且脚本被正确调用,但是我不理解无效的参数错误 这是测试的输出:

CDB001@y81dev00-> python test.py NEWPASS
NEW PASS:  NEWPASS
RUNNING AS:  oracle

RUNNING PASSWORD CHANGE
sudo su - oracle
/orasoft/scripts/password_change.ksh NEWPASS

**stty: standard input: Invalid argument**
CHANGING PASSWORDS ON:     DATABASE:  CDB001  NEW_PASSWORD:  NEWPASS  RUNNING AS:  oracle

NAME
---------
CDB001

1 个答案:

答案 0 :(得分:0)

从sudo su删除破折号(-)解决了该错误。 谢谢!