帕拉米科和须藤
我已经在网上搜索了一段时间来解决我的问题,但没有运气。
我的问题是我似乎无法让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是最近发行版中的一个常见问题,其影响不仅仅限于 paramiko。 (前几天我把一个盒子从 fedora2 升级到最新版本时遇到了这个问题。几个 ssh/sudo 管理脚本坏了)
只要你有权访问远程主机,你就可以编辑
/etc/sudoers
并注释掉该行:Defaults requiretty
详细信息请参见 sudoers 手册页< /a>:
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: