Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

ssh

fabric

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?

like image 343
mmaley Avatar asked Feb 16 '12 00:02

mmaley


People also ask

Can SSH run on different ports?

By default, the SSH server still runs in port 22. However, there are occasions when it is run in a different port. Testing use is one reason. Running multiple configurations on the same host is another.

What is the fabric command to execute a shell command locally?

run (fabric. Fabric's run procedure is used for executing a shell command on one or more remote hosts. The output results of run can be captured using a variable.


1 Answers

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.

like image 131
Morgan Avatar answered Sep 22 '22 02:09

Morgan