001package com.pi4j.system; 002 003/* 004 * #%L 005 * ********************************************************************** 006 * ORGANIZATION : Pi4J 007 * PROJECT : Pi4J :: Java Library (Core) 008 * FILENAME : NetworkInfo.java 009 * 010 * This file is part of the Pi4J project. More information about 011 * this project can be found here: https://www.pi4j.com/ 012 * ********************************************************************** 013 * %% 014 * Copyright (C) 2012 - 2019 Pi4J 015 * %% 016 * This program is free software: you can redistribute it and/or modify 017 * it under the terms of the GNU Lesser General Public License as 018 * published by the Free Software Foundation, either version 3 of the 019 * License, or (at your option) any later version. 020 * 021 * This program is distributed in the hope that it will be useful, 022 * but WITHOUT ANY WARRANTY; without even the implied warranty of 023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 024 * GNU General Lesser Public License for more details. 025 * 026 * You should have received a copy of the GNU General Lesser Public 027 * License along with this program. If not, see 028 * <http://www.gnu.org/licenses/lgpl-3.0.html>. 029 * #L% 030 */ 031 032 033import com.pi4j.util.ExecUtil; 034 035import java.io.IOException; 036import java.util.ArrayList; 037import java.util.List; 038 039public class NetworkInfo { 040 041 // private constructor 042 private NetworkInfo() { 043 // forbid object construction 044 } 045 046 public static String getHostname() throws IOException, InterruptedException { 047 return ExecUtil.execute("hostname --short")[0]; 048 } 049 050 public static String getFQDN() throws IOException, InterruptedException { 051 return ExecUtil.execute("hostname --fqdn")[0]; 052 } 053 054 public static String[] getIPAddresses() throws IOException, InterruptedException { 055 return ExecUtil.execute("hostname --all-ip-addresses", " "); 056 } 057 058 public static String getIPAddress() throws IOException, InterruptedException { 059 return ExecUtil.execute("hostname -i")[0]; 060 } 061 062 public static String[] getFQDNs() throws IOException, InterruptedException { 063 return ExecUtil.execute("hostname --all-fqdns", " "); 064 } 065 066 public static String[] getNameservers() throws IOException, InterruptedException { 067 String[] nameservers = ExecUtil.execute("cat /etc/resolv.conf"); 068 List<String> result = new ArrayList<>(); 069 for (String nameserver : nameservers) { 070 if (nameserver.startsWith("nameserver")) { 071 result.add(nameserver.substring(11).trim()); 072 } 073 } 074 return result.toArray(new String[result.size()]); 075 } 076 077// public static Map<String,NetworkInterface> getNetworkInterfaces() throws IOException, InterruptedException { 078// 079// Map<String,NetworkInterface> interfaces = new HashMap<String, NetworkInterface>(); 080// 081// List<String> result = new ArrayList<String>(); 082// Process p = Runtime.getRuntime().exec("ifconfig"); 083// p.waitFor(); 084// BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 085// String line = reader.readLine(); 086// String key = null; 087// while (line != null) { 088// 089// if (!line.isEmpty()) { 090// 091// // if the line does start with string data and not spaces, then 092// // it is a new interface record 093// if (!line.startsWith(" ")) { 094// 095// String[] parts = line.split(" ", 2); 096// key = parts[0].trim(); 097// //interfaces.put(key, value); 098// } 099// 100// if (key != null && !key.isEmpty()) { 101// 102// String[] properties = line.split(" "); 103// for (String property : properties) { 104// String[] propparts = property.split(":",2); 105// 106// } 107// } 108// 109//// if (split != null || split.isEmpty()) { 110//// result.add(line.trim()); 111//// System.out.println(line.trim()); 112//// } else { 113//// String[] parts = line.trim().split(split); 114//// for (String part : parts) { 115//// if (part != null && !part.isEmpty()) { 116//// result.add(part.trim()); 117//// System.out.println(part.trim()); 118//// } 119//// } 120//// } 121// } 122// line = reader.readLine(); 123// } 124// 125// 126// 127// //throw new RuntimeException("Invalid command: " + command); 128// 129// return interfaces; 130// } 131 132}