DOS命令

命令 意义
net view 获取局域网中的全部主机名
ipconfig -all 获取本地IP,主机名,MAC地址
arp -a 获取本局域网中的全部IP地址和物理地址
ping -a x.x.x.x 获取x.x.x.x的主机名
nbtstat -a 主机名 获取MAC地址

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}

java 获取局域网中的全部主机名和IP地址的更多相关文章

  1. C#获取局域网中的所有正在使用的IP地址

    方法不是很好. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  2. 查看局域网中连接的主机名和对应的IP地址

    1.查看局域网中的所有主机名 2.通过主机名解析IP地址:-4选项 3.通过IP地址解析主机名:-a选项

  3. 二十二、utl_inaddr(用于取得局域网或Internet环境中的主机名和IP地址)

    1.概述 作用:用于取得局域网或Internet环境中的主机名和IP地址. 2.包的组成 1).get_host_name作用:用于取得指定IP地址所对应的主机名语法:utl_inaddr.get_h ...

  4. Linux学习笔记(10)linux网络管理与配置之一——主机名与IP地址,DNS解析与本地hosts解析(1-4)

    Linux学习笔记(10)linux网络管理与配置之一——主机名与IP地址,DNS解析与本地hosts解析 大纲目录 0.常用linux基础网络命令 1.配置主机名 2.配置网卡信息与IP地址 3.配 ...

  5. Solaris 10主机名和IP地址步骤

    1.修改主机名: hostname newname vi /etc/hosts vi /etc/hostname.e1000g0 vi /etc/nodename init 6 #重启 --confi ...

  6. Asp.net MVC获取访问系统的客户端计算机的主机名和IP地址

    string HostName = string.Empty; string ip = string.Empty; string ipv4 = String.Empty; if (!string.Is ...

  7. 查看局域网内所有的主机名、MAC地址和IP地址

    查看所有 IP at MAC $ arp -a ? (10.125.49.187) at 18:81:e:eb:ef:c0 on en0 ifscope [ethernet] ? (10.125.50 ...

  8. Linux CentOS7.0 (02)修改主机名和ip地址

    一.主机名修改 1.查看命令 在CentOS中,有三种定义的主机名:静态的(static),瞬态的(transient),和灵活的(pretty). "静态"主机名也称为内核主机名 ...

  9. 修改Linux的基本配置(修改主机名修改ip地址安装JDK/Tomcat/MySQL等等)

    (一)基本操作修改 修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=server1.itcast.cn 修改ip地址 vi /etc/s ...

随机推荐

  1. JavaScript 作用域和闭包——另一个角度:扩展你对作用域和闭包的认识【翻译+整理】

    原文地址 --这篇文章有点意思,可以扩展你对作用域和闭包的认识. 本文内容 背景 作用域 闭包 臭名昭著的循环问题 自调用函数(匿名函数) 其他 我认为,尝试向别人解释 JavaScript 作用域和 ...

  2. EDA: Event-Driven Architecture事件驱动架构

    EDA: Event-Driven Architecture事件驱动架构 2009-09-24 17:28 5 赞  异步编程      软件架构      EDA事件驱动        SOA的核心 ...

  3. php学习实例3

    新闻发布管理系统 路由器action.php <!DOCTYPE html> <html> <head> <title> </title> ...

  4. 使用jstl报http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar错误

    今天创建了一个maven项目,想使用jstl报http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the ...

  5. Jquery——hover与toggle

    hover方法的语法结构为:hover(enter,leave) hover()当鼠标移动到元素上时,会触发第一个方法,当鼠标移开的时候会触发第二个方法 复制代码 <html> <h ...

  6. openfiler在esxi下的安装配置

    注意分区的时候如果硬盘太小自动分区会导致分配的卷大小不够用 后改为如下: 以root登录: 应该以openfiler登录,口令是password 也可以导入虚拟机安装 升级虚拟机硬件版本 终端登录用户 ...

  7. nginx获得response自定义的header

    Response header send by upstream is $upstream_http_x_uuid http://wiki.nginx.org/HttpUpstreamModule#. ...

  8. linux高精度struct timespec 和 struct timeval

    一.struct timespec 定义: typedef long time_t;#ifndef _TIMESPEC#define _TIMESPECstruct timespec {time_t ...

  9. 〖Linux〗Linux高级编程 - 进程间通信(Interprocess Communication)

    [转自: http://blog.csdn.net/Paradise_for_why/article/details/5550619] 这一章就是著名的IPC,这个东西实际的作用和它的名字一样普及.例 ...

  10. window搭建python环境

    在window开发python代码,搭建python环境! 01.下载python-win https://www.python.org/downloads/windows/ http://ipyth ...