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: http://www.pi4j.com/ 012 * ********************************************************************** 013 * %% 014 * Copyright (C) 2012 - 2013 Pi4J 015 * %% 016 * Licensed under the Apache License, Version 2.0 (the "License"); 017 * you may not use this file except in compliance with the License. 018 * You may obtain a copy of the License at 019 * 020 * http://www.apache.org/licenses/LICENSE-2.0 021 * 022 * Unless required by applicable law or agreed to in writing, software 023 * distributed under the License is distributed on an "AS IS" BASIS, 024 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 025 * See the License for the specific language governing permissions and 026 * limitations under the License. 027 * #L% 028 */ 029 030 031import java.io.IOException; 032import java.util.ArrayList; 033import java.util.List; 034 035import com.pi4j.util.ExecUtil; 036 037public class NetworkInfo { 038 039 // private constructor 040 private NetworkInfo() { 041 // forbid object construction 042 } 043 044 public static String getHostname() throws IOException, InterruptedException { 045 return ExecUtil.execute("hostname --short")[0]; 046 } 047 048 public static String getFQDN() throws IOException, InterruptedException { 049 return ExecUtil.execute("hostname --fqdn")[0]; 050 } 051 052 public static String[] getIPAddresses() throws IOException, InterruptedException { 053 return ExecUtil.execute("hostname --all-ip-addresses", " "); 054 } 055 056 public static String getIPAddress() throws IOException, InterruptedException { 057 return ExecUtil.execute("hostname --ip-address")[0]; 058 } 059 060 public static String[] getFQDNs() throws IOException, InterruptedException { 061 return ExecUtil.execute("hostname --all-fqdns", " "); 062 } 063 064 public static String[] getNameservers() throws IOException, InterruptedException { 065 String[] nameservers = ExecUtil.execute("cat /etc/resolv.conf"); 066 List<String> result = new ArrayList<String>(); 067 for (String nameserver : nameservers) { 068 if (nameserver.startsWith("nameserver")) { 069 result.add(nameserver.substring(11).trim()); 070 } 071 } 072 return result.toArray(new String[0]); 073 } 074 075// public static Map<String,NetworkInterface> getNetworkInterfaces() throws IOException, InterruptedException { 076// 077// Map<String,NetworkInterface> interfaces = new HashMap<String, NetworkInterface>(); 078// 079// List<String> result = new ArrayList<String>(); 080// Process p = Runtime.getRuntime().exec("ifconfig"); 081// p.waitFor(); 082// BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 083// String line = reader.readLine(); 084// String key = null; 085// while (line != null) { 086// 087// if (!line.isEmpty()) { 088// 089// // if the line does start with string data and not spaces, then 090// // it is a new interface record 091// if (!line.startsWith(" ")) { 092// 093// String[] parts = line.split(" ", 2); 094// key = parts[0].trim(); 095// //interfaces.put(key, value); 096// } 097// 098// if (key != null && !key.isEmpty()) { 099// 100// String[] properties = line.split(" "); 101// for (String property : properties) { 102// String[] propparts = property.split(":",2); 103// 104// } 105// } 106// 107//// if (split != null || split.isEmpty()) { 108//// result.add(line.trim()); 109//// System.out.println(line.trim()); 110//// } else { 111//// String[] parts = line.trim().split(split); 112//// for (String part : parts) { 113//// if (part != null && !part.isEmpty()) { 114//// result.add(part.trim()); 115//// System.out.println(part.trim()); 116//// } 117//// } 118//// } 119// } 120// line = reader.readLine(); 121// } 122// 123// 124// 125// //throw new RuntimeException("Invalid command: " + command); 126// 127// return interfaces; 128// } 129 130}