用于 SSH 的库

发布于 2024-10-04 23:26:12 字数 276 浏览 4 评论 0原文

建议图书馆使用 SSH。主要要求是使用 sudo 实用程序正常操作。 我已经尝试过了,我正在遭受什么:

  • paramiko - 根本无法 sudo,在调用 STDIN 密码后尝试,但 sudo 写道,然后输入:“No ttys present”
  • pxssh - mmmmmm,非常慢,非常非常慢,尴尬的
  • 结构 - sudo 只能在理想的世界中使用,因为需要与不同的用户合作以及我需要发送密码的地方?

是否有可以与 sudo 一起使用的普通库?

counsel to the library to work with SSH. The main requirement is normal operation with the utility sudo.
I have already tried and what I am suffering:

  • paramiko - can not sudo at all, trying after a call to serve in STDIN password, but sudo wrote that then type: "No ttys present"
  • pxssh - mmmmmm, very slow, very very slow, awkward
  • fabric - can sudo only in what is an ideal world, as there is to work with different users and where i need send password ?

Have normal libraries that work with sudo, or not?

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

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

发布评论

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

评论(5

梦里寻她 2024-10-11 23:26:12

与其强制 sudo 在没有 tty 的情况下工作,为什么不让 Paramiko 为您分配一个 TTY?

Paramiko 和 Pseudo-tty 分配

Rather than force sudo to work without a tty, why not get Paramiko to allocate you a TTY?

Paramiko and Pseudo-tty Allocation

诗化ㄋ丶相逢 2024-10-11 23:26:12

我认为您正在寻找fabric

I think you are looking for fabric.

笔落惊风雨 2024-10-11 23:26:12

您可以使用“requiretty”设置将 sudo 配置为在没有真实终端的情况下工作。来自 sudoers 手册:

如果设置,sudo 将仅在用户登录到真实 tty 时运行。这将
禁止诸如“rsh somehost sudo ls”之类的内容,因为 rsh(1) 不会
分配一个tty。
因为当没有 tty 时不可能关闭 echo,
某些站点可能希望设置此标志以防止用户输入可见的
密码。默认情况下此标志处于关闭状态。

这对我来说对 paramiko 有用。根据您在做什么,您还可以查看类似 pexpect 的内容。

You can configure sudo to work without a real terminal with 'requiretty' setting. From sudoers manual:

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 site may wish to set this flag to prevent a user from entering a visible
password. This flag is off by default.

This works for me with paramiko. Depending o what are you doing, you can also look at something like pexpect.

裸钻 2024-10-11 23:26:12

我一开始在使用 pxssh 时遇到了同样的问题:速度非常慢!
这是我发现的一种让它运行得更快的方法:

#!/usr/bin/python

import pxssh
import getpass

try:
    s = pxssh.pxssh()
    s.PROMPT = "#"
    hostname = raw_input('hostname: ')
    username = raw_input('username: ')
    password = getpass.getpass('password: ')
    s.login(hostname, username, password, auto_prompt_reset=False)
    s.sendline('ls')   # run a command
    s.prompt()             # match the prompt
    print(s.before)        # print everything before the prompt.
    s.sendline('ls -l /tmp')   # run a command
    s.prompt()             # match the prompt
    print(s.before)        # print everything before the prompt.
    s.logout()
except pxssh.ExceptionPxssh as e:
    print("pxssh failed on login.")
    print(e)

关键部分是 s.login() 中的 s.PROMPT = "#"auto_prompt_reset=False
此方法要求您知道提示的模式(在我的例子中是“#”,我认为 PROMPT 属性可以设置为正则表达式)。

I had the same problem with pxssh at first: it was extremely slow!
Here is a way I found to make it run quicker:

#!/usr/bin/python

import pxssh
import getpass

try:
    s = pxssh.pxssh()
    s.PROMPT = "#"
    hostname = raw_input('hostname: ')
    username = raw_input('username: ')
    password = getpass.getpass('password: ')
    s.login(hostname, username, password, auto_prompt_reset=False)
    s.sendline('ls')   # run a command
    s.prompt()             # match the prompt
    print(s.before)        # print everything before the prompt.
    s.sendline('ls -l /tmp')   # run a command
    s.prompt()             # match the prompt
    print(s.before)        # print everything before the prompt.
    s.logout()
except pxssh.ExceptionPxssh as e:
    print("pxssh failed on login.")
    print(e)

The key part is s.PROMPT = "#" and auto_prompt_reset=False in s.login().
This method requires that you know the pattern for the prompt (in my case it is "#", I think the PROMPT attribute can be set to a regular expression).

晚雾 2024-10-11 23:26:12

我在 pxssh 上的登录速度也遇到了一些问题。我尝试使用上面引用的代码,但仍然需要 10 多秒才能登录。使用original_prompt参数为我解决了这个问题。您需要确保将 Original_prompt 设置为首次 ssh 进入计算机时看到的内容,在我的例子中以“>”结尾。

#!/usr/bin/env python

from pexpect import pxssh

host = 'hostname.domain'
user = 'username'
password = 'password'

terminal = pxssh.pxssh()
terminal.login(host, user, original_prompt='[>$]')

I also had some problems with login speed on pxssh. I tried using the code referenced above, but still was seeing 10+ seconds just to login. Using the original_prompt argument fixed the issue for me. You need to make sure to set the original_prompt to what you see when you first ssh into the machine, which in my case ended in '>'.

#!/usr/bin/env python

from pexpect import pxssh

host = 'hostname.domain'
user = 'username'
password = 'password'

terminal = pxssh.pxssh()
terminal.login(host, user, original_prompt='[>$]')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文