Skip to main content
The net module provides comprehensive networking utilities for checking connectivity, working with IP addresses, performing DNS queries, and making HTTP requests.
Different functions require different tools (curl/wget, nc, dig/nslookup). Functions gracefully degrade and provide helpful error messages when tools are missing.

Connectivity

Functions for testing network connectivity.
Check if the system has internet connectivity.
net::is_online && echo "Connected to internet"
Tries multiple DNS endpoints (8.8.8.8, 1.1.1.1, 9.9.9.9) for reliability.Returns: Exit code 0 if online
Check if a specific host is reachable.
net::can_reach "github.com" && echo "GitHub is reachable"
net::can_reach "internal.server" 5  # 5 second timeout
Parameters:
  • host - Hostname or IP address
  • timeout - Timeout in seconds (default: 2)
Returns: Exit code 0 if reachable
Ping a host and return average round-trip time.
latency=$(net::ping "google.com")
echo "Latency: ${latency}ms"

latency=$(net::ping "server.local" 10)
Parameters:
  • host - Hostname or IP address
  • count - Number of pings (default: 4)
Returns: Average RTT in milliseconds

Port operations

Functions for checking and scanning ports.
Check if a TCP port is open.
net::port::is_open "localhost" 8080 && echo "Server is running"
net::port::is_open "192.168.1.1" 22 1  # Quick check with 1s timeout
Uses nc if available, falls back to pure bash /dev/tcp trick.Parameters:
  • host - Hostname or IP address
  • port - Port number
  • timeout - Timeout in seconds (default: 2)
Returns: Exit code 0 if port is open
Wait until a port is open (useful for service readiness).
# Wait for database to be ready
if net::port::wait "localhost" 5432 30; then
    echo "Database is ready"
else
    echo "Database failed to start within 30 seconds"
fi
Parameters:
  • host - Hostname or IP address
  • port - Port number
  • timeout - Maximum wait time in seconds (default: 30)
  • interval - Check interval in seconds (default: 1)
Returns: Exit code 0 if port opens within timeout
Scan a range of ports and print open ones.
# Scan common ports
net::port::scan "localhost" 1 1024

# Scan specific range
net::port::scan "192.168.1.1" 8000 9000
Parameters:
  • host - Hostname or IP address
  • start_port - First port to scan (default: 1)
  • end_port - Last port to scan (default: 1024)
Returns: List of open ports (one per line)
Scanning large port ranges can be slow. Use responsibly.

IP address operations

Functions for working with IP addresses.
Get the local IP address (first non-loopback).
ip=$(net::ip::local)
echo "Local IP: $ip"
Uses ip command if available, falls back to ifconfig.Returns: Local IP address
Get the public IP address.
public=$(net::ip::public)
echo "Public IP: $public"
Tries multiple services with fallback: ipify.org, ifconfig.me, icanhazip.com, checkip.amazonaws.com.Returns: Public IP address
Get all local IP addresses.
net::ip::all
# 192.168.1.100
# 10.0.0.5
# 172.17.0.1
Returns: All local IPs (one per line)
Check if a string is a valid IPv4 address.
net::ip::is_valid_v4 "192.168.1.1" && echo "Valid IPv4"
net::ip::is_valid_v4 "999.999.999.999" || echo "Invalid"
Validates both format and octet ranges (0-255).Parameters:
  • ip - IP address string to validate
Returns: Exit code 0 if valid IPv4
Check if a string is a valid IPv6 address (basic check).
net::ip::is_valid_v6 "2001:0db8::1" && echo "Valid IPv6"
Parameters:
  • ip - IP address string to validate
Returns: Exit code 0 if valid IPv6 format
Check if an IP is in a private range.
net::ip::is_private "192.168.1.1" && echo "Private IP"
net::ip::is_private "8.8.8.8" || echo "Public IP"
Checks for 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16.Parameters:
  • ip - IPv4 address
Returns: Exit code 0 if private
Check if an IP is a loopback address.
net::ip::is_loopback "127.0.0.1" && echo "Loopback"
net::ip::is_loopback "::1" && echo "IPv6 loopback"
Parameters:
  • ip - IP address
Returns: Exit code 0 if loopback

DNS operations

Functions for DNS queries and hostname resolution.
Get the system hostname.
host=$(net::hostname)
echo "Hostname: $host"
Returns: System hostname
Get the fully qualified domain name.
fqdn=$(net::hostname::fqdn)
echo "FQDN: $fqdn"
Returns: Fully qualified domain name
Resolve hostname to IP address.
ip=$(net::resolve "github.com")
echo "GitHub IP: $ip"
Tries dig, nslookup, getent, then ping for maximum compatibility.Parameters:
  • hostname - Hostname to resolve
Returns: IP address
Reverse DNS lookup — IP to hostname.
hostname=$(net::resolve::reverse "8.8.8.8")
echo "$hostname"  # dns.google
Parameters:
  • ip - IP address
Returns: Hostname
Get DNS records of a specific type.
net::dns::records "example.com" A
net::dns::records "example.com" AAAA
net::dns::records "example.com" CNAME
Parameters:
  • hostname - Domain name
  • type - Record type (default: A)
Returns: DNS records
Get MX (mail) records.
net::dns::mx "gmail.com"
Parameters:
  • domain - Domain name
Returns: MX records with priority
Get TXT records (SPF, DKIM, etc.).
net::dns::txt "example.com"
Parameters:
  • domain - Domain name
Returns: TXT records
Get nameserver records.
net::dns::ns "example.com"
Parameters:
  • domain - Domain name
Returns: NS records
Check DNS propagation across multiple public resolvers.
net::dns::propagation "example.com"
# Google       93.184.216.34
# Cloudflare   93.184.216.34
# Quad9        93.184.216.34
# OpenDNS      93.184.216.34
Queries Google (8.8.8.8), Cloudflare (1.1.1.1), Quad9 (9.9.9.9), and OpenDNS.Parameters:
  • hostname - Domain name to check
Returns: Formatted list of results from each resolver
Useful for verifying DNS changes have propagated globally.

Usage example

source compiled.sh

# Check internet connectivity
if net::is_online; then
    echo "Internet connection: OK"
    
    # Get IP addresses
    local_ip=$(net::ip::local)
    public_ip=$(net::ip::public)
    echo "Local IP: $local_ip"
    echo "Public IP: $public_ip"
    
    # Check if IP is private
    if net::ip::is_private "$local_ip"; then
        echo "Using private network"
    fi
    
    # DNS lookup
    github_ip=$(net::resolve "github.com")
    echo "GitHub resolves to: $github_ip"
    
    # Check if service is available
    if net::port::is_open "github.com" 443; then
        echo "GitHub HTTPS is accessible"
    fi
else
    echo "No internet connection"
fi

# Wait for local service to start
echo "Starting database..."
if net::port::wait "localhost" 5432 30; then
    echo "Database is ready"
else
    echo "Database failed to start"
    exit 1
fi

Build docs developers (and LLMs) love