Esempio n. 1
0
    def test_netifaces(self):
        ifaces = get_interfaces()
        if not ifaces:
            return
        ip_ifaces = defaultdict(list)
        for iface in ifaces:
            if if_nametoindex:
                try:
                    i = if_nametoindex(iface)
                except Exception:
                    pass
                else:
                    if if_indextoname:
                        assert if_indextoname(
                            i
                        ) == iface, "expected interface %s for index %i but got %s" % (
                            iface, i, if_indextoname(i))
            ipmasks = do_get_bind_ifacemask(iface)
            for ip, _ in ipmasks:
                ip_ifaces[ip].append(iface)
        for ip, ifaces in ip_ifaces.items():
            assert get_iface(
                ip
            ) in ifaces, "expected interface for ip %s to be one of %s but got %s" % (
                ip, ifaces, get_iface(ip))
        ia = get_interfaces_addresses()
        assert ia
        #for iface, address in ia.items():
        #    iface2 = get_interface(address)
        #    assert iface2==iface, "expected %s but got %s" % (iface, iface2)
        get_gateways()
        get_bind_IPs()
        get_ssl_info()
        get_info()

        if if_indextoname:
            assert if_indextoname(-1) is None

        def invalid_iface(s):
            v = get_iface(s)
            if v:
                raise Exception(
                    "invalid IP '%s' should not return interface '%s'" %
                    (s, v))

        invalid_iface(None)
        invalid_iface("")
        invalid_iface("%")
        invalid_iface(":")
        with silence_error(net_util):
            invalid_iface("INVALIDHOSTNAME")
        invalid_iface("10.0.0")
        get_iface("localhost")

        assert get_interface("invalid") is None
Esempio n. 2
0
def main():
    import sys
    import socket

    from xpra.os_util import POSIX
    from xpra.util import print_nested_dict
    from xpra.net.net_util import import_netifaces, get_interfaces, if_nametoindex
    from xpra.platform import program_context
    from xpra.log import Logger, enable_color, add_debug_category, enable_debug_for
    log = Logger("network")
    with program_context("Network-Device-Info", "Network Device Info"):
        enable_color()
        verbose = "-v" in sys.argv or "--verbose" in sys.argv
        if verbose:
            enable_debug_for("network")
            add_debug_category("network")
            log.enable_debug()

        print("Network interfaces found:")
        netifaces = import_netifaces()
        for iface in get_interfaces():
            if if_nametoindex:
                print("* %s (index=%s)" %
                      (iface.ljust(20), if_nametoindex(iface)))
            else:
                print("* %s" % iface)
            addresses = netifaces.ifaddresses(iface)  #@UndefinedVariable
            for addr, defs in addresses.items():
                if addr in (socket.AF_INET, socket.AF_INET6):
                    for d in defs:
                        ip = d.get("addr")
                        if ip:
                            stype = {
                                socket.AF_INET: "IPv4",
                                socket.AF_INET6: "IPv6",
                            }[addr]
                            print(" * %s:     %s" % (stype, ip))
                            if POSIX:
                                from xpra.net.socket_util import create_tcp_socket
                                try:
                                    sock = create_tcp_socket(ip, 0)
                                    sockfd = sock.fileno()
                                    info = get_interface_info(sockfd, iface)
                                    if info:
                                        print_nested_dict(info,
                                                          prefix="    ",
                                                          lchar="-")
                                finally:
                                    sock.close()
            if not POSIX:
                info = get_interface_info(0, iface)
                if info:
                    print("  %s" % info)
Esempio n. 3
0
def main():
    from xpra.platform import program_context
    with program_context("Network-Speed", "Network Speed Query Tool"):
        from xpra.net.net_util import get_interfaces
        from xpra.simple_stats import std_unit
        interfaces = get_interfaces()
        for iface in interfaces:
            speed = get_interface_info(0, iface).get("speed", 0)
            try:
                v = int(speed)
                s = "%sbps" % std_unit(v)
                print("%s : %s" % (iface, s))
            except ValueError:
                logger().error("Error: parsing speed value '%s'", speed, exc_info=True)
Esempio n. 4
0
 def test_netifaces(self):
     ifaces = get_interfaces()
     if not ifaces:
         return
     for iface in ifaces:
         do_get_bind_ifacemask(iface)
     ia = get_interfaces_addresses()
     assert ia
     #for iface, address in ia.items():
     #    iface2 = get_interface(address)
     #    assert iface2==iface, "expected %s but got %s" % (iface, iface2)
     get_gateways()
     get_bind_IPs()
     get_ssl_info()
     get_info()
Esempio n. 5
0
def main():
    import sys
    from xpra.net.net_util import get_interfaces, if_nametoindex
    from xpra.platform import program_context
    from xpra.log import Logger, enable_color, add_debug_category, enable_debug_for
    log = Logger("network")
    with program_context("Network-Device-Info", "Network Device Info"):
        enable_color()
        verbose = "-v" in sys.argv or "--verbose" in sys.argv
        if verbose:
            enable_debug_for("network")
            add_debug_category("network")
            log.enable_debug()

        print("Network interfaces found:")
        for iface in get_interfaces():
            if if_nametoindex:
                print("* %s (index=%s)" %
                      (iface.ljust(20), if_nametoindex(iface)))
            else:
                print("* %s" % iface)
            print_iface(iface)