def listener(port): listener = socket.socket() listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(('localhost', port)) listener.listen(8) while True: local_stream, peer_addr = listener.accept() logger.info( "Accepting %s:%s and opening SSH channel to %s:%s", *peer_addr, args.device, port) if client.get_transport() is None: logger.error( "Trying to open a channel before the transport is ready!" ) continue try: remote_stream = \ transport.open_channel('direct-tcpip', (args.device, port), peer_addr) except Exception: logger.exception("Cannot open channel on port %s", port) continue thread = threading.Thread(target=forwarder, args=(local_stream, remote_stream), name="forward-{}".format(port), daemon=True) thread.start()
def run_command(cmd, **kws): logger.info("Executing {}".format(cmd)) chan = get_ssh().get_transport().open_session() chan.set_combine_stderr(True) chan.exec_command(cmd.format(tmp=tmp, env=env, serial=args.serial, ip=args.ip, firmware=firmware, **kws)) return chan.makefile()
def forwarder(port): listener = socket.socket() listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(('localhost', port)) listener.listen(1) while True: local_stream, peer_addr = listener.accept() logger.info("Accepting %s:%s and opening SSH channel to %s:%s", *peer_addr, args.ip, port) if get_ssh().get_transport() is None: logger.error("Trying to open a channel before the transport is ready!") continue try: remote_stream = get_ssh().get_transport() \ .open_channel('direct-tcpip', (args.ip, port), peer_addr) except Exception as e: logger.exception("Cannot open channel on port %s", port) while True: try: r, w, x = select.select([local_stream, remote_stream], [], []) if local_stream in r: data = local_stream.recv(1024) if data == b"": break remote_stream.send(data) if remote_stream in r: data = remote_stream.recv(1024) if data == b"": break local_stream.send(data) except Exception as e: logger.exception("Forward error on port %s", port) local_stream.close() remote_stream.close()
def main(): args = get_argparser().parse_args() init_logger(args) client = SSHClient(args.host) sftp = client.get_sftp() tcpdump = client.spawn_command( "/usr/sbin/tcpdump host {device} -w {tmp}/trace.pcap", get_pty=True, device=args.device) try: subprocess.check_call(args.command) except subprocess.CalledProcessError: logger.error("Command failed") tcpdump.close() sftp.get("{tmp}/trace.pcap".format(tmp=client.tmp), args.file + ".new") os.rename(args.file + ".new", args.file) logger.info("Pcap file {file} retrieved".format(file=args.file))
def forwarder(port): listener = socket.socket() listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(('localhost', port)) listener.listen(1) while True: local_stream, peer_addr = listener.accept() logger.info("Accepting %s:%s and opening SSH channel to %s:%s", *peer_addr, args.device, port) if client.get_transport() is None: logger.error("Trying to open a channel before the transport is ready!") continue try: remote_stream = \ transport.open_channel('direct-tcpip', (args.device, port), peer_addr) except Exception as e: logger.exception("Cannot open channel on port %s", port) continue while True: try: r, _, _ = select.select([local_stream, remote_stream], [], []) if local_stream in r: data = local_stream.recv(65535) if data == b"": break remote_stream.sendall(data) if remote_stream in r: data = remote_stream.recv(65535) if data == b"": break local_stream.sendall(data) except Exception as e: logger.exception("Forward error on port %s", port) break local_stream.close() remote_stream.close()
def main(): args = get_argparser().parse_args() init_logger(args) if args.target == "kc705_dds" or args.target == "kc705_drtio_master": firmware = "runtime" elif args.target == "kc705_drtio_satellite": firmware = "satman" else: raise NotImplementedError("unknown target {}".format(args.target)) client = SSHClient(args.host) substs = { "env": "bash -c 'export PATH=$HOME/miniconda/bin:$PATH; exec $0 $*' ", "serial": args.serial, "firmware": firmware, } for action in args.actions: if action == "build": logger.info("Building firmware") try: subprocess.check_call([ "python3", "-m", "artiq.gateware.targets." + args.target, "--no-compile-gateware", "--output-dir", "/tmp/{target}".format(target=args.target) ]) except subprocess.CalledProcessError: logger.error("Build failed") sys.exit(1) elif action == "clean": logger.info("Cleaning build directory") target_dir = "/tmp/{target}".format(target=args.target) if os.path.isdir(target_dir): shutil.rmtree(target_dir) elif action == "reset": logger.info("Resetting device") client.run_command( "{env} artiq_flash start" + (" --target-file " + args.config if args.config else ""), **substs) elif action == "boot" or action == "boot+log": logger.info("Uploading firmware") client.get_sftp().put( "/tmp/{target}/software/{firmware}/{firmware}.bin".format( target=args.target, firmware=firmware), "{tmp}/{firmware}.bin".format(tmp=client.tmp, firmware=firmware)) logger.info("Booting firmware") flterm = client.spawn_command( "{env} python3 flterm.py {serial} " + "--kernel {tmp}/{firmware}.bin " + ("--upload-only" if action == "boot" else "--output-only"), **substs) artiq_flash = client.spawn_command( "{env} artiq_flash start" + (" --target-file " + args.config if args.config else ""), **substs) client.drain(flterm) elif action == "connect": transport = client.get_transport() def forwarder(local_stream, remote_stream): try: while True: r, _, _ = select.select([local_stream, remote_stream], [], []) if local_stream in r: data = local_stream.recv(65535) if data == b"": break remote_stream.sendall(data) if remote_stream in r: data = remote_stream.recv(65535) if data == b"": break local_stream.sendall(data) except Exception as err: logger.error("Cannot forward on port %s: %s", port, repr(err)) local_stream.close() remote_stream.close() def listener(port): listener = socket.socket() listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(('localhost', port)) listener.listen(8) while True: local_stream, peer_addr = listener.accept() logger.info( "Accepting %s:%s and opening SSH channel to %s:%s", *peer_addr, args.device, port) if client.get_transport() is None: logger.error( "Trying to open a channel before the transport is ready!" ) continue try: remote_stream = \ transport.open_channel('direct-tcpip', (args.device, port), peer_addr) except Exception: logger.exception("Cannot open channel on port %s", port) continue thread = threading.Thread(target=forwarder, args=(local_stream, remote_stream), name="forward-{}".format(port), daemon=True) thread.start() for port in (1380, 1381, 1382, 1383): thread = threading.Thread(target=listener, args=(port, ), name="listen-{}".format(port), daemon=True) thread.start() logger.info("Connecting to device") client.run_command( "{env} python3 flterm.py {serial} --output-only", **substs) elif action == "hotswap": logger.info("Hotswapping firmware") try: subprocess.check_call([ "python3", "-m", "artiq.frontend.artiq_coreboot", "hotswap", "/tmp/{target}/software/{firmware}/{firmware}.bin".format( target=args.target, firmware=firmware) ]) except subprocess.CalledProcessError: logger.error("Build failed") sys.exit(1) else: logger.error("Unknown action {}".format(action)) sys.exit(1)
def main(): args = get_argparser().parse_args() init_logger(args) if args.target == "kc705_dds" or args.target == "kc705_drtio_master": firmware = "runtime" elif args.target == "kc705_drtio_satellite": firmware = "satman" else: raise NotImplementedError("unknown target {}".format(args.target)) client = SSHClient(args.host) substs = { "env": "bash -c 'export PATH=$HOME/miniconda/bin:$PATH; exec $0 $*' ", "serial": args.serial, "firmware": firmware, } for action in args.actions: if action == "build": logger.info("Building firmware") try: subprocess.check_call(["python3", "-m", "artiq.gateware.targets." + args.target, "--no-compile-gateware", "--output-dir", "/tmp/{target}".format(target=args.target)]) except subprocess.CalledProcessError: logger.error("Build failed") sys.exit(1) elif action == "clean": logger.info("Cleaning build directory") target_dir = "/tmp/{target}".format(target=args.target) if os.path.isdir(target_dir): shutil.rmtree(target_dir) elif action == "reset": logger.info("Resetting device") client.run_command( "{env} artiq_flash start" + (" --target-file " + args.config if args.config else ""), **substs) elif action == "boot" or action == "boot+log": logger.info("Uploading firmware") client.get_sftp().put("/tmp/{target}/software/{firmware}/{firmware}.bin" .format(target=args.target, firmware=firmware), "{tmp}/{firmware}.bin" .format(tmp=client.tmp, firmware=firmware)) logger.info("Booting firmware") flterm = client.spawn_command( "{env} python3 flterm.py {serial} " + "--kernel {tmp}/{firmware}.bin " + ("--upload-only" if action == "boot" else "--output-only"), **substs) artiq_flash = client.spawn_command( "{env} artiq_flash start" + (" --target-file " + args.config if args.config else ""), **substs) client.drain(flterm) elif action == "connect": transport = client.get_transport() def forwarder(port): listener = socket.socket() listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(('localhost', port)) listener.listen(1) while True: local_stream, peer_addr = listener.accept() logger.info("Accepting %s:%s and opening SSH channel to %s:%s", *peer_addr, args.device, port) if client.get_transport() is None: logger.error("Trying to open a channel before the transport is ready!") continue try: remote_stream = \ transport.open_channel('direct-tcpip', (args.device, port), peer_addr) except Exception as e: logger.exception("Cannot open channel on port %s", port) continue while True: try: r, _, _ = select.select([local_stream, remote_stream], [], []) if local_stream in r: data = local_stream.recv(65535) if data == b"": break remote_stream.sendall(data) if remote_stream in r: data = remote_stream.recv(65535) if data == b"": break local_stream.sendall(data) except Exception as e: logger.exception("Forward error on port %s", port) break local_stream.close() remote_stream.close() for port in (1380, 1381, 1382, 1383): thread = threading.Thread(target=forwarder, args=(port,), name="port-{}".format(port), daemon=True) thread.start() logger.info("Connecting to device") client.run_command( "{env} python3 flterm.py {serial} --output-only", **substs) elif action == "hotswap": logger.info("Hotswapping firmware") try: subprocess.check_call(["python3", "-m", "artiq.frontend.artiq_coreboot", "hotswap", "/tmp/{target}/software/{firmware}/{firmware}.bin" .format(target=args.target, firmware=firmware)]) except subprocess.CalledProcessError: logger.error("Build failed") sys.exit(1) else: logger.error("Unknown action {}".format(action)) sys.exit(1)
def main(): args = get_argparser().parse_args() init_logger(args) if args.target == "kc705_dds": firmware = "runtime" elif args.target == "kc705_drtio_satellite": firmware = "satman" else: raise NotImplementedError("unknown target {}".format(args.target)) ssh = None def get_ssh(): nonlocal ssh if ssh is not None: return ssh ssh = paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(args.host) return ssh sftp = None def get_sftp(): nonlocal sftp if sftp is not None: return sftp sftp = get_ssh().open_sftp() return sftp rng = Random() tmp = "artiq" + "".join([rng.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ") for _ in range(6)]) env = "bash -c 'export PATH=$HOME/miniconda/bin:$PATH; exec $0 $*' " def run_command(cmd, **kws): logger.info("Executing {}".format(cmd)) chan = get_ssh().get_transport().open_session() chan.set_combine_stderr(True) chan.exec_command(cmd.format(tmp=tmp, env=env, serial=args.serial, ip=args.ip, firmware=firmware, **kws)) return chan.makefile() def drain(chan): while True: char = chan.read(1) if char == b"": break sys.stderr.write(char.decode("utf-8", errors='replace')) for action in args.actions: if action == "build": logger.info("Building firmware") try: subprocess.check_call(["python3", "-m", "artiq.gateware.targets." + args.target, "--no-compile-gateware", "--output-dir", "/tmp/{target}".format(target=args.target)]) except subprocess.CalledProcessError: logger.error("Build failed") sys.exit(1) elif action == "boot" or action == "boot+log": logger.info("Uploading firmware") get_sftp().mkdir("/tmp/{tmp}".format(tmp=tmp)) get_sftp().put("/tmp/{target}/software/{firmware}/{firmware}.bin" .format(target=args.target, firmware=firmware), "/tmp/{tmp}/{firmware}.bin".format(tmp=tmp, firmware=firmware)) logger.info("Booting firmware") flterm = run_command( "{env} python3 flterm.py {serial} " + "--kernel /tmp/{tmp}/{firmware}.bin " + ("--upload-only" if action == "boot" else "--output-only")) artiq_flash = run_command( "{env} artiq_flash start") drain(flterm) elif action == "connect": def forwarder(port): listener = socket.socket() listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(('localhost', port)) listener.listen(1) while True: local_stream, peer_addr = listener.accept() logger.info("Accepting %s:%s and opening SSH channel to %s:%s", *peer_addr, args.ip, port) if get_ssh().get_transport() is None: logger.error("Trying to open a channel before the transport is ready!") continue try: remote_stream = get_ssh().get_transport() \ .open_channel('direct-tcpip', (args.ip, port), peer_addr) except Exception as e: logger.exception("Cannot open channel on port %s", port) while True: try: r, w, x = select.select([local_stream, remote_stream], [], []) if local_stream in r: data = local_stream.recv(1024) if data == b"": break remote_stream.send(data) if remote_stream in r: data = remote_stream.recv(1024) if data == b"": break local_stream.send(data) except Exception as e: logger.exception("Forward error on port %s", port) local_stream.close() remote_stream.close() for port in (1381, 1382): thread = threading.Thread(target=forwarder, args=(port,), name="port-{}".format(port), daemon=True) thread.start() logger.info("Connecting to device") flterm = run_command( "{env} python3 flterm.py {serial} --output-only") drain(flterm) else: logger.error("Unknown action {}".format(action)) sys.exit(1)