【问题标题】:Python Script throwing TTY Sudo error when running SUDO Command运行 SUDO 命令时 Python 脚本抛出 TTY Sudo 错误
【发布时间】:2020-05-15 08:51:04
【问题描述】:
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”。

【问题讨论】:

  • 你试过ssh -t user@example.com sudo命令/path/to/file,即使用-t开关吗?
  • 是的,我也尝试使用'-t',但它抛出了同样的错误
  • 你能粘贴输出吗:sudo cat /etc/sudoers | grep 要求
  • Defaults requiretty # 已更改,以便能够在没有 tty 的情况下使用 sudo。见上面的requiretty。默认值:consul !requiretty

标签: python python-3.x python-2.7 python-requests sudo


【解决方案1】:

尝试使用

运行您的脚本

sudo -S python {script_name} {args}

在某些情况下它对我有用。

【讨论】:

    【解决方案2】:
    1. 在终端中输入:

    sudo visudo /etc/sudoers

    1. 找到这一行:

    默认要求

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

    1. 保存并关闭文件:

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

    在 nano 中:按 Ctrl+X,然后按 y 确认您要保存更改。然后在不更改默认文件名的情况下按 Enter。

    【讨论】:

    • 它成功了..你是天才..有没有办法在评论行中做到这一点,因为我不应该能够在生产环境中更改该文件
    • 已知一些 Linux 发行版将此作为默认配置。 RedHat 最近刚刚从 Fedora 和 REHL 中删除了这个。如果生产环境发生错误,那么我建议您发布另一个问题,其中包含有关您的生产环境的详细信息,以便该问题对正在寻找解决方案的其他人有所帮助。
    猜你喜欢
    • 2012-12-22
    • 2019-12-23
    • 2015-12-17
    • 1970-01-01
    • 2023-03-26
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多