如何使用 Fabric 通过 SSH 连接到同一服务器上的两个不同端口?

发布于 2025-01-05 15:39:05 字数 2217 浏览 1 评论 0原文

我正在尝试使用 Fabric (v1.3.4) 在各种服务器上配置 Karaf 实例。 Karaf 实现了 SSH 服务器。所以,我有 2 个 ssh 守护进程在同一台服务器上运行;一个在端口 22 上,一个在端口 8101 上。使用 Fabric 的 Fabric.tasks.execute() 方法,我可以连接到备用主机:端口。

问题是,由于 env.user 明显被劫持,我的初始会话被第二个连接的指定用户劫持。

下面是一个简化的 fabfile.py 示例:

from fabric.api import env, run
from fabric.tasks import execute

env.hosts = ['192.168.5.250']

def firstSSH():
        run("echo first")
        executeHosts = []
        for host in env.hosts:
                executeHosts.append("notmmaley@" + host + ":8101")
        execute(secondSSH, hosts=executeHosts)
        run("echo first again")

def secondSSH():
    run("echo second", shell=False, pty=False)

两个 SSH 服务器位于同一服务器上,具有两个不同的端口并允许两个不同的用户。以下是输出:

~/fabric$ fab firstSSH
[192.168.5.250] Executing task 'firstSSH'
[192.168.5.250] run: echo first
[192.168.5.250] Login password:
[192.168.5.250] out: first

[[email protected]:8101] Executing task 'secondSSH'
[[email protected]:8101] run: echo second
[[email protected]:8101] Login password:
[[email protected]:8101] out: second

[[email protected]:8101] run: echo first again

Done.
Disconnecting from 192.168.5.250:8101... done.
Disconnecting from [email protected]... done.

请注意“再次回显”是如何以 notmmaley 用户身份执行的,该用户是为execute()命令的主机严格指定的。我想要(需要)的是,execute() 命令作为指定的 user@host:port 的指定用户出现,然后将原始用户返回给我以执行剩余的任务。这可以通过 Fabric/execute() 实现吗?和/或我哪里出了问题?

I'm trying to use Fabric (v1.3.4) to provision Karaf instances on various servers. Karaf implements an SSH server. So, I have 2 ssh daemons running on the same server; one on port 22 and one on 8101. Using the fabric.tasks.execute() method of Fabric, I can connect to an alternative host:port.

The problem is, my initial session becomes hijacked by the named user of the second connection due to an apparent hijacking of env.user.

Here's a simplified fabfile.py example:

from fabric.api import env, run
from fabric.tasks import execute

env.hosts = ['192.168.5.250']

def firstSSH():
        run("echo first")
        executeHosts = []
        for host in env.hosts:
                executeHosts.append("notmmaley@" + host + ":8101")
        execute(secondSSH, hosts=executeHosts)
        run("echo first again")

def secondSSH():
    run("echo second", shell=False, pty=False)

Both SSH servers are on the same server, with on two different ports and allowing for two different users. Here is the output:

~/fabric$ fab firstSSH
[192.168.5.250] Executing task 'firstSSH'
[192.168.5.250] run: echo first
[192.168.5.250] Login password:
[192.168.5.250] out: first

[[email protected]:8101] Executing task 'secondSSH'
[[email protected]:8101] run: echo second
[[email protected]:8101] Login password:
[[email protected]:8101] out: second

[[email protected]:8101] run: echo first again

Done.
Disconnecting from 192.168.5.250:8101... done.
Disconnecting from [email protected]... done.

Note how the "echo first again" is executed as the notmmaley user that was specified strictly for hosts of the execute() command. What I want (need) is for the execute() command to occur as named user for the specified user@host:port and then return the original user to me for the remaining tasks. Is this possible with Fabric/execute() and/or where have I gone wrong?

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

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

发布评论

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

评论(1

堇年纸鸢 2025-01-12 15:39:05

我相信这是 Bug 568 中解决的问题,该问题已在 Fabric 1.4.1 中修补+。您应该更新到最新版本,看看这是否能解决您的问题。附带说明一下,通过对主机操作执行此操作可能会更好:

execute(secondSSH, hosts=["notmmaley@%s:8101" % h for h in env.hosts])

因为您没有创建任何变量,也没有简单的 for 循环来填充它们。

I believe this is an issue addressed in Bug 568, which is patched in Fabric 1.4.1+. You should update to the newest and see if this addresses your issue. On a side note you might be better served by doing this for your host manipulation:

execute(secondSSH, hosts=["notmmaley@%s:8101" % h for h in env.hosts])

As you're not making any vars, or simple for loops to populate them.

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