运行SUDO命令时Python脚本抛出TTY Sudo错误

问题描述 投票:1回答:2
def execute_cli_locally(command, timeout=CLI_EXECUTION_TIMEOUT,
                    return_output_as_string=True)

try:
    logger.info("Executing commands locally :\n%s", command)
    ssh = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)

    stdout, stderr = ssh.communicate(command)

    if ssh.returncode == 0:
        stdout = stdout.strip()
        if len(stdout) != 0:
            logger.info("Stdout :\n%s", stdout)
        return stdout

    else:
        logger.error("Local command execution failed. Error :\n%s" % stderr)
        print_response_and_exit(STATUS_FAILED,
                                "Local commands [%s] execution failed. Error :\n%s" %
                                (command, stderr))

我正在对此Python脚本执行SUDO命令,但抛出错误“ sudo:抱歉,您必须具有tty才能运行sudo”。

python python-3.x python-2.7 python-requests sudo
2个回答
0
投票
    在终端输入此:
  • sudo visudo / etc / sudoers

      找到此行:
  • 默认要求]

    并使用'#'注释掉该行,即#Defaults requiretty。

      保存并关闭文件:
  • 在vi / vim中:输入“:wq” –这意味着先按:会激活命令模式,然后输入wq并按Enter。此顺序将保存文件并退出编辑器。

    在nano中:按Ctrl + X,然后按y确认要保存更改。然后按Enter键而不更改默认文件名。


  • 0
    投票
    尝试使用]运行脚本>

    sudo -S python {script_name} {args}

    © www.soinside.com 2019 - 2024. All rights reserved.