DOS命令 命令 意义 net view 获取局域网中的所有主机名 ipconfig -all 获取本地IP,主机名,MAC地址 arp -a 获取本局域网中的所有IP地址和物理地址 ping -a x.x.
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 getIPs()
{
List list = new ArrayList();
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 getHostnames(List ips){
Map map = new HashMap();
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;
}
结果
{ =BY-=all-routers.mcast.net, =xxx-PC, =igmp.mcast.net, =xxx-PC, =xxx-PC}