def forward(options, free_port): """ :param options: options concludes user,key,local,server,remote user: username on server host(bastion host) key: in .ssh/id_rsd, to verify the access of user server: server host and port remote: the destination host and port(22 in most time) :return: """ client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.WarningPolicy()) context.verbose('Connecting to ssh host %s:%d ...' % (options.server[0], options.server[1])) try: client.connect(hostname=options.server[0], port=options.server[1], username=options.user, key_filename=options.key) except Exception as e: print('*** Failed to connect to %s:%d: %r' % (options.server[0], options.server[1], e)) sys.exit(1) context.verbose('Now forwarding port %d to %s:%d ...' % (free_port, options.remote[0], options.remote[1])) try: forward_tunnel(free_port, options.remote[0], options.remote[1], client.get_transport()) except KeyboardInterrupt: print('C-c: Port forwarding stopped.') sys.exit(0)
def multi_do_exec_command(): """ connect to local port via ssh. :return: """ remote_nodes, _ = databean.get_nodes(context.path[1]) if remote_nodes is None: context.verbose("there are not the forwarding to localhost") return cmds = databean.get_commands() ssh = {} # ssh = {"gpu1":SSHController} for node in remote_nodes: if node.name in cmds.keys(): ssh[node.name] = SSHController(node) processes = [] p = Pool(len(ssh)) try: for tmp in ssh.keys(): p.apply_async(do_exec_command, args=(ssh[tmp], cmds[tmp])) p.close() p.join() except: print("Error: unable to start process") return processes
def handle(self): try: chan = self.ssh_transport.open_channel('direct-tcpip', (self.chain_host, self.chain_port), self.request.getpeername()) except Exception as e: context.verbose('Incoming request to %s:%d failed: %s' % (self.chain_host, self.chain_port, repr(e))) return if chan is None: context.verbose('Incoming request to %s:%d was rejected by the SSH server.' % (self.chain_host, self.chain_port)) return context.verbose('Connected! Tunnel open %r -> %r -> %r' % (self.request.getpeername(), chan.getpeername(), (self.chain_host, self.chain_port))) while True: r, w, x = select.select([self.request, chan], [], []) if self.request in r: data = self.request.recv(1024) if len(data) == 0: break chan.send(data) if chan in r: data = chan.recv(1024) if len(data) == 0: break self.request.send(data) peername = self.request.getpeername() chan.close() self.request.close() context.verbose('Tunnel closed from %r' % (peername,))
def multi_do_download(remotepath, localpath): remote_nodes, _ = databean.get_nodes(context.path[1]) if remote_nodes is None: context.verbose("there are not the forwarding to localhost") return ssh = {} # ssh = {"gpu1":SSHController} for node in remote_nodes: ssh[node.name] = SSHController(node) processes = [] p = Pool(len(ssh)) try: for tmp in ssh.keys(): p.apply_async(do_download, args=(ssh[tmp], remotepath, localpath, tmp)) p.close() p.join() except: print("Error: unable to start process") return processes
def check_env(): if conf["machine"] == "local": flag = False for str in os.popen("ps -ef|grep forward").readlines(): if "forward" in str and "grep" not in str: flag = True break if flag is not True: context.verbose("run forward.py first!") return False else: # on server machine # TODO: need to update the nodes_local.json remote_nodes, _ = databean.get_nodes(context.path[0]) local_ports = [] for remote_node in remote_nodes: local_ports.append({ "name": remote_node.name, "host": remote_node.host, "port": remote_node.port }) with open(context.path[1], "w", encoding="utf-8") as json_file: json_file.write(json.dumps(local_ports)) return True
def exec_command(self, command): context.verbose("...%s exec the command:" % self.node.name + command) stdin, stdout, stderr = self.client.exec_command(command) context.verbose("... %s result:" % self.node.name) for line in stdout: context.verbose(line.strip("\n"))
def connect(self): self.client.connect(hostname=self.node.host, port=self.node.port, username=self.node.user, key_filename=self.node.key) context.verbose("... connected %s with key on Linux" % self.node.name)