帕拉米科和须藤

发布于 2024-11-03 02:05:35 字数 1364 浏览 1 评论 0原文

我已经在网上搜索了一段时间来解决我的问题,但没有运气。

我的问题是我似乎无法让 sudo 命令与 paramiko 一起运行。

起初,我尝试简单地使用 SSHClient().exec_command(command) 函数来执行 sudo 命令,但是出现错误:

sudo: sorry, you must have a tty to run sudo

然后,按照这篇文章的建议: 如何使用 Paramiko 制作 sudo 命令,我尝试过这个:

#self._ssh is the SSHClient object    
self._ssh.invoke_shell().exec_command(command)

但是,大多数时候,这只是给了我:

File "pipelines/load_instance/ssh_tools.py", line 71, in executeCommand
    stdin, stdout, stderr = self._ssh.invoke_shell().exec_command(command)
  File "<absolute_path>/paramiko/channel.py", line 213, in exec_command
    self._wait_for_event()
  File "<absolute_path>/paramiko/channel.py", line 1084, in _wait_for_event
    raise e
paramiko.SSHException: Channel closed.

另一个,我不得不说,40%的时间,我得到这个:

File "<absolute_path>/paramiko/client.py", line 291, in connect
    sock.connect(addr)
File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused

作为旁注,我试图在 Amazon Web Services EC2 实例上运行这些 sudo 命令,所以我'我很困惑为什么我会收到连接被拒绝错误,因为每次我运行此代码时它都是在一个全新的实例上...

另外,我正在使用 paramiko 1.7.6。

I've been searching the nets for some time now for a solution to my problem, and no luck.

My issue is that I can't seem to get sudo commands to run with paramiko.

At first, I tried to simply use the SSHClient().exec_command(command) function to execute a sudo command, but this errors:

sudo: sorry, you must have a tty to run sudo

Then, following the advice of this post: How to make a sudo command using Paramiko, I tried this:

#self._ssh is the SSHClient object    
self._ssh.invoke_shell().exec_command(command)

However, most of the time, this just gives me:

File "pipelines/load_instance/ssh_tools.py", line 71, in executeCommand
    stdin, stdout, stderr = self._ssh.invoke_shell().exec_command(command)
  File "<absolute_path>/paramiko/channel.py", line 213, in exec_command
    self._wait_for_event()
  File "<absolute_path>/paramiko/channel.py", line 1084, in _wait_for_event
    raise e
paramiko.SSHException: Channel closed.

The other, I'd have to say, 40% of the time, I get this:

File "<absolute_path>/paramiko/client.py", line 291, in connect
    sock.connect(addr)
File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused

As a side note, I'm attempting to run these sudo commands on an Amazon Web Services EC2 instance, so I'm confused as to why I would get the Connection Refused error, since each time I run this code it's on a fresh new instance...

Also, I'm using paramiko 1.7.6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

z祗昰~ 2024-11-10 02:05:35

这是最近发行版中的一个常见问题,其影响不仅仅限于 paramiko。 (前几天我把一个盒子从 fedora2 升级到最新版本时遇到了这个问题。几个 ssh/sudo 管理脚本坏了)

只要你有权访问远程主机,你就可以编辑 /etc/sudoers 并注释掉该行: Defaults requiretty

详细信息请参见 sudoers 手册页< /a>:

要求

如果设置,sudo 将仅在以下情况下运行
用户登录到真实的 tty。这
将不允许诸如“rsh
somehost sudo ls" 因为 rsh(1) 确实
不分配tty。因为它不是
可以在存在时关闭回声
不存在 tty,某些网站可能带有
设置此标志以阻止用户
输入可见的密码。这
标志默认关闭。

This is a common problem on recent distributions and impacts more than just paramiko. (I ran into it the other day when I upgraded a box from fedora2 to latest. Several ssh/sudo management scripts broke)

As long as you have access to the remote host, you can edit /etc/sudoers and comment out the line: Defaults requiretty

Details in the sudoers man page:

requiretty

If set, sudo will only run when the
user is logged in to a real tty. This
will disallow things like "rsh
somehost sudo ls" since rsh(1) does
not allocate a tty. Because it is not
possible to turn off echo when there
is no tty present, some sites may with
to set this flag to prevent a user
from entering a visible password. This
flag is off by default.

离不开的别离 2024-11-10 02:05:35
stdin, stdout, stderr = client.exec_command(cmd,  get_pty=True)
stdin, stdout, stderr = client.exec_command(cmd,  get_pty=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文