def mtu_path_is_safe(host, ipversion=None): """ Using tracepath, check to see if the MTU along the path to 'host' gets any narrower than what it is locally. Set 'ipversion' to 4 or 6 to force a particular IP version. If not provided, the function will try to figure it out using the resolver. Returns a tuple of (boolean, status message). """ assert ipversion in [4, 6, None], "Invalid IP version" if ipversion is None: (ipversion, message) = ip_addr_version(host) if ipversion is None: return (False, message) assert ipversion is not None, "No ip version; cannot proceed." if ipversion == 6: tracepath = "tracepath6" else: tracepath = "tracepath" status, stdout, stderr = run_program([tracepath, host], timeout=30) if status != 0: return (False, "Error: %s" % (stderr.strip())) mtu_match = re.compile("^.*pmtu ([0-9]+)") mtus = [] for line in stdout.split("\n"): matches = mtu_match.match(line) if matches is not None: mtu = int(matches.groups()[0]) mtus.append(mtu) if not mtus: return (False, "Found no MTU information in trace to %s" % (host)) if len(mtus) == 1: return (False, "Found only one MTU in trace to %s" % (host)) initial_mtu = mtus[0] last_low_mtu = initial_mtu drops = [] for mtu in mtus[1:]: if mtu < last_low_mtu: drops.append(str(mtu)) last_low_mtu = mtu if drops: return "MTU along path drops from %d to %s" % (initial_mtu, " to ".join(drops)) return (True, "%d+" % (initial_mtu))
def mtu_path_is_safe(host): """ Using tracepath, check to see if the MTU along the path to 'host' gets any narrower than what it is locally. Returns a tuple of (boolean, status message). """ status, stdout, stderr = run_program(["tracepath", host], timeout=30) if status != 0: return (False, "Error: %s" % (stderr.strip())) mtu_match = re.compile("^.*pmtu ([0-9]+)") mtus = [] for line in stdout.split("\n"): matches = mtu_match.match(line) if matches is not None: mtu = int(matches.groups()[0]) mtus.append(mtu) if not mtus: return (False, "Found no MTU information in trace to %s" % (host)) if len(mtus) == 1: return (False, "Found only one MTU in trace to %s" % (host)) initial_mtu = mtus[0] last_low_mtu = initial_mtu drops = [] for mtu in mtus[1:]: if mtu < last_low_mtu: drops.append(str(mtu)) last_low_mtu = mtu if drops: return "MTU along path drops from %d to %s" % (initial_mtu, " to ".join(drops)) return (True, "%d+" % (initial_mtu))
from program import parse_commands, run_program lines = [l.strip() for l in open('input8').readlines()] commands = parse_commands(lines) acc, _ = run_program(commands) print('Part 1:', acc) for idx, (cmd, _) in enumerate(commands): if cmd == 'nop': commands[idx][0] = 'jmp' elif cmd == 'jmp': commands[idx][0] = 'nop' acc, terminated = run_program(commands) if terminated: print('Part 2:', acc) break commands[idx][0] = cmd