mt.net

Submodules

Functions

mt.net.get_default_ifaces()

Returns a list of (host_ip_addr, subnet, broadcast, gateway_ip_address, iface) tuples of default ifaces.

mt.net.is_port_open(addr, port, timeout=2.0)

Checks if a port is open, with timeout.

Parameters:
  • addr (str) – ip address, hostname or fqdn

  • port (int) – port number

  • time_out (float) – timeout in seconds

Returns:

whether or not the port at the given address is open

Return type:

bool

mt.net.get_hostname()

Returns the machine’s hostname.

mt.net.get_username()

Returns the current username.

Returns:

the current username

Return type:

str

mt.net.get_all_hosts_from_network(ip_network)

Gets all hosts (ip_addr, mac_addr) from a given ip network.

Parameters:

ip_network (ipaddress.Ipv4Network) – IP network

Returns:

list of (ip_addr -> ipaddress.Ipv4Address, mac_addr -> str) pairs of detected hosts in the subnet.

Return type:

list

mt.net.get_all_inet4_ipaddresses()

Returns all network INET4 interfaces’ IP addresses+netmasks.

Returns:

A dictionary of interface_name -> (ip_address, netmask)

Return type:

dict

mt.net.get_public_ip_address()

Obtains the public IP address using AWS.

Returns:

public ip address of the current host

Return type:

ipaddress.Ipv4Address

mt.net.listen_to_port(listen_config: str, blocking: bool = True, logger: IndentedLoggerAdapter | None = None) socket

Listens to a local port, returning the listening socket.

The function repeats indefinitely until it can open the port.

Parameters:
  • listen_config (str) – listening config as an ‘addr:port’ pair. For example, ‘:30443’, ‘0.0.0.0:324’, ‘localhost:345’, etc.

  • blocking (bool) – whether or not the returning socket is blocking

  • logger (mt.logg.IndentedLoggerAdapter, optional) – logger for debugging purposes

Returns:

dock_socket – the output listening socket. None may be returned if something wrong has happened.

Return type:

socket.socket, optional

mt.net.set_keepalive_linux(sock, after_idle_sec=1, interval_sec=3, max_fails=5)

Set TCP keepalive on an open socket.

It activates after 1 second (after_idle_sec) of idleness, then sends a keepalive ping once every 3 seconds (interval_sec), and closes the connection after 5 failed ping (max_fails), or 15 seconds

mt.net.set_keepalive_osx(sock, after_idle_sec=1, interval_sec=3, max_fails=5)

Set TCP keepalive on an open socket.

sends a keepalive ping once every 3 seconds (interval_sec)

mt.net.launch_port_forwarder(listen_config, connect_configs, timeout=30, logger: IndentedLoggerAdapter | None = None)

Launchs in other threads a port forwarding service.

Parameters:
  • listen_config (str) – listening config as an ‘addr:port’ pair. For example, ‘:30443’, ‘0.0.0.0:324’, ‘localhost:345’, etc.

  • connect_configs (iterable) – list of connecting configs, each of which is an ‘addr:port’ pair. For example, ‘home2.sdfamily.co.uk:443’, etc. Special case ‘::1:port’ stands for localhost in ipv6 with a specific port.

  • timeout (int) – number of seconds for connection timeout

  • logger (mt.logg.IndentedLoggerAdapter, optional) – logger for debugging purposes

async mt.net.port_forwarder_actx(listen_config, connect_configs, logger: IndentedLoggerAdapter | None = None) Server

Launches an asynchronous port forwarding server.

Parameters:
  • listen_config (str) – listening config as an ‘addr:port’ pair. For example, ‘:30443’, ‘0.0.0.0:324’, ‘localhost:345’, etc.

  • connect_configs (iterable) – list of connecting configs, each of which is an ‘addr:port’ pair. For example, ‘home2.sdfamily.co.uk:443’, etc. Special case ‘::1:port’ stands for localhost in ipv6 with a specific port.

  • logger (mt.logg.IndentedLoggerAdapter, optional) – logger for debugging purposes

Returns:

server – the port forwarding server that can be used as an asynchronous context

Return type:

asyncio.base_events.Server

mt.net.launch_ssh_forwarder(listen_config, ssh_tunnel_forwarder, timeout=30, logger: IndentedLoggerAdapter | None = None)

Launchs in other threads a port forwarding service via SSH tunnel.

Parameters:
  • listen_config (str) – listening config as an ‘addr:port’ pair. For example, ‘:30443’, ‘0.0.0.0:324’, ‘localhost:345’, etc.

  • ssh_tunnel_forwarder (sshtunnel.SSHTunnelForwarder) – a stopped SSHTunnelForwarder instance

  • timeout (int) – number of seconds for connection timeout

  • logger (mt.logg.IndentedLoggerAdapter, optional) – logger for debugging purposes

Classes

class mt.net.HostPort(port: int, host_addr: IPv4Address | IPv6Address | None = None, host_name: str | None = None)

Pair of host and port, where host can be by name or by ip address.

The port must be given. As for the host, if an ip address is given, it has the priority. If not, the host name is taken. If even the host name is not provided, it is assumed that the host is localhost.

Parameters:
  • port (int) – the port number

  • host_addr (ipaddress.IPv4Address or ipaddress.IPv6Address or None) – the host address, if known

  • host_name (str, optional) – the host name, if known

Inheritance

digraph inheritance4a4132a4eb { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "HostPort" [URL="#mt.net.HostPort",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Pair of host and port, where host can be by name or by ip address."]; }
classmethod from_str(s: str)

Deserializes from a string.

socket_address() Tuple[str, int]

Returns the (addr, port) pair for socket programming.

to_str() str

Serializes to a string.

class mt.net.SSHTunnelWatcher(ssh_tunnel_forwarder, logger=None)

Inheritance

digraph inheritance76cabd5225 { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "SSHTunnelWatcher" [URL="#mt.net.SSHTunnelWatcher",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top"]; }