0
# Network Information
1
2
Network interface details, IP configuration, and network statistics including bandwidth usage and connection information.
3
4
## Network Interface Information
5
6
Access to network interfaces through the HardwareAbstractionLayer.
7
8
```java { .api }
9
interface HardwareAbstractionLayer {
10
List<NetworkIF> getNetworkIFs();
11
List<NetworkIF> getNetworkIFs(boolean includeLocalInterfaces);
12
}
13
```
14
15
### Methods
16
17
- **`getNetworkIFs()`** - Gets non-local network interfaces (excludes loopback and interfaces with no hardware address)
18
- **`getNetworkIFs(boolean includeLocalInterfaces)`** - Gets network interfaces with option to include local interfaces
19
20
## NetworkIF Interface
21
22
Comprehensive network interface information including configuration and statistics.
23
24
```java { .api }
25
interface NetworkIF {
26
// Interface identification
27
String getName();
28
String getDisplayName();
29
int getIndex();
30
long getMTU();
31
String getMacaddr();
32
IfType getIfType();
33
34
// IP configuration
35
String[] getIPv4addr();
36
Short[] getSubnetMasks();
37
String[] getIPv6addr();
38
Short[] getPrefixLengths();
39
40
// Network statistics
41
long getBytesRecv();
42
long getBytesSent();
43
long getPacketsRecv();
44
long getPacketsSent();
45
long getInErrors();
46
long getOutErrors();
47
long getInDrops();
48
long getCollisions();
49
long getSpeed();
50
long getTimeStamp();
51
52
// Utility methods
53
boolean isKnownVmMacAddr();
54
boolean updateAttributes();
55
56
enum IfType {
57
OTHER, REGULAR1822, HDH1822, DDN_X25, RFC877X25, ETHERNET_CSMACD,
58
ISO88023_CSMACD, ISO88024_TOKENBUS, ISO88025_TOKENRING, ISO88026_MAN,
59
STARLAN, PROTEON_10MBIT, PROTEON_80MBIT, HYPERCHANNEL, FDDI, LAPB,
60
SDLC, DS1, E1, BASIC_ISDN, PRIMARY_ISDN, PROP_PTP_SERIAL, PPP,
61
SOFTWARE_LOOPBACK, EON, ETHERNET_3MBIT, NSIP, SLIP, ULTRA, DS3, SIP,
62
FRAMERELAY, RS232, PARA, ARCNET, ARCNETPLUS, ATM, MIOX25, SONET,
63
X25PLE, ISO88022LLC, LOCALTALK, SMDSDXI, FRAMERELAYSERVICE, V35,
64
HSSI, HIPPI, MODEM, AAL5, SONETPATH, SONET_VT, SMDS_ICIP, PROP_VIRTUAL,
65
PROP_MULTIPLEXOR, IEEE80212, FIBRECHANNEL, HIPPIINTERFACE, FRAMERELAYINTERCONNECT,
66
AFLANE_8023, AFLANE_8025, CCTEMUL, FASTETHER, ISDN, V11, V36, G703_64K,
67
G703_2MB, QLLC, FASTETHERFX, CHANNEL, IEEE80211, IBM370PARCHAN,
68
ESCON, DLSW, ISDN_S, ISDN_U, LAPD, IPSWITCH, RSRB, ATM_LOGICAL,
69
DS0, DS0_BUNDLE, BSC, ASYNC, CNR, ISO88025R_DTR, EPLRS, ARAP,
70
PROP_CNLS, HOSTPAD, TERMPAD, FRAMERELAY_MPI, X213, ADSL, RADSL,
71
SDSL, VDSL, ISO88025_CRFPRINT, MYRINET, VOICE_EM, VOICE_FXO,
72
VOICE_FXS, VOICE_ENCAP, VOICE_OVERIP, ATM_DXI, ATM_FUNI, ATM_IMA,
73
PPPMULTILINKBUNDLE, IPOVERCDLC, IPOVERCLAW, STACKTOSTACK, VIRTUALIPADDRESS,
74
MPC, IPOVERATM, ISO88025_FIBER, TDLC, GIGABITETHERNET, HDLC, LAPF,
75
V37, X25MLP, X25HUNTGROUP, TRANSPHDLC, INTERLEAVE, FAST, IP,
76
DOCSCABLEMACLAYER, DOCSCABLEDOWNSTREAM, DOCSCABLEUPSTREAM, A12MPPSWITCH,
77
TUNNEL, COFFEE, CES, ATM_SUBINTERFACE, L2_VLAN, L3_IPVLAN, L3_IPXVLAN,
78
DIGITALPOWERLINE, MEDIAMAILOVERIP, DTM, DCN, IPFORWARD, MSDSL,
79
IEEE1394, IF_GSN, DVBRCC_MACLAYER, DVBRCC_DOWNSTREAM, DVBRCC_UPSTREAM,
80
ATM_VIRTUAL, MPLS_TUNNEL, SRP, VOICEOVERATM, VOICEOVERFRAMERELAY,
81
IDSL, COMPOSITELINK, SS7_SIGLINK, PROP_WIRELESS_P2P, FR_FORWARD,
82
RFC1483, USB, IEEE8023AD_LAG, BGP_POLICY_ACCOUNTING, FRF16_MFR_BUNDLE,
83
H323_GATEKEEPER, H323_PROXY, MPLS, MF_SIGLINK, HDSL2, SHDSL,
84
DS1_FDL, POS, DVB_ASI_IN, DVB_ASI_OUT, PLC, NFAS, TR008, GR303_RDT,
85
GR303_IDT, ISUP, PROP_DOCSWIRELESS_MACLAYER, PROP_DOCSWIRELESS_DOWNSTREAM,
86
PROP_DOCSWIRELESS_UPSTREAM, HIPERLAN2, PROP_BWA_P2MP, SONET_OVERHEAD_CHANNEL,
87
DIGITAL_WRAPPER_OVERHEAD_CHANNEL, AAL2, RADIO_MAC, ATM_RADIO,
88
IMT, MVL, REACH_DSL, FR_DLCI_ENDPT, ATM_VCI_ENDPT, OPTICAL_CHANNEL,
89
OPTICAL_TRANSPORT, IEEE80216_WMAN, ADSL2, MACSECCONTROLLEDIF,
90
MACSECUNCONTROLLEDIF, AVICI_OPTICAL_ETHER, ATMBOND, VOICE_FGDN,
91
VOICE_FGDS, VOICE_FGD, ISDN_Q921, ISDN_Q931, ISDN_QSIG, LAN,
92
WIRELESS_LAN, BRIDGE, LINECONFIG, WWAN, WWANPP, WWANPP2, VOICEOTHER,
93
OTHER_WWAN, UNKNOWN
94
}
95
}
96
```
97
98
## Network Parameters
99
100
Network configuration information through the OperatingSystem interface.
101
102
```java { .api }
103
interface NetworkParams {
104
String getHostName();
105
String getDomainName();
106
String[] getDnsServers();
107
String getIpv4DefaultGateway();
108
String getIpv6DefaultGateway();
109
}
110
```
111
112
## Internet Protocol Statistics
113
114
Network protocol statistics and active connections.
115
116
```java { .api }
117
interface InternetProtocolStats {
118
TcpStats getTCPv4Stats();
119
TcpStats getTCPv6Stats();
120
UdpStats getUDPv4Stats();
121
UdpStats getUDPv6Stats();
122
List<IPConnection> getConnections();
123
124
class TcpStats {
125
long getConnectionsEstablished();
126
long getConnectionsActive();
127
long getConnectionsPassive();
128
long getConnectionFailures();
129
long getConnectionsReset();
130
long getSegmentsSent();
131
long getSegmentsReceived();
132
long getSegmentsRetransmitted();
133
long getInErrors();
134
long getOutRsts();
135
}
136
137
class UdpStats {
138
long getDatagramsSent();
139
long getDatagramsReceived();
140
long getDatagramsNoPort();
141
long getDatagramsReceivedErrors();
142
}
143
144
class IPConnection {
145
String getType();
146
String getLocalAddress();
147
int getLocalPort();
148
String getForeignAddress();
149
int getForeignPort();
150
TcpState getState();
151
int getOwningProcessId();
152
153
enum TcpState {
154
CLOSED, LISTEN, SYN_SENT, SYN_RECV, ESTABLISHED, FIN_WAIT_1,
155
FIN_WAIT_2, CLOSE_WAIT, CLOSING, LAST_ACK, TIME_WAIT, DELETE_TCB,
156
NONE, UNKNOWN
157
}
158
}
159
}
160
```
161
162
## Usage Examples
163
164
### List Network Interfaces
165
166
```java
167
import oshi.SystemInfo;
168
import oshi.hardware.NetworkIF;
169
170
SystemInfo si = new SystemInfo();
171
List<NetworkIF> networkIFs = si.getHardware().getNetworkIFs();
172
173
System.out.println("=== Network Interfaces ===");
174
for (NetworkIF netIF : networkIFs) {
175
System.out.println("Interface: " + netIF.getDisplayName());
176
System.out.println(" Name: " + netIF.getName());
177
System.out.println(" MAC Address: " + netIF.getMacaddr());
178
System.out.println(" MTU: " + netIF.getMTU());
179
System.out.println(" Speed: " + formatSpeed(netIF.getSpeed()));
180
System.out.println(" Type: " + netIF.getIfType());
181
182
// IPv4 addresses
183
String[] ipv4Addresses = netIF.getIPv4addr();
184
if (ipv4Addresses.length > 0) {
185
System.out.println(" IPv4 Addresses: " + Arrays.toString(ipv4Addresses));
186
}
187
188
// IPv6 addresses
189
String[] ipv6Addresses = netIF.getIPv6addr();
190
if (ipv6Addresses.length > 0) {
191
System.out.println(" IPv6 Addresses: " + Arrays.toString(ipv6Addresses));
192
}
193
194
System.out.println();
195
}
196
197
private static String formatSpeed(long bps) {
198
if (bps <= 0) return "Unknown";
199
if (bps < 1_000_000) return (bps / 1_000) + " Kbps";
200
if (bps < 1_000_000_000) return (bps / 1_000_000) + " Mbps";
201
return String.format("%.1f Gbps", bps / 1_000_000_000.0);
202
}
203
```
204
205
### Network Interface Statistics
206
207
```java
208
import oshi.SystemInfo;
209
import oshi.hardware.NetworkIF;
210
211
SystemInfo si = new SystemInfo();
212
List<NetworkIF> networkIFs = si.getHardware().getNetworkIFs();
213
214
System.out.println("=== Network Interface Statistics ===");
215
System.out.printf("%-20s %12s %12s %12s %12s %8s %8s%n",
216
"Interface", "Bytes Recv", "Bytes Sent", "Packets Recv", "Packets Sent", "In Errors", "Out Errors");
217
System.out.println("-".repeat(100));
218
219
for (NetworkIF netIF : networkIFs) {
220
System.out.printf("%-20s %12s %12s %12s %12s %8d %8d%n",
221
netIF.getName(),
222
formatBytes(netIF.getBytesRecv()),
223
formatBytes(netIF.getBytesSent()),
224
netIF.getPacketsRecv(),
225
netIF.getPacketsSent(),
226
netIF.getInErrors(),
227
netIF.getOutErrors()
228
);
229
}
230
231
private static String formatBytes(long bytes) {
232
if (bytes < 1024) return bytes + " B";
233
if (bytes < 1024 * 1024) return String.format("%.1f KB", bytes / 1024.0);
234
if (bytes < 1024L * 1024L * 1024L) return String.format("%.1f MB", bytes / 1024.0 / 1024.0);
235
return String.format("%.1f GB", bytes / 1024.0 / 1024.0 / 1024.0);
236
}
237
```
238
239
### Network Configuration
240
241
```java
242
import oshi.SystemInfo;
243
import oshi.software.os.NetworkParams;
244
245
SystemInfo si = new SystemInfo();
246
NetworkParams netParams = si.getOperatingSystem().getNetworkParams();
247
248
System.out.println("=== Network Configuration ===");
249
System.out.println("Hostname: " + netParams.getHostName());
250
System.out.println("Domain Name: " + netParams.getDomainName());
251
System.out.println("IPv4 Default Gateway: " + netParams.getIpv4DefaultGateway());
252
System.out.println("IPv6 Default Gateway: " + netParams.getIpv6DefaultGateway());
253
254
String[] dnsServers = netParams.getDnsServers();
255
System.out.println("DNS Servers:");
256
for (String dns : dnsServers) {
257
System.out.println(" " + dns);
258
}
259
```
260
261
### TCP/UDP Statistics
262
263
```java
264
import oshi.SystemInfo;
265
import oshi.software.os.InternetProtocolStats;
266
267
SystemInfo si = new SystemInfo();
268
InternetProtocolStats ipStats = si.getOperatingSystem().getInternetProtocolStats();
269
270
System.out.println("=== TCP Statistics ===");
271
InternetProtocolStats.TcpStats tcpv4 = ipStats.getTCPv4Stats();
272
System.out.println("TCPv4:");
273
System.out.println(" Connections Established: " + tcpv4.getConnectionsEstablished());
274
System.out.println(" Active Connections: " + tcpv4.getConnectionsActive());
275
System.out.println(" Passive Connections: " + tcpv4.getConnectionsPassive());
276
System.out.println(" Connection Failures: " + tcpv4.getConnectionFailures());
277
System.out.println(" Connections Reset: " + tcpv4.getConnectionsReset());
278
System.out.println(" Segments Sent: " + tcpv4.getSegmentsSent());
279
System.out.println(" Segments Received: " + tcpv4.getSegmentsReceived());
280
System.out.println(" Segments Retransmitted: " + tcpv4.getSegmentsRetransmitted());
281
282
InternetProtocolStats.TcpStats tcpv6 = ipStats.getTCPv6Stats();
283
System.out.println("TCPv6:");
284
System.out.println(" Connections Established: " + tcpv6.getConnectionsEstablished());
285
System.out.println(" Segments Sent: " + tcpv6.getSegmentsSent());
286
System.out.println(" Segments Received: " + tcpv6.getSegmentsReceived());
287
288
System.out.println("\n=== UDP Statistics ===");
289
InternetProtocolStats.UdpStats udpv4 = ipStats.getUDPv4Stats();
290
System.out.println("UDPv4:");
291
System.out.println(" Datagrams Sent: " + udpv4.getDatagramsSent());
292
System.out.println(" Datagrams Received: " + udpv4.getDatagramsReceived());
293
System.out.println(" Datagrams No Port: " + udpv4.getDatagramsNoPort());
294
System.out.println(" Datagram Errors: " + udpv4.getDatagramsReceivedErrors());
295
296
InternetProtocolStats.UdpStats udpv6 = ipStats.getUDPv6Stats();
297
System.out.println("UDPv6:");
298
System.out.println(" Datagrams Sent: " + udpv6.getDatagramsSent());
299
System.out.println(" Datagrams Received: " + udpv6.getDatagramsReceived());
300
```
301
302
### Active Network Connections
303
304
```java
305
import oshi.SystemInfo;
306
import oshi.software.os.InternetProtocolStats;
307
import oshi.software.os.InternetProtocolStats.IPConnection;
308
309
SystemInfo si = new SystemInfo();
310
InternetProtocolStats ipStats = si.getOperatingSystem().getInternetProtocolStats();
311
312
List<IPConnection> connections = ipStats.getConnections();
313
314
System.out.println("=== Active Network Connections ===");
315
System.out.printf("%-8s %-20s %-6s %-20s %-6s %-12s %s%n",
316
"Type", "Local Address", "Port", "Remote Address", "Port", "State", "PID");
317
System.out.println("-".repeat(90));
318
319
for (IPConnection conn : connections) {
320
System.out.printf("%-8s %-20s %-6d %-20s %-6d %-12s %d%n",
321
conn.getType(),
322
conn.getLocalAddress(),
323
conn.getLocalPort(),
324
conn.getForeignAddress(),
325
conn.getForeignPort(),
326
conn.getState(),
327
conn.getOwningProcessId()
328
);
329
}
330
```
331
332
### Network Bandwidth Monitoring
333
334
```java
335
import oshi.SystemInfo;
336
import oshi.hardware.NetworkIF;
337
338
SystemInfo si = new SystemInfo();
339
340
// Find active network interface (first non-loopback with traffic)
341
NetworkIF activeInterface = null;
342
for (NetworkIF netIF : si.getHardware().getNetworkIFs(true)) {
343
if (!netIF.getName().toLowerCase().contains("loopback") &&
344
(netIF.getBytesRecv() > 0 || netIF.getBytesSent() > 0)) {
345
activeInterface = netIF;
346
break;
347
}
348
}
349
350
if (activeInterface != null) {
351
System.out.println("Monitoring bandwidth for: " + activeInterface.getDisplayName());
352
353
// Initial measurement
354
long initialRecv = activeInterface.getBytesRecv();
355
long initialSent = activeInterface.getBytesSent();
356
long initialTime = System.currentTimeMillis();
357
358
try {
359
Thread.sleep(5000); // Wait 5 seconds
360
} catch (InterruptedException e) {
361
Thread.currentThread().interrupt();
362
}
363
364
// Update and measure again
365
if (activeInterface.updateAttributes()) {
366
long currentRecv = activeInterface.getBytesRecv();
367
long currentSent = activeInterface.getBytesSent();
368
long currentTime = System.currentTimeMillis();
369
370
long deltaRecv = currentRecv - initialRecv;
371
long deltaSent = currentSent - initialSent;
372
long deltaTime = currentTime - initialTime;
373
374
// Calculate rates in bytes per second
375
double recvRate = deltaRecv * 1000.0 / deltaTime;
376
double sentRate = deltaSent * 1000.0 / deltaTime;
377
378
System.out.printf("Download rate: %s/s%n", formatBytes((long)recvRate));
379
System.out.printf("Upload rate: %s/s%n", formatBytes((long)sentRate));
380
System.out.printf("Total received: %s%n", formatBytes(currentRecv));
381
System.out.printf("Total sent: %s%n", formatBytes(currentSent));
382
}
383
}
384
```
385
386
### Find Network Interface by Name
387
388
```java
389
import oshi.SystemInfo;
390
import oshi.hardware.NetworkIF;
391
392
SystemInfo si = new SystemInfo();
393
String targetInterface = "eth0"; // or "en0" on macOS, "Ethernet" on Windows
394
395
NetworkIF targetNetIF = null;
396
for (NetworkIF netIF : si.getHardware().getNetworkIFs(true)) {
397
if (netIF.getName().equals(targetInterface) ||
398
netIF.getDisplayName().contains(targetInterface)) {
399
targetNetIF = netIF;
400
break;
401
}
402
}
403
404
if (targetNetIF != null) {
405
System.out.println("Found interface: " + targetNetIF.getDisplayName());
406
System.out.println("Name: " + targetNetIF.getName());
407
System.out.println("MAC Address: " + targetNetIF.getMacaddr());
408
System.out.println("Speed: " + formatSpeed(targetNetIF.getSpeed()));
409
410
String[] ipv4 = targetNetIF.getIPv4addr();
411
if (ipv4.length > 0) {
412
System.out.println("IPv4: " + Arrays.toString(ipv4));
413
}
414
415
String[] ipv6 = targetNetIF.getIPv6addr();
416
if (ipv6.length > 0) {
417
System.out.println("IPv6: " + Arrays.toString(ipv6));
418
}
419
} else {
420
System.out.println("Interface not found: " + targetInterface);
421
}
422
```
423
424
### Network Interface Types
425
426
```java
427
import oshi.SystemInfo;
428
import oshi.hardware.NetworkIF;
429
430
SystemInfo si = new SystemInfo();
431
List<NetworkIF> networkIFs = si.getHardware().getNetworkIFs(true);
432
433
// Group interfaces by type
434
Map<NetworkIF.IfType, List<NetworkIF>> interfacesByType = networkIFs.stream()
435
.collect(Collectors.groupingBy(NetworkIF::getIfType));
436
437
System.out.println("=== Network Interfaces by Type ===");
438
for (Map.Entry<NetworkIF.IfType, List<NetworkIF>> entry : interfacesByType.entrySet()) {
439
NetworkIF.IfType type = entry.getKey();
440
List<NetworkIF> interfaces = entry.getValue();
441
442
System.out.printf("%s (%d interface%s):%n",
443
type, interfaces.size(), interfaces.size() == 1 ? "" : "s");
444
445
for (NetworkIF netIF : interfaces) {
446
System.out.printf(" %-20s %s%n", netIF.getName(), netIF.getDisplayName());
447
}
448
System.out.println();
449
}
450
```