留待以后观看

————————————————————————————————————————————————————————————————————————————

public class IP_MAC_TypeHelper {

	/**
* 控制台执行arp -a命令
*
* @return
*
*/
public static InputStream getInputStream() {
Runtime rt = Runtime.getRuntime();
InputStream in = null;
try {
Process p = rt.exec("cmd.exe /c arp -a");
in = p.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return in; } /**
* 获取的字节流转成String
*
* @param in
* @return
*/
public static String read(InputStream in) {
InputStreamReader isr;
try {
isr = new InputStreamReader(in, "GBK");
BufferedReader br = new BufferedReader(isr);
String inline = null;
StringBuffer sb = new StringBuffer();
while ((inline = br.readLine()) != null) {
// System.out.println(inline);
sb.append(inline);
}
return sb.toString();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null; } /**
* 格式化输出信息
*
* @param msg
* @return
*/
public static String[] getMsg(String msg) {
//按换行截取
String[] tempmessage = msg.split("\r\n");
StringBuffer sb = new StringBuffer();
for (String s : tempmessage) {
sb.append(s + " ");
}
String temp = sb.toString();
return temp.split(" {1,}");
} /**
* 截取IP地址信息
*
* @param msg
* @return
*/
public static List<String> getIp(String[] msg) {
List<String> list = new ArrayList<String>();
for (String s : msg) {
boolean flag = s.matches("^[0-9]{1,3}(\\.[0-9]{1,3}){3}$");// 匹配IP的正则
if (flag) {
list.add(s);
}
}
return list;
} /**
* 截取MAC地址信息
*
* @param msg
* @return
*/
public static List<String> getMac(String[] msg) {
List<String> list = new ArrayList<String>();
String regx = "^[a-zA-Z0-9]{2}(-[a-zA-Z0-9]{2}){5}$"; // 匹配MAC地址的正则
for (String s : msg) {
if (s.matches(regx)) {
list.add(s);
}
}
return list;
} /**
* 截取类型信息
*
* @param msg
* @return
*/
public static List<String> getType(String[] msg) { List<String> list = new ArrayList<String>();
for (String s : msg) {
if (s.contains("态")) { // 判断是否为指定字符
list.add(s);
}
}
return list; } /**
* 移除本机IP(包含网卡)
* @param ipList
* @return
*/
public static List<String> removeLocalIp(List<String> ipList) {
List<String> ripList = new ArrayList<String>();
try {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface nif = netInterfaces.nextElement();
Enumeration<InetAddress> iparray = nif.getInetAddresses();
while (iparray.hasMoreElements()) {
/*
* System.out.println("IP:" +
* iparray.nextElement().getHostAddress());
*/
ripList.add(iparray.nextElement().getHostAddress());
}
} } catch (Exception e) {
System.out.println(e.getMessage());
}
for (int i = 0; i < ipList.size() - 1; i++) {
for (String rip : ripList) {
if (ipList.get(i).equals(rip)) {
ipList.remove(i);
}
}
}
return ipList;
} /**
* 获取对应的IP、MAC、类型
* @return
*/
public static List<Admin> getIp_Mac_Type() {
List<Admin> aList = new ArrayList<Admin>();
InputStream in = getInputStream();
String message = read(in);
String[] msg = getMsg(message);
List<String> list_ip = removeLocalIp(getIp(msg));
List<String> list_mac = getMac(msg);
List<String> list_type = getType(msg);
for(int i = 0; i<list_ip.size(); i++){
Admin admin = new Admin();
admin.setIp(list_ip.get(i));
admin.setMac(list_mac.get(i));
admin.setType(list_type.get(i));
aList.add(admin);
}
for(Admin a:aList){
System.out.println(a.getIp());
}
return aList;
} }

我们知道在cmd命令行窗口中输入arp -a能得到局域网下所有IP,上述代码调用该命令得到所有IP,以上仅为借鉴。有待完善。。。

————————————————————————————————————————————————————————

版权所有,出自http://www.cnblogs.com/ytlds

调用cmd命令行命令(借鉴)的更多相关文章

  1. system调用命令行命令而不显示命令行窗口

    system调用命令行命令而不显示命令行窗口 通常用system调用命令行命令时都会弹出黑底白字的命令行窗口,下面的代码可以不显示弹出的命令行窗口. 代码如下 #pragma comment( lin ...

  2. C/C++ 程序中调用命令行命令并获取命令行输出结果

    在 c/c++ 程序中,可以使用 system()函数运行命令行命令,但是只能得到该命令行的 int 型返回值,并不能获得显示结果.例如system(“ls”)只能得到0或非0,如果要获得ls的执行结 ...

  3. Windows命令行命令集锦

    原文:Windows命令行命令集锦 转自:http://www.me2wg.com/bbs/forum.php?mod=viewthread&tid=15830 winver--------- ...

  4. MySql命令行命令和SQL语句

    一.常用mysql命令行命令 1.启动MYSQL服务 net start mysql 停止MYSQL服务 net stop mysql 2.netstat -na|findstr 3306 查看被监听 ...

  5. iOS工程师常用的命令行命令总结

    感觉有点标题党了. 作为一个iOS工程师,没有做过服务端,主要用的是mac电脑,此篇博文是记录我在工作,学习的过程中用的命令行命令的记录和归纳总结 一. mac命令行 1. cd /Users/xxx ...

  6. Windows与Linux的命令行命令对比

    Windows与Linux的命令行命令对比 * Windows不区分大小写,Linux区分大小写的. sn DOS Command UNIX Equivalent Effect 影响 1 ASSIGN ...

  7. 一些坑 Java 执行命令行命令 Spring Boot 打包为jar ResourceUtils.getFile 等出现的问题

    Java 执行命令行命令 这个没技术含量的东西耗费了我半个多小时 String command = ....; Process process = Runtime.getRuntime().exec( ...

  8. [转帖]Windows与Linux的命令行命令对比

    Windows与Linux的命令行命令对比 https://www.cnblogs.com/sztom/p/10785140.html * Windows不区分大小写,Linux区分大小写的. sn ...

  9. 我自己总结的sqlite的命令行命令集

    我自己总结的sqlite 的命令行命令 导入文本数据文件时,设置分隔符为","sql>.separator "," sql>import devic ...

随机推荐

  1. Struts2学习第三课 Action

    action  VS  Action类 action:代表一个Struts2的请求 Action类:能够处理struts2请求的类. 属性的名字必须遵守与JavaBean属性名相同的命名规则. 属性的 ...

  2. 教育网bt站点

    北京交通大学 晨光BT (http://cgbt.cn)清华晨光BT(http://thubt.cn)北京科技大学 iBeiKeBT(http://bt.ibeike.com)上海大学 乐乎BT (h ...

  3. 6.【应急响应】Linux入侵排查思路

    0x01 入侵排查思路 一.账号安全 基本使用: 1.用户信息文件/etc/passwd root:x:0:0:root:/root:/bin/bash account:password:UID:GI ...

  4. 会话临时表 ORA-14452

    需要使用Oracle的临时表,向其中插入记录,用完后再删除.但是后来发现临时表的删除总是失败,返回错误: ORA-14452: attempt to create, alter or drop an ...

  5. 【Strtus2】

    基于mvc设计模式的web应用框架!strtus2作为控制器来建立模型与视图数据的交互.

  6. Codeforces - 102222H - Fight Against Monsters - 贪心

    https://codeforc.es/gym/102222/problem/H 题意:有一堆怪兽,怪兽有HP和ATK.你有一个英雄,英雄每次先被所有怪兽打,然后打其中一个怪兽.打的伤害递增,第一次1 ...

  7. SAS笔记(7) PROC SQL

    参考资料:<Longitudinal Data and SAS: A Programmer's Guide>

  8. webpack@3.6.0(1) -- 快速开始

    本篇内容 前言 配置入口和输出 热更新 loader配置 js代码压缩 html的打包与发布 前言 //全局安装 npm install -g webpack(3.6.0) npm init //安装 ...

  9. 分层图最短路 【bzoj1579】[Usaco2009 Feb]Revamping Trails 道路升级

    1579: [Usaco2009 Feb]Revamping Trails 道路升级 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M< ...

  10. 因为.patch_storage目录丢失,导致opatch打补丁失败

    一套新装的ORACLE Restart环境(11.2.0.3.0),计划最新的PSU,在使用opath auto方式安装补丁时报错,表面上的错误信息提示opatch工具不满足版本要求: [root@d ...