~ubuntu-branches/ubuntu/utopic/pyzmq/utopic

« back to all changes in this revision

Viewing changes to zmq/ssh/tunnel.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2014-06-17 23:23:35 UTC
  • mfrom: (7.2.1 sid)
  • Revision ID: package-import@ubuntu.com-20140617232335-ugs1yd1547jl0l9c
Tags: 14.3.1-1
* New upstream release
* monitor-test.patch: fix test logic for synchronous connects on freebsd
* ignore test results on mips, hard to debug race condition, no reason to
  delay testing migration (Closes: #750813)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
zeromq connections.
3
3
"""
4
4
 
5
 
#-----------------------------------------------------------------------------
6
 
#  Copyright (C) 2010-2011  IPython Development Team
7
 
#  Copyright (C) 2011- Min Ragan-Kelley
8
 
#  This file is part of pyzmq.
 
5
# Copyright (C) 2010-2011  IPython Development Team
 
6
# Copyright (C) 2011- PyZMQ Developers
9
7
#
10
 
#  Redistributed from IPython under the terms of the BSD License.
11
 
#-----------------------------------------------------------------------------
12
 
 
13
 
 
14
 
#-----------------------------------------------------------------------------
15
 
# Imports
16
 
#-----------------------------------------------------------------------------
 
8
# Redistributed from IPython under the terms of the BSD License.
 
9
 
 
10
 
17
11
from __future__ import print_function
18
12
 
19
13
import atexit
40
34
except ImportError:
41
35
    pexpect = None
42
36
 
43
 
#-----------------------------------------------------------------------------
44
 
# Code
45
 
#-----------------------------------------------------------------------------
46
37
 
47
38
_random_ports = set()
48
39
 
212
203
        server, port = server.split(':')
213
204
        ssh += " -p %s" % port
214
205
    
 
206
    cmd = "%s -O check %s" % (ssh, server)
 
207
    (output, exitstatus) = pexpect.run(cmd, withexitstatus=True)
 
208
    if not exitstatus:
 
209
        pid = int(output[output.find("(pid=")+5:output.find(")")]) 
 
210
        cmd = "%s -O forward -L 127.0.0.1:%i:%s:%i %s" % (
 
211
            ssh, lport, remoteip, rport, server)
 
212
        (output, exitstatus) = pexpect.run(cmd, withexitstatus=True)
 
213
        if not exitstatus:
 
214
            atexit.register(_stop_tunnel, cmd.replace("-O forward", "-O cancel", 1))
 
215
            return pid
215
216
    cmd = "%s -f -S none -L 127.0.0.1:%i:%s:%i %s sleep %i" % (
216
217
        ssh, lport, remoteip, rport, server, timeout)
217
218
    tunnel = pexpect.spawn(cmd)
238
239
            tunnel.sendline(password)
239
240
            failed = True
240
241
    
 
242
def _stop_tunnel(cmd):
 
243
    pexpect.run(cmd)
 
244
 
241
245
def _split_server(server):
242
246
    if '@' in server:
243
247
        username,server = server.split('@', 1)