java 获取局域网中的所有主机名和IP地址
来源:互联网 发布:苹果手机qq数据备份 编辑:程序博客网 时间:2024/04/28 02:55
DOS命令
java exec
执行外部命令
String command = "net view"Runtime r = Runtime.getRuntime();Process p = r.exec(command);BufferedReader br = new BufferedReader(new InputStreamReader(p .getInputStream()));
提取局域网IP
public static List<String> getIPs() { List<String> list = new ArrayList<String>(); boolean flag = false; int count=0; Runtime r = Runtime.getRuntime(); Process p; try { p = r.exec("arp -a"); BufferedReader br = new BufferedReader(new InputStreamReader(p .getInputStream())); String inline; while ((inline = br.readLine()) != null) { if(inline.indexOf("接口") > -1){ flag = !flag; if(!flag){ //碰到下一个"接口"退出循环 break; } } if(flag){ count++; if(count > 2){ //有效IP String[] str=inline.split(" {4}"); list.add(str[0]); } } System.out.println(inline); } br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(list); return list; }
根据IP提取主机名
public static Map<String,String> getHostnames(List<String> ips){ Map<String,String> map = new HashMap<String,String>(); System.out.println("正在提取hostname..."); for(String ip : ips){ String command = "ping -a " + ip; Runtime r = Runtime.getRuntime(); Process p; try { p = r.exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(p .getInputStream())); String inline; while ((inline = br.readLine()) != null) { if(inline.indexOf("[") > -1){ int start = inline.indexOf("Ping "); int end = inline.indexOf("["); String hostname = inline.substring(start+"Ping ".length(),end-1); System.out.println(hostname); map.put(ip,hostname); } } br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("提取结束!"); return map; }
结果
{ 222.26.28.51=BY-201507012043, 224.0.0.2=all-routers.mcast.net, 222.26.28.20=xxx-PC, 224.0.0.22=igmp.mcast.net, 222.26.28.218=xxx-PC, 222.26.28.223=xxx-PC}
0 0
- java 获取局域网中的所有主机名和IP地址
- php 获取局域网的 所有用户的电脑IP和主机名、及mac地址
- C++ 获取局域网内所有可用IP和主机名
- C++ 获取局域网内所有可用IP和主机名
- java获取访问者主机名和IP地址
- 获得局域网内IP地址和主机名
- 获取主机名和IP地址
- 获取主机名和IP地址
- 获取主机名和IP地址
- C#获取局域网中的所有正在使用的IP地址
- 获取局域网的ip和主机名
- 获取局域网的主机名和ip
- 知道局域网ip地址,获取该ip的主机名
- 获取局域网所有IP地址和主机名称
- Linux中根据主机名获取其在局域网中的IP地址
- Java使用InetAddress类获取主机名和IP地址
- java中得到主机名与IP,扫描局域网中的主机名
- linux获取局域网、公网IP地址以及主机名
- zoj1383
- UI003---transform属性
- hdu 5288 - OO’s Sequence 数学
- 帮助Jimmy
- 【opencv】VS2010+CMake重编译opencv2.4.10
- java 获取局域网中的所有主机名和IP地址
- UVA 10785 The Mad Numerologist
- iOS-NSDate之今天,昨天,这周,这个月,上个月
- UVA 10115
- SPOOLing技术
- 百练6044--鸣人与佐助(BFS)
- Android support v4中的Fragment和app包中的Fragment
- 6. php 基本语法
- POJ 1001 Exponentiation 高精度指数