简体   繁体   English

Python子进程sudo返回错误:错误:['sudo:抱歉,您必须具有tty才能运行sudo \\ n']

[英]Python subprocess sudo returns error: ERROR: ['sudo: sorry, you must have a tty to run sudo\n']

Here is my code: 这是我的代码:

import subprocess

HOST = 'host_name'
PORT = '111'
USER = 'user_name'
CMD = 'sudo su - ec2-user; ls'

process = subprocess.Popen(['ssh','{}@{}'.format(USER, HOST),
                        '-p', PORT, CMD],
                       shell=False,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)

result = process.stdout.readlines()

if not result:
    print "Im an error"
    err = process.stderr.readlines()
    print('ERROR: {}'.format(err))
else:
    print "I'm a success"
    print(result)

When I run this I receive the following output in my terminal: 运行此命令时,我在终端中收到以下输出:

dredbounds-computer: documents$ python terminal_test.py
Im an error
ERROR: ['sudo: sorry, you must have a tty to run sudo\n']

I've tried multiple things but I keep getting that error "sudo: sorry, you must have a tty to run sudo". 我已经尝试了多种方法,但始终收到该错误“ sudo:对不起,您必须有一个tty才能运行sudo”。 It works fine if I just do it through the terminal manually, but I need to automate this. 如果我只是通过终端手动完成它,则可以正常工作,但是我需要使其自动化。 I read that a workaround might be to use '-t' or '-tt' in my ssh call, but I haven't been able to implement this successfully in subprocess yet (terminal just hangs for me). 我读到一种解决方法可能是在ssh调用中使用'-t'或'-tt',但是我还不能在子进程中成功实现这一点(终端只是为我挂起了)。 Anyone know how I can fix my code, or work around this issue? 有人知道我该如何解决我的代码,或者解决此问题? Ideally I'd like to ssh, then switch to the sudo user, and then run a file from there (I just put ls for testing purposes). 理想情况下,我想使用ssh,然后切换到sudo用户,然后从那里运行文件(我只是将ls用于测试目的)。

sudo is prompting you for a password, but it needs a terminal to do that. sudo提示您输入密码,但是它需要一个终端来执行。 Passing -t or -tt provides a terminal for the remote command to run in, but now it is waiting for you to enter a password. 传递-t-tt为运行远程命令提供了一个终端,但是现在它正在等待您输入密码。

process = subprocess.Popen(['ssh','-tt', '{}@{}'.format(USER, HOST),
                          '-p', PORT, CMD],
                          shell=False,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE,
                          stdin=subprocess.PIPE)
process.stdin.write("password\r\n")

Keep in mind, though, that the ls doesn't run until after the shell started by su exits. 但是请记住, ls直到su启动的shell退出后才运行。 You should either log into the machine as ec2-user directly (if possible), or just use sudo to run whatever command you want without going through su first. 您应该直接以ec2-user身份登录到计算机(如果可能),或者仅使用sudo运行所需的任何命令,而无需先通过su

You can tell sudo to work without requiring a password. 您可以告诉sudo无需密码即可工作。 Just add this to /etc/sudoers on the remote server host_name . 只需将其添加到远程服务器host_name上的/etc/sudoers

user ALL = (ec2-user) NOPASSWD: ls

This allows the user named user to execute the command ls as ec2-user without entering a password. 这允许名为userec2-user身份执行命令ls ,而无需输入密码。

This assumes you change your command to look like this, which seems more reasonable to me: 假设您将命令更改为如下所示,这对我来说似乎更合理:

CMD = 'sudo -u ec2-user ls'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM