def __init__(self, name=None, **kwds): '''create a ssh tunnel launcher Inputs: name -- a unique identifier (string) for the launcher ''' xyz = string.ascii_letters self.name = ''.join(random.choice(xyz) for i in range(16)) \ if name is None else name self._launcher = Pipe('launcher') self.__disconnect() if kwds: self.connect(**kwds) return
def __init__(self, name=None, **kwds): """create a ssh tunnel launcher Inputs: name -- a unique identifier (string) for the launcher """ xyz = string.ascii_letters self.name = "".join(random.choice(xyz) for i in range(16)) if name is None else name self._launcher = Pipe("launcher") self.__disconnect() if kwds: self.connect(**kwds) return
sleep(1) #FIXME: needs time to work... print('executing {scp %s %s:%s}' % (source0,cpu1,dest0)) copier(source=source0, destination=cpu1+':'+dest0) copier.launch() sleep(1) #FIXME: needs time to work... print('executing {scp %s:%s %s:%s}' % (cpu1,source1,cpu2,dest1)) copier(source=cpu1+':'+source1, destination=cpu2+':'+dest1) copier.launch() sleep(1) #FIXME: needs time to work... print('executing {scp %s:%s %s}' % (cpu2,source2,dest2)) copier(source=cpu2+':'+source2, destination=dest2) copier.launch() sleep(1) #FIXME: needs time to work... print('cleanup temporary files...') import os os.remove(source0) launcher = Pipe('cleanup') launcher(command=del1, host=cpu1, background=True) launcher.launch() launcher(command=del2, host=cpu2, background=True) launcher.launch() # print('cleanup result file...') # os.remove("."+os.sep+os.path.basename(source2)) # End of file
sleep(1) #FIXME: needs time to work... print('executing {scp %s %s:%s}' % (source0, cpu1, dest0)) copier(source=source0, destination=cpu1 + ':' + dest0) copier.launch() sleep(1) #FIXME: needs time to work... print('executing {scp %s:%s %s:%s}' % (cpu1, source1, cpu2, dest1)) copier(source=cpu1 + ':' + source1, destination=cpu2 + ':' + dest1) copier.launch() sleep(1) #FIXME: needs time to work... print('executing {scp %s:%s %s}' % (cpu2, source2, dest2)) copier(source=cpu2 + ':' + source2, destination=dest2) copier.launch() sleep(1) #FIXME: needs time to work... print('cleanup temporary files...') import os os.remove(source0) launcher = Pipe('cleanup') launcher(command=del1, host=cpu1, background=True) launcher.launch() launcher(command=del2, host=cpu2, background=True) launcher.launch() # print('cleanup result file...') # os.remove("."+os.sep+os.path.basename(source2)) # End of file
To run: python secure_hello.py """ from pathos.secure import Pipe if __name__ == '__main__': # test command and remote host command1 = 'echo "hello from..."' command2 = 'hostname' #command3 = 'sleep 5' #XXX: buggy? #command3 = '' #XXX: buggy ? rhost = 'localhost' #rhost = 'computer.cacr.caltech.edu' #rhost = 'foobar.danse.us' launcher = Pipe('LauncherSSH') launcher(command=command1, host=rhost, background=False) launcher.launch() print(launcher.response()) launcher(command=command2, host=rhost, background=False) launcher.launch() print(launcher.response()) #launcher(command=command3, host=rhost, background=False) #launcher.launch() #print(launcher.response()) # End of file
class Tunnel(object): """a ssh-tunnel launcher for parallel and distributed computing.""" #MINPORT = 49152 MINPORT = 1024 MAXPORT = 65535 verbose = True def connect(self, host, port=None, through=None): '''establish a secure shell tunnel between local and remote host Input: host -- remote hostname [user@host:path is also valid] port -- remote port number Additional Input: through -- 'tunnel-through' hostname [default = None] ''' from pathos.portpicker import portnumber if port is None: from pathos.core import randomport port = randomport(through) if through else randomport(host) pick = portnumber(self.MINPORT, self.MAXPORT) while True: localport = pick() if localport < 0: raise TunnelException('No available local port') #print('Trying port %d...' % localport) try: self._connect(localport, host, port, through=through) #print('SSH tunnel %d:%s:%d' % (localport, host, port)) except TunnelException as e: # breaks 2.5 compatibility if e.args[0] == 'bind': self.disconnect() continue else: self.__disconnect() raise TunnelException('Connection failed') self.connected = True return localport def disconnect(self): '''destroy the ssh tunnel''' #FIXME: grep (?) for self._launcher.message, then kill the pid if self._pid > 0: if self.verbose: print('Kill ssh pid=%d' % self._pid) os.kill(self._pid, signal.SIGTERM) os.waitpid(self._pid, 0) self.__disconnect() return def __disconnect(self): '''disconnect tunnel internals''' self._pid = 0 self.connected = False self._lport = None self._rport = None self._host = None return def __init__(self, name=None, **kwds): '''create a ssh tunnel launcher Inputs: name -- a unique identifier (string) for the launcher ''' xyz = string.ascii_letters self.name = ''.join(random.choice(xyz) for i in range(16)) \ if name is None else name self._launcher = Pipe('launcher') self.__disconnect() if kwds: self.connect(**kwds) return def __repr__(self): if not self.connected: return "Tunnel('%s')" % self.name try: msg = self._launcher.message.split(' ', 1)[-1].rstrip('"').rstrip() except: msg = self._launcher.message return "Tunnel('%s')" % msg def _connect(self, localport, remotehost, remoteport, through=None): options = '-q -N -L %d:%s:%d' % (localport, remotehost, remoteport) command = '' if through: rhost = through else: rhost = remotehost self._launcher(host=rhost, command=command, options=options, background=True) #XXX: MMM #options=options, background=False) self._launcher.launch() self._lport = localport self._rport = remoteport self._host = rhost self._pid = self._launcher.pid( ) #FIXME: should be tunnel_pid [pid()+1] line = self._launcher.response() if line: if line.startswith('bind'): raise TunnelException('bind') else: print(line) raise TunnelException('failure') return
class Tunnel(object): """a ssh-tunnel launcher for parallel and distributed computing.""" # MINPORT = 49152 MINPORT = 1024 MAXPORT = 65535 verbose = True def connect(self, host, port=None, through=None): """establish a secure shell tunnel between local and remote host Input: host -- remote hostname [user@host:path is also valid] port -- remote port number Additional Input: through -- 'tunnel-through' hostname [default = None] """ from pathos.portpicker import portnumber if port is None: from pathos.core import randomport port = randomport(through) if through else randomport(host) pick = portnumber(self.MINPORT, self.MAXPORT) while True: localport = pick() if localport < 0: raise TunnelException("No available local port") # print('Trying port %d...' % localport) try: self._connect(localport, host, port, through=through) # print('SSH tunnel %d:%s:%d' % (localport, host, port)) except TunnelException as e: # breaks 2.5 compatibility if e.args[0] == "bind": self.disconnect() continue else: self.__disconnect() raise TunnelException("Connection failed") self.connected = True return localport def disconnect(self): """destroy the ssh tunnel""" # FIXME: grep (?) for self._launcher.message, then kill the pid if self._pid > 0: if self.verbose: print("Kill ssh pid=%d" % self._pid) os.kill(self._pid, signal.SIGTERM) os.waitpid(self._pid, 0) self.__disconnect() return def __disconnect(self): """disconnect tunnel internals""" self._pid = 0 self.connected = False self._lport = None self._rport = None self._host = None return def __init__(self, name=None, **kwds): """create a ssh tunnel launcher Inputs: name -- a unique identifier (string) for the launcher """ xyz = string.ascii_letters self.name = "".join(random.choice(xyz) for i in range(16)) if name is None else name self._launcher = Pipe("launcher") self.__disconnect() if kwds: self.connect(**kwds) return def __repr__(self): if not self.connected: return "Tunnel('%s')" % self.name try: msg = self._launcher.message.split(" ", 1)[-1].rstrip('"').rstrip() except: msg = self._launcher.message return "Tunnel('%s')" % msg def _connect(self, localport, remotehost, remoteport, through=None): options = "-q -N -L %d:%s:%d" % (localport, remotehost, remoteport) command = "" if through: rhost = through else: rhost = remotehost self._launcher(host=rhost, command=command, options=options, background=True) # XXX: MMM # options=options, background=False) self._launcher.launch() self._lport = localport self._rport = remoteport self._host = rhost self._pid = self._launcher.pid() # FIXME: should be tunnel_pid [pid()+1] line = self._launcher.response() if line: if line.startswith("bind"): raise TunnelException("bind") else: print(line) raise TunnelException("failure") return