调用cmd命令行命令(借鉴)
留待以后观看
————————————————————————————————————————————————————————————————————————————
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命令行命令(借鉴)的更多相关文章
- system调用命令行命令而不显示命令行窗口
system调用命令行命令而不显示命令行窗口 通常用system调用命令行命令时都会弹出黑底白字的命令行窗口,下面的代码可以不显示弹出的命令行窗口. 代码如下 #pragma comment( lin ...
- C/C++ 程序中调用命令行命令并获取命令行输出结果
在 c/c++ 程序中,可以使用 system()函数运行命令行命令,但是只能得到该命令行的 int 型返回值,并不能获得显示结果.例如system(“ls”)只能得到0或非0,如果要获得ls的执行结 ...
- Windows命令行命令集锦
原文:Windows命令行命令集锦 转自:http://www.me2wg.com/bbs/forum.php?mod=viewthread&tid=15830 winver--------- ...
- MySql命令行命令和SQL语句
一.常用mysql命令行命令 1.启动MYSQL服务 net start mysql 停止MYSQL服务 net stop mysql 2.netstat -na|findstr 3306 查看被监听 ...
- iOS工程师常用的命令行命令总结
感觉有点标题党了. 作为一个iOS工程师,没有做过服务端,主要用的是mac电脑,此篇博文是记录我在工作,学习的过程中用的命令行命令的记录和归纳总结 一. mac命令行 1. cd /Users/xxx ...
- Windows与Linux的命令行命令对比
Windows与Linux的命令行命令对比 * Windows不区分大小写,Linux区分大小写的. sn DOS Command UNIX Equivalent Effect 影响 1 ASSIGN ...
- 一些坑 Java 执行命令行命令 Spring Boot 打包为jar ResourceUtils.getFile 等出现的问题
Java 执行命令行命令 这个没技术含量的东西耗费了我半个多小时 String command = ....; Process process = Runtime.getRuntime().exec( ...
- [转帖]Windows与Linux的命令行命令对比
Windows与Linux的命令行命令对比 https://www.cnblogs.com/sztom/p/10785140.html * Windows不区分大小写,Linux区分大小写的. sn ...
- 我自己总结的sqlite的命令行命令集
我自己总结的sqlite 的命令行命令 导入文本数据文件时,设置分隔符为","sql>.separator "," sql>import devic ...
随机推荐
- swiper实现左右滑动图片
ref:http://www.swiper.com.cn/usage/index.html help:https://segmentfault.com/a/1190000002962202 src: ...
- 1.sql注入原理
一.什么是sql注入呢? 所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网站泄露V ...
- 【转】webservice
一.WebService概念 Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它 ...
- 使用Log4j2,打包后提示ERROR StatusLogger Log4j2 could not find a logging implementation.
从Log4j切换到Log4j2,没有打包之前日志输出正常,但是打包后总是提示下面内容: 错误一: ERROR StatusLogger Log4j2 could not find a logging ...
- HBase高可用原理与实践
前言 前段时间有套线上HBase出了点小问题,导致该套HBase集群服务停止了2个小时,从而造成使用该套HBase作为数据存储的应用也出现了服务异常.在排查问题之余,我们不禁也在思考,以后再出现类似的 ...
- 前后分离调用API跨域
前后分离调用API接口跨域问题 什么是跨域? 跨域是指一个域下的文档或脚本试图去请求另一个域下的资源,这里跨域是广义的. 广义的跨域: 资源跳转:A链接.重定向.表单提交. 资源嵌入: <li ...
- 剑指Offer的学习笔记(C#篇)-- 旋转数组的最小数字
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...
- iOS获取手机型号、iOS获取当前app的名称和版本号
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...
- 使用JMeter进行API功能测试
使用JMeter进行API功能测试 Apache JMeter是一种流行的开源软件,用于性能测试. 在本博客中,我们将阐明如何使用JMeter for REST API自动化进行功能测试. 我们使用了 ...
- HTTP的学习记录(二)头部
本文主要讲一些 HTTP头部的信息 首先看一段 惊为天人 的文章. 来自于 <淘宝技术这十年> 你发现快要过年了,于是想给你的女朋友买一件毛衣,你打开了www.taobao.com.这时你 ...