Пример #1
0
def _pprint_key_entries(user, key_fn, key_entries, hash_meth='md5',
                        prefix='ci-info: '):
    if not key_entries:
        message = ("%sno authorized ssh keys fingerprints found for user %s.\n"
                   % (prefix, user))
        util.multi_log(message)
        return
    tbl_fields = ['Keytype', 'Fingerprint (%s)' % (hash_meth), 'Options',
                  'Comment']
    tbl = SimpleTable(tbl_fields)
    for entry in key_entries:
        if _is_printable_key(entry):
            row = []
            row.append(entry.keytype or '-')
            row.append(_gen_fingerprint(entry.base64, hash_meth) or '-')
            row.append(entry.options or '-')
            row.append(entry.comment or '-')
            tbl.add_row(row)
    authtbl_s = tbl.get_string()
    authtbl_lines = authtbl_s.splitlines()
    max_len = len(max(authtbl_lines, key=len))
    lines = [
        util.center("Authorized keys from %s for user %s" %
                    (key_fn, user), "+", max_len),
    ]
    lines.extend(authtbl_lines)
    for line in lines:
        util.multi_log(text="%s%s\n" % (prefix, line),
                       stderr=False, console=True)
Пример #2
0
def netdev_pformat():
    lines = []
    try:
        netdev = netdev_info(empty=".")
    except Exception as e:
        lines.append(
            util.center(
                "Net device info failed ({error})".format(error=str(e)), '!',
                80))
    else:
        if not netdev:
            return '\n'
        fields = ['Device', 'Up', 'Address', 'Mask', 'Scope', 'Hw-Address']
        tbl = SimpleTable(fields)
        for (dev, data) in sorted(netdev.items()):
            for addr in data.get('ipv4'):
                tbl.add_row([
                    dev, data["up"], addr["ip"], addr["mask"],
                    addr.get('scope', '.'), data["hwaddr"]
                ])
            for addr in data.get('ipv6'):
                tbl.add_row([
                    dev, data["up"], addr["ip"], ".", addr["scope6"],
                    data["hwaddr"]
                ])
        netdev_s = tbl.get_string()
        max_len = len(max(netdev_s.splitlines(), key=len))
        header = util.center("Net device info", "+", max_len)
        lines.extend([header, netdev_s])
    return "\n".join(lines) + "\n"
Пример #3
0
def route_pformat():
    lines = []
    try:
        routes = route_info()
    except Exception as e:
        lines.append(
            util.center("Route info failed ({error})".format(error=str(e)),
                        "!", 80))
        util.logexc(LOG, "Route info failed: %s" % e)
    else:
        if routes.get("ipv4"):
            fields_v4 = [
                "Route",
                "Destination",
                "Gateway",
                "Genmask",
                "Interface",
                "Flags",
            ]
            tbl_v4 = SimpleTable(fields_v4)
            for (n, r) in enumerate(routes.get("ipv4")):
                route_id = str(n)
                tbl_v4.add_row([
                    route_id,
                    r["destination"],
                    r["gateway"],
                    r["genmask"],
                    r["iface"],
                    r["flags"],
                ])
            route_s = tbl_v4.get_string()
            max_len = len(max(route_s.splitlines(), key=len))
            header = util.center("Route IPv4 info", "+", max_len)
            lines.extend([header, route_s])
        if routes.get("ipv6"):
            fields_v6 = [
                "Route",
                "Destination",
                "Gateway",
                "Interface",
                "Flags",
            ]
            tbl_v6 = SimpleTable(fields_v6)
            for (n, r) in enumerate(routes.get("ipv6")):
                route_id = str(n)
                if r["iface"] == "lo":
                    continue
                tbl_v6.add_row([
                    route_id,
                    r["destination"],
                    r["gateway"],
                    r["iface"],
                    r["flags"],
                ])
            route_s = tbl_v6.get_string()
            max_len = len(max(route_s.splitlines(), key=len))
            header = util.center("Route IPv6 info", "+", max_len)
            lines.extend([header, route_s])
    return "\n".join(lines) + "\n"
Пример #4
0
def route_pformat():
    lines = []
    try:
        routes = route_info()
    except Exception as e:
        lines.append(
            util.center('Route info failed ({error})'.format(error=str(e)),
                        '!', 80))
        util.logexc(LOG, "Route info failed: %s" % e)
    else:
        if routes.get('ipv4'):
            fields_v4 = [
                'Route', 'Destination', 'Gateway', 'Genmask', 'Interface',
                'Flags'
            ]
            tbl_v4 = SimpleTable(fields_v4)
            for (n, r) in enumerate(routes.get('ipv4')):
                route_id = str(n)
                tbl_v4.add_row([
                    route_id, r['destination'], r['gateway'], r['genmask'],
                    r['iface'], r['flags']
                ])
            route_s = tbl_v4.get_string()
            max_len = len(max(route_s.splitlines(), key=len))
            header = util.center("Route IPv4 info", "+", max_len)
            lines.extend([header, route_s])
        if routes.get('ipv6'):
            fields_v6 = [
                'Route', 'Destination', 'Gateway', 'Interface', 'Flags'
            ]
            tbl_v6 = SimpleTable(fields_v6)
            for (n, r) in enumerate(routes.get('ipv6')):
                route_id = str(n)
                if r['iface'] == 'lo':
                    continue
                tbl_v6.add_row([
                    route_id, r['destination'], r['gateway'], r['iface'],
                    r['flags']
                ])
            route_s = tbl_v6.get_string()
            max_len = len(max(route_s.splitlines(), key=len))
            header = util.center("Route IPv6 info", "+", max_len)
            lines.extend([header, route_s])
    return "\n".join(lines) + "\n"
Пример #5
0
def netdev_pformat():
    lines = []
    try:
        netdev = netdev_info(empty=".")
    except Exception:
        lines.append(util.center("Net device info failed", '!', 80))
    else:
        fields = ['Device', 'Up', 'Address', 'Mask', 'Scope', 'Hw-Address']
        tbl = SimpleTable(fields)
        for (dev, d) in netdev.items():
            tbl.add_row([dev, d["up"], d["addr"], d["mask"], ".", d["hwaddr"]])
            if d.get('addr6'):
                tbl.add_row([dev, d["up"],
                             d["addr6"], ".", d.get("scope6"), d["hwaddr"]])
        netdev_s = tbl.get_string()
        max_len = len(max(netdev_s.splitlines(), key=len))
        header = util.center("Net device info", "+", max_len)
        lines.extend([header, netdev_s])
    return "\n".join(lines)
Пример #6
0
def _pprint_key_entries(user,
                        key_fn,
                        key_entries,
                        hash_meth="sha256",
                        prefix="ci-info: "):
    if not key_entries:
        message = (
            "%sno authorized SSH keys fingerprints found for user %s.\n" %
            (prefix, user))
        util.multi_log(message, console=True, stderr=False)
        return
    tbl_fields = [
        "Keytype",
        "Fingerprint (%s)" % (hash_meth),
        "Options",
        "Comment",
    ]
    tbl = SimpleTable(tbl_fields)
    for entry in key_entries:
        if _is_printable_key(entry):
            row = [
                entry.keytype or "-",
                _gen_fingerprint(entry.base64, hash_meth) or "-",
                entry.options or "-",
                entry.comment or "-",
            ]
            tbl.add_row(row)
    authtbl_s = tbl.get_string()
    authtbl_lines = authtbl_s.splitlines()
    max_len = len(max(authtbl_lines, key=len))
    lines = [
        util.center(
            "Authorized keys from %s for user %s" % (key_fn, user),
            "+",
            max_len,
        ),
    ]
    lines.extend(authtbl_lines)
    for line in lines:
        util.multi_log(text="%s%s\n" % (prefix, line),
                       stderr=False,
                       console=True)
Пример #7
0
def route_pformat():
    lines = []
    try:
        routes = route_info()
    except Exception as e:
        lines.append(util.center('Route info failed', '!', 80))
        util.logexc(LOG, "Route info failed: %s" % e)
    else:
        if routes.get('ipv4'):
            fields_v4 = [
                'Route', 'Destination', 'Gateway', 'Genmask', 'Interface',
                'Flags'
            ]
            tbl_v4 = SimpleTable(fields_v4)
            for (n, r) in enumerate(routes.get('ipv4')):
                route_id = str(n)
                tbl_v4.add_row([
                    route_id, r['destination'], r['gateway'], r['genmask'],
                    r['iface'], r['flags']
                ])
            route_s = tbl_v4.get_string()
            max_len = len(max(route_s.splitlines(), key=len))
            header = util.center("Route IPv4 info", "+", max_len)
            lines.extend([header, route_s])
        if routes.get('ipv6'):
            fields_v6 = [
                'Route', 'Proto', 'Recv-Q', 'Send-Q', 'Local Address',
                'Foreign Address', 'State'
            ]
            tbl_v6 = SimpleTable(fields_v6)
            for (n, r) in enumerate(routes.get('ipv6')):
                route_id = str(n)
                tbl_v6.add_row([
                    route_id, r['proto'], r['recv-q'], r['send-q'],
                    r['local address'], r['foreign address'], r['state']
                ])
            route_s = tbl_v6.get_string()
            max_len = len(max(route_s.splitlines(), key=len))
            header = util.center("Route IPv6 info", "+", max_len)
            lines.extend([header, route_s])
    return "\n".join(lines)
Пример #8
0
def netdev_pformat():
    lines = []
    empty = "."
    try:
        netdev = netdev_info(empty=empty)
    except Exception as e:
        lines.append(
            util.center(
                "Net device info failed ({error})".format(error=str(e)),
                '!', 80))
    else:
        if not netdev:
            return '\n'
        fields = ['Device', 'Up', 'Address', 'Mask', 'Scope', 'Hw-Address']
        tbl = SimpleTable(fields)
        for (dev, data) in sorted(netdev.items()):
            for addr in data.get('ipv4'):
                tbl.add_row(
                    (dev, data["up"], addr["ip"], addr["mask"],
                     addr.get('scope', empty), data["hwaddr"]))
            for addr in data.get('ipv6'):
                tbl.add_row(
                    (dev, data["up"], addr["ip"], empty,
                     addr.get("scope6", empty), data["hwaddr"]))
            if len(data.get('ipv6')) + len(data.get('ipv4')) == 0:
                tbl.add_row((dev, data["up"], empty, empty, empty,
                             data["hwaddr"]))
        netdev_s = tbl.get_string()
        max_len = len(max(netdev_s.splitlines(), key=len))
        header = util.center("Net device info", "+", max_len)
        lines.extend([header, netdev_s])
    return "\n".join(lines) + "\n"
Пример #9
0
def route_pformat():
    lines = []
    try:
        routes = route_info()
    except Exception as e:
        lines.append(
            util.center(
                'Route info failed ({error})'.format(error=str(e)),
                '!', 80))
        util.logexc(LOG, "Route info failed: %s" % e)
    else:
        if routes.get('ipv4'):
            fields_v4 = ['Route', 'Destination', 'Gateway',
                         'Genmask', 'Interface', 'Flags']
            tbl_v4 = SimpleTable(fields_v4)
            for (n, r) in enumerate(routes.get('ipv4')):
                route_id = str(n)
                tbl_v4.add_row([route_id, r['destination'],
                                r['gateway'], r['genmask'],
                                r['iface'], r['flags']])
            route_s = tbl_v4.get_string()
            max_len = len(max(route_s.splitlines(), key=len))
            header = util.center("Route IPv4 info", "+", max_len)
            lines.extend([header, route_s])
        if routes.get('ipv6'):
            fields_v6 = ['Route', 'Destination', 'Gateway', 'Interface',
                         'Flags']
            tbl_v6 = SimpleTable(fields_v6)
            for (n, r) in enumerate(routes.get('ipv6')):
                route_id = str(n)
                if r['iface'] == 'lo':
                    continue
                tbl_v6.add_row([route_id, r['destination'],
                                r['gateway'], r['iface'], r['flags']])
            route_s = tbl_v6.get_string()
            max_len = len(max(route_s.splitlines(), key=len))
            header = util.center("Route IPv6 info", "+", max_len)
            lines.extend([header, route_s])
    return "\n".join(lines) + "\n"
Пример #10
0
def netdev_pformat():
    lines = []
    empty = "."
    try:
        netdev = netdev_info(empty=empty)
    except Exception as e:
        lines.append(
            util.center(
                "Net device info failed ({error})".format(error=str(e)),
                "!",
                80,
            )
        )
    else:
        if not netdev:
            return "\n"
        fields = ["Device", "Up", "Address", "Mask", "Scope", "Hw-Address"]
        tbl = SimpleTable(fields)
        for (dev, data) in sorted(netdev.items()):
            for addr in data.get("ipv4"):
                tbl.add_row(
                    (
                        dev,
                        data["up"],
                        addr["ip"],
                        addr["mask"],
                        addr.get("scope", empty),
                        data["hwaddr"],
                    )
                )
            for addr in data.get("ipv6"):
                tbl.add_row(
                    (
                        dev,
                        data["up"],
                        addr["ip"],
                        empty,
                        addr.get("scope6", empty),
                        data["hwaddr"],
                    )
                )
            if len(data.get("ipv6")) + len(data.get("ipv4")) == 0:
                tbl.add_row(
                    (dev, data["up"], empty, empty, empty, data["hwaddr"])
                )
        netdev_s = tbl.get_string()
        max_len = len(max(netdev_s.splitlines(), key=len))
        header = util.center("Net device info", "+", max_len)
        lines.extend([header, netdev_s])
    return "\n".join(lines) + "\n"
Пример #11
0
 def test_get_string(self):
     """get_string() method returns the same content as str()."""
     table = SimpleTable(AUTHORIZED_KEYS_FIELDS)
     for row in AUTHORIZED_KEYS_ROWS:
         table.add_row(row)
     self.assertEqual(table.get_string(), str(table))
Пример #12
0
 def test_authorized_keys(self):
     """SSH authorized keys are rendered as they were with PrettyTable."""
     table = SimpleTable(AUTHORIZED_KEYS_FIELDS)
     for row in AUTHORIZED_KEYS_ROWS:
         table.add_row(row)
Пример #13
0
 def test_route_ipv4(self):
     """Route IPv4 info is rendered as it was with PrettyTable."""
     table = SimpleTable(ROUTE_IPV4_FIELDS)
     for row in ROUTE_IPV4_ROWS:
         table.add_row(row)
     self.assertEqual(str(table), ROUTE_IPV4_TABLE)
Пример #14
0
 def test_net_dev(self):
     """Net device info is rendered as it was with PrettyTable."""
     table = SimpleTable(NET_DEVICE_FIELDS)
     for row in NET_DEVICE_ROWS:
         table.add_row(row)
     self.assertEqual(str(table), NET_DEVICE_TABLE)
Пример #15
0
 def test_authorized_keys(self):
     """SSH authorized keys are rendered as they were with PrettyTable."""
     table = SimpleTable(AUTHORIZED_KEYS_FIELDS)
     for row in AUTHORIZED_KEYS_ROWS:
         table.add_row(row)
Пример #16
0
 def test_route_ipv4(self):
     """Route IPv4 info is rendered as it was with PrettyTable."""
     table = SimpleTable(ROUTE_IPV4_FIELDS)
     for row in ROUTE_IPV4_ROWS:
         table.add_row(row)
     self.assertEqual(str(table), ROUTE_IPV4_TABLE)
Пример #17
0
 def test_net_dev(self):
     """Net device info is rendered as it was with PrettyTable."""
     table = SimpleTable(NET_DEVICE_FIELDS)
     for row in NET_DEVICE_ROWS:
         table.add_row(row)
     self.assertEqual(str(table), NET_DEVICE_TABLE)
Пример #18
0
 def test_get_string(self):
     """get_string() method returns the same content as str()."""
     table = SimpleTable(AUTHORIZED_KEYS_FIELDS)
     for row in AUTHORIZED_KEYS_ROWS:
         table.add_row(row)
     self.assertEqual(table.get_string(), str(table))