Python - 不使用非内置模块的带密码的 ssh 到外国计算机

发布于 2024-12-09 20:23:30 字数 1079 浏览 0 评论 0原文

在你对此投反对票之前,我要说的是,我已经阅读了很多关于这个主题的问题,但还没有找到答案。尽管在“尼尔”的回答中,这个最接近我需要的东西。

使用 Python 进行 SSH 的最简单方法是什么?

我想找到一种使用 ssh 登录另一台主机、提供密码、而不导入非标准 python 模块的方法。然后我需要能够使用 ssh 连接发送多个命令。我需要这样做,因为我需要在许多封闭系统上实现一些自动化,而我无法在这些系统上获取新模块(无法访问互联网)。

参考问题的答案是:

如果你想避免任何额外的模块,你可以使用子进程 要运行的模块

ssh [主机] [命令] 并捕获输出。

尝试如下:

process = subprocess.Popen("ssh example.com ls", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 输出,stderr = process.communicate() status = process.poll() 打印输出 处理 有了用户名和密码,您可以使用子进程进行交互 ssh 进程,或者您可以在服务器上安装公钥 避免出现密码提示。

这听起来不错,但我不太理解这一点,无法进一步深入。有人可以帮助我迈出一小步吗?

(1) log in to ssh
(2) provide the password when its prompted
(3) issue an ls command and display the result

如果我能获得密码输入,我就可以完成其余的自动化工作。

PS:我尝试使用expect,但系统上没有TCL,所以请不要告诉我使用它!如果你知道我可以打电话来帮助 python 的其他东西,我很乐意尝试它,只要它可能默认内置到 Fedora Enterprise Linux 系统中。

Before you down vote this, let me say that I've read ALOT of questions on this subject on SO and haven't found the answer. This one has the closest thing to what I need though on the answer by "Neil".

What is the simplest way to SSH using Python?

I would like to find a way to log into another host using ssh, providing a password, without importing non-standard python modules. I would then need to be able to send multiple commands using the ssh connection. I need to do this because I need to automate something on a lot of closed systems that I cannot get new modules on to (non internet accessible).

The answer in the referenced question is:

If you want to avoid any extra modules, you can use the subprocess
module to run

ssh [host] [command] and capture the output.

Try something like:

process = subprocess.Popen("ssh example.com ls", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output,stderr = process.communicate() status = process.poll() print output To deal
with usernames and passwords, you can use subprocess to interact with
the ssh process, or you could install a public key on the server to
avoid the password prompt.

Which sounds good, but I can't quite understand this well enough to take it farther. Can someone help me to get a baby step going?

(1) log in to ssh
(2) provide the password when its prompted
(3) issue an ls command and display the result

I can do the rest of the automation if I can just get pass password entry.

PS: I tried using expect, but TCL isn't on the system so please don't tell me to use that! If you know something else I can call to aid python I'm happy to try it though as long as its likely to be built into a Fedora Enterprise Linux system by default.

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

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

发布评论

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

评论(1

帥小哥 2024-12-16 20:23:30

此代码使用 Popen.communicate() 来运行命令。正如 Python 库参考第 17 章所述,communicate() 打开 stdin、stdout 和 stderr 的管道,启动子进程,并等待进程终止。供其他读者参考:startupinfo 是在 GUI 应用程序下运行子进程而不弹出黑色控制台窗口的 Windows 特定方式;作为控制台脚本运行或在 Linux 下运行时不需要它。

def runcmd(self, prog, args, inputs):
    cmd = " ".join((prog, args))
    si = subprocess.STARTUPINFO()
    si.dwFlags = subprocess.STARTF_USESHOWWINDOW
    process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, startupinfo=si)
    return(process.communicate(input=inputs))

可以用以下方式调用它:

    p = self.runcmd("c:\Program Files\ApplicationDir\application.exe", "arguments", "inputs")
    result = str(p[0],"ascii").strip()
    print(result)

结果是一个 2 元组(stdout,stderr),应从字节字符串转换为字符串。作为“输入”传递的密码应该是一个字节字符串。

This code uses Popen.communicate() to run a command. As described in chapter 17 of the Python Library Reference, communicate() opens pipes for stdin, stdout, and stderr, starts up the subprocess, and waits for the process to terminate. FYI for other readers: startupinfo is the Windows-specific way of running the subprocess under a GUI app without popping up a black console window; it's not needed when running as a console script or under Linux.

def runcmd(self, prog, args, inputs):
    cmd = " ".join((prog, args))
    si = subprocess.STARTUPINFO()
    si.dwFlags = subprocess.STARTF_USESHOWWINDOW
    process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, startupinfo=si)
    return(process.communicate(input=inputs))

It can be called with:

    p = self.runcmd("c:\Program Files\ApplicationDir\application.exe", "arguments", "inputs")
    result = str(p[0],"ascii").strip()
    print(result)

The result a 2-tuple (stdout, stderr), which should be converted from byte strings to strings. The password passed as "inputs" should be a byte string.

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