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

I am trying to use Fabric (v1.3.4) to provide instances of Karaf on different 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 () Fabric method, I can connect to an alternate host: port.

The problem is that my original session was hijacked by the named user of the second connection due to the explicit hijacking of env.user.

Here's a simplified example of 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) 

Both SSH servers are located on the same server with two different ports and allow the use of two different users. Here is the result:

 ~/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 [ notmmaley@192.168.5.250 :8101] Executing task 'secondSSH' [ notmmaley@192.168.5.250 :8101] run: echo second [ notmmaley@192.168.5.250 :8101] Login password: [ notmmaley@192.168.5.250 :8101] out: second [ notmmaley@192.168.5.250 :8101] run: echo first again Done. Disconnecting from 192.168.5.250:8101... done. Disconnecting from mmaley@192.168.5.250... done. 

Note how "echo first again" is executed as the user notmmaley, which has been strictly defined for the hosts of the execute () command. What I want (need) is to execute the execute () command as the named user for the specified user port user @host :, and then return the original user to me for the rest of the tasks. Is this possible with Fabric / execute () and / or where did I go wrong?

+7
source share
1 answer

I believe this is the issue discussed in Bug 568 , which is fixed in Fabric 1.4.1+. You should upgrade to the latest version and see if this applies to your problem. On a side note, you might be better off if you do this to manipulate the host:

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

Like you do not do any vars or just for loops to fill them.

+5
source

All Articles