CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-github-oshi--oshi-core

JNA-based operating system and hardware information library for Java providing cross-platform access to system metrics

Pending
Overview
Eval results
Files

network.mddocs/

Network Information

Network interface details, IP configuration, and network statistics including bandwidth usage and connection information.

Network Interface Information

Access to network interfaces through the HardwareAbstractionLayer.

interface HardwareAbstractionLayer {
    List<NetworkIF> getNetworkIFs();
    List<NetworkIF> getNetworkIFs(boolean includeLocalInterfaces);
}

Methods

  • getNetworkIFs() - Gets non-local network interfaces (excludes loopback and interfaces with no hardware address)
  • getNetworkIFs(boolean includeLocalInterfaces) - Gets network interfaces with option to include local interfaces

NetworkIF Interface

Comprehensive network interface information including configuration and statistics.

interface NetworkIF {
    // Interface identification
    String getName();
    String getDisplayName();
    int getIndex();
    long getMTU();
    String getMacaddr();
    IfType getIfType();
    
    // IP configuration
    String[] getIPv4addr();
    Short[] getSubnetMasks();
    String[] getIPv6addr();
    Short[] getPrefixLengths();
    
    // Network statistics
    long getBytesRecv();
    long getBytesSent();
    long getPacketsRecv();
    long getPacketsSent();
    long getInErrors();
    long getOutErrors();
    long getInDrops();
    long getCollisions();
    long getSpeed();
    long getTimeStamp();
    
    // Utility methods
    boolean isKnownVmMacAddr();
    boolean updateAttributes();
    
    enum IfType {
        OTHER, REGULAR1822, HDH1822, DDN_X25, RFC877X25, ETHERNET_CSMACD,
        ISO88023_CSMACD, ISO88024_TOKENBUS, ISO88025_TOKENRING, ISO88026_MAN,
        STARLAN, PROTEON_10MBIT, PROTEON_80MBIT, HYPERCHANNEL, FDDI, LAPB,
        SDLC, DS1, E1, BASIC_ISDN, PRIMARY_ISDN, PROP_PTP_SERIAL, PPP,
        SOFTWARE_LOOPBACK, EON, ETHERNET_3MBIT, NSIP, SLIP, ULTRA, DS3, SIP,
        FRAMERELAY, RS232, PARA, ARCNET, ARCNETPLUS, ATM, MIOX25, SONET,
        X25PLE, ISO88022LLC, LOCALTALK, SMDSDXI, FRAMERELAYSERVICE, V35,
        HSSI, HIPPI, MODEM, AAL5, SONETPATH, SONET_VT, SMDS_ICIP, PROP_VIRTUAL,
        PROP_MULTIPLEXOR, IEEE80212, FIBRECHANNEL, HIPPIINTERFACE, FRAMERELAYINTERCONNECT,
        AFLANE_8023, AFLANE_8025, CCTEMUL, FASTETHER, ISDN, V11, V36, G703_64K,
        G703_2MB, QLLC, FASTETHERFX, CHANNEL, IEEE80211, IBM370PARCHAN,
        ESCON, DLSW, ISDN_S, ISDN_U, LAPD, IPSWITCH, RSRB, ATM_LOGICAL,
        DS0, DS0_BUNDLE, BSC, ASYNC, CNR, ISO88025R_DTR, EPLRS, ARAP,
        PROP_CNLS, HOSTPAD, TERMPAD, FRAMERELAY_MPI, X213, ADSL, RADSL,
        SDSL, VDSL, ISO88025_CRFPRINT, MYRINET, VOICE_EM, VOICE_FXO,
        VOICE_FXS, VOICE_ENCAP, VOICE_OVERIP, ATM_DXI, ATM_FUNI, ATM_IMA,
        PPPMULTILINKBUNDLE, IPOVERCDLC, IPOVERCLAW, STACKTOSTACK, VIRTUALIPADDRESS,
        MPC, IPOVERATM, ISO88025_FIBER, TDLC, GIGABITETHERNET, HDLC, LAPF,
        V37, X25MLP, X25HUNTGROUP, TRANSPHDLC, INTERLEAVE, FAST, IP,
        DOCSCABLEMACLAYER, DOCSCABLEDOWNSTREAM, DOCSCABLEUPSTREAM, A12MPPSWITCH,
        TUNNEL, COFFEE, CES, ATM_SUBINTERFACE, L2_VLAN, L3_IPVLAN, L3_IPXVLAN,
        DIGITALPOWERLINE, MEDIAMAILOVERIP, DTM, DCN, IPFORWARD, MSDSL,
        IEEE1394, IF_GSN, DVBRCC_MACLAYER, DVBRCC_DOWNSTREAM, DVBRCC_UPSTREAM,
        ATM_VIRTUAL, MPLS_TUNNEL, SRP, VOICEOVERATM, VOICEOVERFRAMERELAY,
        IDSL, COMPOSITELINK, SS7_SIGLINK, PROP_WIRELESS_P2P, FR_FORWARD,
        RFC1483, USB, IEEE8023AD_LAG, BGP_POLICY_ACCOUNTING, FRF16_MFR_BUNDLE,
        H323_GATEKEEPER, H323_PROXY, MPLS, MF_SIGLINK, HDSL2, SHDSL,
        DS1_FDL, POS, DVB_ASI_IN, DVB_ASI_OUT, PLC, NFAS, TR008, GR303_RDT,
        GR303_IDT, ISUP, PROP_DOCSWIRELESS_MACLAYER, PROP_DOCSWIRELESS_DOWNSTREAM,
        PROP_DOCSWIRELESS_UPSTREAM, HIPERLAN2, PROP_BWA_P2MP, SONET_OVERHEAD_CHANNEL,
        DIGITAL_WRAPPER_OVERHEAD_CHANNEL, AAL2, RADIO_MAC, ATM_RADIO,
        IMT, MVL, REACH_DSL, FR_DLCI_ENDPT, ATM_VCI_ENDPT, OPTICAL_CHANNEL,
        OPTICAL_TRANSPORT, IEEE80216_WMAN, ADSL2, MACSECCONTROLLEDIF,
        MACSECUNCONTROLLEDIF, AVICI_OPTICAL_ETHER, ATMBOND, VOICE_FGDN,
        VOICE_FGDS, VOICE_FGD, ISDN_Q921, ISDN_Q931, ISDN_QSIG, LAN,
        WIRELESS_LAN, BRIDGE, LINECONFIG, WWAN, WWANPP, WWANPP2, VOICEOTHER,
        OTHER_WWAN, UNKNOWN
    }
}

Network Parameters

Network configuration information through the OperatingSystem interface.

interface NetworkParams {
    String getHostName();
    String getDomainName();
    String[] getDnsServers();
    String getIpv4DefaultGateway();
    String getIpv6DefaultGateway();
}

Internet Protocol Statistics

Network protocol statistics and active connections.

interface InternetProtocolStats {
    TcpStats getTCPv4Stats();
    TcpStats getTCPv6Stats();
    UdpStats getUDPv4Stats();
    UdpStats getUDPv6Stats();
    List<IPConnection> getConnections();
    
    class TcpStats {
        long getConnectionsEstablished();
        long getConnectionsActive();
        long getConnectionsPassive();
        long getConnectionFailures();
        long getConnectionsReset();
        long getSegmentsSent();
        long getSegmentsReceived();
        long getSegmentsRetransmitted();
        long getInErrors();
        long getOutRsts();
    }
    
    class UdpStats {
        long getDatagramsSent();
        long getDatagramsReceived();
        long getDatagramsNoPort();
        long getDatagramsReceivedErrors();
    }
    
    class IPConnection {
        String getType();
        String getLocalAddress();
        int getLocalPort();
        String getForeignAddress();
        int getForeignPort();
        TcpState getState();
        int getOwningProcessId();
        
        enum TcpState {
            CLOSED, LISTEN, SYN_SENT, SYN_RECV, ESTABLISHED, FIN_WAIT_1,
            FIN_WAIT_2, CLOSE_WAIT, CLOSING, LAST_ACK, TIME_WAIT, DELETE_TCB,
            NONE, UNKNOWN
        }
    }
}

Usage Examples

List Network Interfaces

import oshi.SystemInfo;
import oshi.hardware.NetworkIF;

SystemInfo si = new SystemInfo();
List<NetworkIF> networkIFs = si.getHardware().getNetworkIFs();

System.out.println("=== Network Interfaces ===");
for (NetworkIF netIF : networkIFs) {
    System.out.println("Interface: " + netIF.getDisplayName());
    System.out.println("  Name: " + netIF.getName());
    System.out.println("  MAC Address: " + netIF.getMacaddr());
    System.out.println("  MTU: " + netIF.getMTU());
    System.out.println("  Speed: " + formatSpeed(netIF.getSpeed()));
    System.out.println("  Type: " + netIF.getIfType());
    
    // IPv4 addresses
    String[] ipv4Addresses = netIF.getIPv4addr();
    if (ipv4Addresses.length > 0) {
        System.out.println("  IPv4 Addresses: " + Arrays.toString(ipv4Addresses));
    }
    
    // IPv6 addresses
    String[] ipv6Addresses = netIF.getIPv6addr();
    if (ipv6Addresses.length > 0) {
        System.out.println("  IPv6 Addresses: " + Arrays.toString(ipv6Addresses));
    }
    
    System.out.println();
}

private static String formatSpeed(long bps) {
    if (bps <= 0) return "Unknown";
    if (bps < 1_000_000) return (bps / 1_000) + " Kbps";
    if (bps < 1_000_000_000) return (bps / 1_000_000) + " Mbps";
    return String.format("%.1f Gbps", bps / 1_000_000_000.0);
}

Network Interface Statistics

import oshi.SystemInfo;
import oshi.hardware.NetworkIF;

SystemInfo si = new SystemInfo();
List<NetworkIF> networkIFs = si.getHardware().getNetworkIFs();

System.out.println("=== Network Interface Statistics ===");
System.out.printf("%-20s %12s %12s %12s %12s %8s %8s%n",
    "Interface", "Bytes Recv", "Bytes Sent", "Packets Recv", "Packets Sent", "In Errors", "Out Errors");
System.out.println("-".repeat(100));

for (NetworkIF netIF : networkIFs) {
    System.out.printf("%-20s %12s %12s %12s %12s %8d %8d%n",
        netIF.getName(),
        formatBytes(netIF.getBytesRecv()),
        formatBytes(netIF.getBytesSent()),
        netIF.getPacketsRecv(),
        netIF.getPacketsSent(),
        netIF.getInErrors(),
        netIF.getOutErrors()
    );
}

private static String formatBytes(long bytes) {
    if (bytes < 1024) return bytes + " B";
    if (bytes < 1024 * 1024) return String.format("%.1f KB", bytes / 1024.0);
    if (bytes < 1024L * 1024L * 1024L) return String.format("%.1f MB", bytes / 1024.0 / 1024.0);
    return String.format("%.1f GB", bytes / 1024.0 / 1024.0 / 1024.0);
}

Network Configuration

import oshi.SystemInfo;
import oshi.software.os.NetworkParams;

SystemInfo si = new SystemInfo();
NetworkParams netParams = si.getOperatingSystem().getNetworkParams();

System.out.println("=== Network Configuration ===");
System.out.println("Hostname: " + netParams.getHostName());
System.out.println("Domain Name: " + netParams.getDomainName());
System.out.println("IPv4 Default Gateway: " + netParams.getIpv4DefaultGateway());
System.out.println("IPv6 Default Gateway: " + netParams.getIpv6DefaultGateway());

String[] dnsServers = netParams.getDnsServers();
System.out.println("DNS Servers:");
for (String dns : dnsServers) {
    System.out.println("  " + dns);
}

TCP/UDP Statistics

import oshi.SystemInfo;
import oshi.software.os.InternetProtocolStats;

SystemInfo si = new SystemInfo();
InternetProtocolStats ipStats = si.getOperatingSystem().getInternetProtocolStats();

System.out.println("=== TCP Statistics ===");
InternetProtocolStats.TcpStats tcpv4 = ipStats.getTCPv4Stats();
System.out.println("TCPv4:");
System.out.println("  Connections Established: " + tcpv4.getConnectionsEstablished());
System.out.println("  Active Connections: " + tcpv4.getConnectionsActive());
System.out.println("  Passive Connections: " + tcpv4.getConnectionsPassive());
System.out.println("  Connection Failures: " + tcpv4.getConnectionFailures());
System.out.println("  Connections Reset: " + tcpv4.getConnectionsReset());
System.out.println("  Segments Sent: " + tcpv4.getSegmentsSent());
System.out.println("  Segments Received: " + tcpv4.getSegmentsReceived());
System.out.println("  Segments Retransmitted: " + tcpv4.getSegmentsRetransmitted());

InternetProtocolStats.TcpStats tcpv6 = ipStats.getTCPv6Stats();
System.out.println("TCPv6:");
System.out.println("  Connections Established: " + tcpv6.getConnectionsEstablished());
System.out.println("  Segments Sent: " + tcpv6.getSegmentsSent());
System.out.println("  Segments Received: " + tcpv6.getSegmentsReceived());

System.out.println("\n=== UDP Statistics ===");
InternetProtocolStats.UdpStats udpv4 = ipStats.getUDPv4Stats();
System.out.println("UDPv4:");
System.out.println("  Datagrams Sent: " + udpv4.getDatagramsSent());
System.out.println("  Datagrams Received: " + udpv4.getDatagramsReceived());
System.out.println("  Datagrams No Port: " + udpv4.getDatagramsNoPort());
System.out.println("  Datagram Errors: " + udpv4.getDatagramsReceivedErrors());

InternetProtocolStats.UdpStats udpv6 = ipStats.getUDPv6Stats();
System.out.println("UDPv6:");
System.out.println("  Datagrams Sent: " + udpv6.getDatagramsSent());
System.out.println("  Datagrams Received: " + udpv6.getDatagramsReceived());

Active Network Connections

import oshi.SystemInfo;
import oshi.software.os.InternetProtocolStats;
import oshi.software.os.InternetProtocolStats.IPConnection;

SystemInfo si = new SystemInfo();
InternetProtocolStats ipStats = si.getOperatingSystem().getInternetProtocolStats();

List<IPConnection> connections = ipStats.getConnections();

System.out.println("=== Active Network Connections ===");
System.out.printf("%-8s %-20s %-6s %-20s %-6s %-12s %s%n",
    "Type", "Local Address", "Port", "Remote Address", "Port", "State", "PID");
System.out.println("-".repeat(90));

for (IPConnection conn : connections) {
    System.out.printf("%-8s %-20s %-6d %-20s %-6d %-12s %d%n",
        conn.getType(),
        conn.getLocalAddress(),
        conn.getLocalPort(),
        conn.getForeignAddress(),
        conn.getForeignPort(),
        conn.getState(),
        conn.getOwningProcessId()
    );
}

Network Bandwidth Monitoring

import oshi.SystemInfo;
import oshi.hardware.NetworkIF;

SystemInfo si = new SystemInfo();

// Find active network interface (first non-loopback with traffic)
NetworkIF activeInterface = null;
for (NetworkIF netIF : si.getHardware().getNetworkIFs(true)) {
    if (!netIF.getName().toLowerCase().contains("loopback") && 
        (netIF.getBytesRecv() > 0 || netIF.getBytesSent() > 0)) {
        activeInterface = netIF;
        break;
    }
}

if (activeInterface != null) {
    System.out.println("Monitoring bandwidth for: " + activeInterface.getDisplayName());
    
    // Initial measurement
    long initialRecv = activeInterface.getBytesRecv();
    long initialSent = activeInterface.getBytesSent();
    long initialTime = System.currentTimeMillis();
    
    try {
        Thread.sleep(5000); // Wait 5 seconds
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    
    // Update and measure again
    if (activeInterface.updateAttributes()) {
        long currentRecv = activeInterface.getBytesRecv();
        long currentSent = activeInterface.getBytesSent();
        long currentTime = System.currentTimeMillis();
        
        long deltaRecv = currentRecv - initialRecv;
        long deltaSent = currentSent - initialSent;
        long deltaTime = currentTime - initialTime;
        
        // Calculate rates in bytes per second
        double recvRate = deltaRecv * 1000.0 / deltaTime;
        double sentRate = deltaSent * 1000.0 / deltaTime;
        
        System.out.printf("Download rate: %s/s%n", formatBytes((long)recvRate));
        System.out.printf("Upload rate: %s/s%n", formatBytes((long)sentRate));
        System.out.printf("Total received: %s%n", formatBytes(currentRecv));
        System.out.printf("Total sent: %s%n", formatBytes(currentSent));
    }
}

Find Network Interface by Name

import oshi.SystemInfo;
import oshi.hardware.NetworkIF;

SystemInfo si = new SystemInfo();
String targetInterface = "eth0"; // or "en0" on macOS, "Ethernet" on Windows

NetworkIF targetNetIF = null;
for (NetworkIF netIF : si.getHardware().getNetworkIFs(true)) {
    if (netIF.getName().equals(targetInterface) || 
        netIF.getDisplayName().contains(targetInterface)) {
        targetNetIF = netIF;
        break;
    }
}

if (targetNetIF != null) {
    System.out.println("Found interface: " + targetNetIF.getDisplayName());
    System.out.println("Name: " + targetNetIF.getName());
    System.out.println("MAC Address: " + targetNetIF.getMacaddr());
    System.out.println("Speed: " + formatSpeed(targetNetIF.getSpeed()));
    
    String[] ipv4 = targetNetIF.getIPv4addr();
    if (ipv4.length > 0) {
        System.out.println("IPv4: " + Arrays.toString(ipv4));
    }
    
    String[] ipv6 = targetNetIF.getIPv6addr();
    if (ipv6.length > 0) {
        System.out.println("IPv6: " + Arrays.toString(ipv6));
    }
} else {
    System.out.println("Interface not found: " + targetInterface);
}

Network Interface Types

import oshi.SystemInfo;
import oshi.hardware.NetworkIF;

SystemInfo si = new SystemInfo();
List<NetworkIF> networkIFs = si.getHardware().getNetworkIFs(true);

// Group interfaces by type
Map<NetworkIF.IfType, List<NetworkIF>> interfacesByType = networkIFs.stream()
    .collect(Collectors.groupingBy(NetworkIF::getIfType));

System.out.println("=== Network Interfaces by Type ===");
for (Map.Entry<NetworkIF.IfType, List<NetworkIF>> entry : interfacesByType.entrySet()) {
    NetworkIF.IfType type = entry.getKey();
    List<NetworkIF> interfaces = entry.getValue();
    
    System.out.printf("%s (%d interface%s):%n", 
        type, interfaces.size(), interfaces.size() == 1 ? "" : "s");
    
    for (NetworkIF netIF : interfaces) {
        System.out.printf("  %-20s %s%n", netIF.getName(), netIF.getDisplayName());
    }
    System.out.println();
}

Install with Tessl CLI

npx tessl i tessl/maven-com-github-oshi--oshi-core

docs

file-system.md

hardware.md

index.md

network.md

operating-system.md

process-management.md

system-info.md

tile.json