如何使用 Fabric 通过 SSH 连接到同一服务器上的两个不同端口?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这是 Bug 568 中解决的问题,该问题已在 Fabric 1.4.1 中修补+。您应该更新到最新版本,看看这是否能解决您的问题。附带说明一下,通过对主机操作执行此操作可能会更好:
因为您没有创建任何变量,也没有简单的 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:
As you're not making any vars, or simple for loops to populate them.