net

IP addresses, CIDR ranges, DNS lookups and simple TCP checks. The checking functions (tcp_check, check_ports, banner) make ordinary connections and are for defence. Only check computers you own or have written permission to test; checking other people's systems can be against the law.

The IP and CIDR helpers work everywhere, including the playground. Lookups and port checks (resolve, reverse, tcp_check, check_ports, banner, my_ip) need the TB app on your computer.

import net

Example

import net

say(net.is_ip("203.0.113.42"), net.is_private("192.168.1.20"))
say(net.in_cidr("10.1.2.3", "10.0.0.0/8"))
let info = net.cidr_info("192.168.1.0/24")
say(info)
say(net.common_ports[22], net.common_ports[443])

Needs the TB app on your computer, so there is no Run button here.

import net

# Only check computers you own or are allowed to test.
let open = net.check_ports("127.0.0.1", [22, 80, 443, 8000])
for port in open {
  if open[port] {
    say(f"port {port} is open")
  }
}
say(net.resolve("example.com"))

Functions

is_ip(x)

returns bool

True if x is text holding an IPv4 or IPv6 address, like "8.8.8.8" or "::1".

is_ipv4(x)

returns bool

True if x is an IPv4 address like "192.168.1.10".

is_ipv6(x)

returns bool

True if x is an IPv6 address like "2001:db8::1".

is_private(ip)

returns bool

True for addresses that only work inside a home or company network (10.x, 172.16-31.x, 192.168.x, fc00::/7), plus loopback and link-local. Handy to skip internal IPs before looking them up online.

is_loopback(ip)

returns bool

True for this computer talking to itself (127.x.x.x or ::1).

ip_info(ip)

returns map

Facts about an address: {"ip", "version" (4 or 6), "private", "loopback", "multicast", "reserved", "global"}. "global" is true for normal public internet addresses.

in_cidr(ip, cidr)

returns bool

True if ip is inside the range: in_cidr("10.0.0.7", "10.0.0.0/24") is true.

cidr_info(cidr)

returns map

About a range: {"network", "netmask", "broadcast", "prefix", "size" (number of addresses), "first", "last"} (first/last usable host).

cidr_hosts(cidr)

returns list

Every usable host address in a range: cidr_hosts("192.168.1.0/30") is ["192.168.1.1", "192.168.1.2"]. At most 65,536 addresses (a /16), so big ranges give an error.

resolve(host, timeout?)

returns list

The IP addresses of a host name (IPv4 first): resolve("example.com"). Gives an error you can catch if the name doesn't exist. timeout is in seconds (default 5).

reverse(ip, timeout?)

returns text

The host name an IP address points back to (reverse DNS, PTR record), or nil if it has none.

hostname()

returns text

This computer's own name on the network.

my_ip()

returns text

This computer's local IP address on its network (like "192.168.1.23"), or "127.0.0.1" when it isn't connected. It sends nothing over the network; it is not your public IP.

tcp_check(host, port, timeout?)

returns bool

True if something accepts a TCP connection on that port (a normal connect, then it hangs up). timeout is in seconds (default 1). Only check computers you own or have written permission to test; checking other people's systems can be against the law.

check_ports(host, ports, timeout?)

returns map

Try a normal TCP connection to each port and give back a map port -> true/false (open or not), in the order given. ports is a list like [22, 80, 443] or text like "22,80,8000-8010" (at most 1024). Several ports are tried at once; timeout is per port, in seconds (default 1). Only check computers you own or have written permission to test; checking other people's systems can be against the law.

banner(host, port, timeout?)

returns text

Connect and read the greeting a service sends first (SSH, FTP and mail servers do), at most 512 bytes. Gives "" if the service says nothing before the timeout (seconds, default 2). It sends nothing itself. Only check computers you own or have written permission to test; checking other people's systems can be against the law.

Values

NameValueWhat it is
net.common_ports{21: "ftp", 22: "ssh", 23: "telnet", 25: "smtp", 53: "dns", 80: "http", 110: ...A map of well-known port numbers to the service usually found there: net.common_ports[22] is "ssh".