资源描述:
《获取mac地址_电脑基础知识_IT计算机_专业资料》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、PublicclassMacUtils{〃这个方法运行时间太长publicstaticStringcallCmd(String[]cmd){Stringresult="Stringline=Htry{Processproc=Runtime.getRuntime().exec(cmd);工nputStreamReaderis=newTnputSt:reamReader(proc.getlnpu七51:28山());BufferedReaderbr=newBufferedReader(is);while((line=br.readLine())!=null){re
2、suIt+=line;}}catch(Exceptione){e.printStackTrace();}returnresult;}/***@paramcmd第一个命令*@paramanother第二个命令*@return第二个命令的执行结果*/publicstaticStringcallCmd(String[]cmd.String[]another){Stringresult="Stringline="try{Runtimert=Runtime•getRuntime();Processproc=rt.exec(cmd);proc.waitFor();//已经
3、执行完第—个命令,准备执行第二个命令proc=exec(another);Inputsis=newInputStreamReader(proc.getlnputStream());BufferedReaderbr=newBufferedReader(is);while((line=br.readLine())!=null){resuIt+=line;}}catch(Exceptione){e.printStackTrace();}returnresuIt;}/****@paramip目标1^—般在局域网内*gparamsourcestring命令处理的结果字符
4、串*@parammacSeparatormac分隔符号*^returnmac地址,用上面的分隔符号表示publicstaticStringfilterMacAddress(finalStringip,finalStringsourcestring,finalStringmacSeparator){Stringresult="11;StringregExp=,,((([0-9>A-F?a-门{1,2}”+macSeparator+”){1,5})[0・9,A-F,a-f]{l,2})";Patternpattern=Pattern•cowpite(regExp)
5、;Matchermatcher=pattern.matcher(sourceString);while(matcher.find()){result二matcher.group(l);if(sourcestring.indexOf(ip)<=sourcestring.lastlndexCH^matcher.group⑴)){break;//如果有多个TP,只匹配本TP对应的Mac.}}returnresult;}/***@paramip目标ip*@returnMacAddress*/—publicstaticStringgetMacInWindows(fina
6、lStringip){Stringresult=HString[]cmd={,,cmdu/c"ping"+ip};//这个命令运行的时间很长,大概有10秒左右,整个程序就这的运行时间最长String[]another={"cmd",Vc",Harp-a"};StringcmdResult=caLLCmd(cmdanother);result二fiLterMacAddress(ipcmdResult,"-");returnresult;}***@paramip目标ip*@returnMacAddress**/publicstaticStringgetMacI
7、nLinux(finalStringip){Stringresult="11;String[]cmd={"/bin/sh",ypingH+ip+H・c2StringcmdResult=caLLCmd(cmd);resuIt=fiLterMacAddress(ip.,cmdResult^":");returnresuIt;***获取MAC地址*^return返回MAC地址*/publicstaticStringgetMacAddress(Stringip){StringmacAddress=nmacAddress=getMacInlA/indo^s(ip)rim
8、();if(macAddress==n