~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to docs/source/ssh.rst

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-02-24 19:23:15 UTC
  • mfrom: (1.2.1) (9 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20130224192315-qhmwp3m3ymk8r60d
Tags: 2.2.0.1-1
* New upstream release
* relicense debian packaging to LGPL-3
* update watch file to use github directly
  thanks to Bart Martens for the file
* add autopkgtests
* drop obsolete DM-Upload-Allowed
* bump standard to 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. PyZMQ ssh doc, by Min Ragan-Kelley, 2011
 
2
 
 
3
.. _ssh:
 
4
 
 
5
Tunneling PyZMQ Connections with SSH
 
6
====================================
 
7
 
 
8
.. versionadded:: 2.1.9
 
9
 
 
10
You may want to connect ØMQ sockets across machines, or untrusted networks. One common way
 
11
to do this is to tunnel the connection via SSH. IPython_ introduced some tools for
 
12
tunneling ØMQ connections over ssh in simple cases. These functions have been brought into
 
13
pyzmq as :mod:`zmq.ssh` under IPython's BSD license.
 
14
 
 
15
PyZMQ will use the shell ssh command via pexpect_ by default, but it also supports
 
16
using paramiko_ for tunnels, so it should work on Windows.
 
17
 
 
18
.. note::
 
19
 
 
20
    pexpect has no Python3 support at this time, so Python 3 users should get Thomas
 
21
    Kluyver's `pexpect-u`_ fork.
 
22
 
 
23
An SSH tunnel has five basic components:
 
24
 
 
25
* server : the SSH server through which the tunnel will be created
 
26
* remote ip : the IP of the remote machine *as seen from the server* 
 
27
  (remote ip may be, but is not not generally the same machine as server).
 
28
* remote port : the port on the remote machine that you want to connect to.
 
29
* local ip : the interface on your local machine you want to use (default: 127.0.0.1)
 
30
* local port : the local port you want to forward to the remote port (default: high random)
 
31
 
 
32
So once you have established the tunnel, connections to ``localip:localport`` will actually
 
33
be connections to ``remoteip:remoteport``.
 
34
 
 
35
In most cases, you have a zeromq url for a remote machine, but you need to tunnel the
 
36
connection through an ssh server.  This is
 
37
 
 
38
So if you would use this command from the same LAN as the remote machine:
 
39
 
 
40
.. sourcecode:: python
 
41
 
 
42
    sock.connect("tcp://10.0.1.2:5555")
 
43
 
 
44
to make the same connection from another machine that is outside the network, but you have
 
45
ssh access to a machine ``server`` on the same LAN, you would simply do:
 
46
 
 
47
.. sourcecode:: python
 
48
 
 
49
    from zmq import ssh
 
50
    ssh.tunnel_connection(sock, "tcp://10.0.1.2:5555", "server")
 
51
 
 
52
Note that ``"server"`` can actually be a fully specified ``"user@server:port"`` ssh url.
 
53
Since this really just launches a shell command, all your ssh configuration of usernames,
 
54
aliases, keys, etc. will be respected. If necessary, :func:`tunnel_connection` does take
 
55
arguments for specific passwords, private keys (the ssh ``-i`` option), and non-default
 
56
choice of whether to use paramiko.
 
57
 
 
58
If you are on the same network as the machine, but it is only listening on localhost, you
 
59
can still connect by making the machine itself the server, and using loopback as the
 
60
remote ip:
 
61
 
 
62
.. sourcecode:: python
 
63
 
 
64
    from zmq import ssh
 
65
    ssh.tunnel_connection(sock, "tcp://127.0.0.1:5555", "10.0.1.2")
 
66
 
 
67
The :func:`tunnel_connection` function is a simple utility that forwards a random
 
68
localhost port to the real destination, and connects a socket to the new local url,
 
69
rather than the remote one that wouldn't actually work.
 
70
 
 
71
.. seealso::
 
72
 
 
73
    A short discussion of ssh tunnels: http://www.revsys.com/writings/quicktips/ssh-tunnel.html
 
74
 
 
75
 
 
76
.. _IPython: http://ipython.org
 
77
.. _pexpect: http://www.noah.org/wiki/pexpect
 
78
.. _pexpect-u: http://pypi.python.org/pypi/pexpect-u
 
79
.. _paramiko: http://www.lag.net/paramiko/
 
80