mmaley
mmaley

Reputation: 73

How do I use Fabric to SSH to two different ports on the same server?

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?

Upvotes: 7

Views: 8176

Answers (1)

Morgan
Morgan

Reputation: 4131

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.

Upvotes: 5

Related Questions