下面是获取Linux下子网掩码,Windows的获取方式有变化,需要通过cmd.exe ipconfig运行后截取内容,这里暂时只有Linux的获取方式。
String getLocalMask(){
String cmd="ifconfig";
//String cmd="curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \\([0-9\\.]*\\).*/\\1/g'";
String Mask="";//子网掩码
try{
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader theReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String infor ="";
while((infor=theReader.readLine())!=null){
//out.print(infor);
if (infor.indexOf("Mask:")!=-1){
Mask = infor.substring(infor.indexOf("Mask:")+5);
break;
}
}
theReader.close();
proc.destroy();
}catch(Exception e){
e.printStackTrace();
}
return Mask;
}