public void getHostIpConfig() throws IOException {
netConfig=new NetConfig();
netConfig2=new NetConfig();
Process pro = Runtime.getRuntime().exec("ipconfig /all");
BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream(),"gbk"));
List<String> rowList = new ArrayList();
List<String> rowList1 = new ArrayList();
String temp;
int count=0;
while((temp = br.readLine()) != null){
if(temp.contains("Ethernet adapter")){
count=count+1;
}
if(count==1){
rowList.add(temp);
}else {
rowList1.add(temp);
}
}
for(int i=0;i<2;i++){
List<String> tempList = new ArrayList();
if(i==0){
tempList=rowList;
}else{
tempList=rowList1;
}
for (String string : tempList) {
Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string);
String adapter =null;
if(string.indexOf("Ethernet adapter") != -1){
adapter = string.substring(string.indexOf("adapter") + 7,string.length()-1).trim();
if(i==0){
netConfig.setNetName(adapter);
}else{
netConfig2.setNetName(adapter);
}
}else if (string.indexOf("Physical Address. . . . . . . . . :") != -1) {
if (string.indexOf(":") != -1) {
String physicalAddress = string.substring(string.indexOf(":") + 2);
if(i==0){
netConfig.setMac(physicalAddress);
}else{
netConfig2.setMac(physicalAddress);
}
}
}else if(string.indexOf("IP Address") != -1){
if(mc.find()){
if(i==0){
netConfig.setIp(mc.group());
}else{
netConfig2.setIp(mc.group());
}
}
}else if(string.indexOf("Subnet Mask") != -1){
if(mc.find()){
if(i==0){
netConfig.setMask(mc.group());
}else{
netConfig2.setMask(mc.group());
}
}
}else if(string.indexOf("Default Gateway") != -1){
if(mc.find()){
if(i==0){
netConfig.setGetway(mc.group());
}else{
netConfig2.setGetway(mc.group());
}
}
}else if(string.indexOf("DNS Servers") != -1){
if(mc.find()){
if(i==0){
netConfig.setDns(mc.group());
}else{
netConfig2.setDns(mc.group());
}
}
}else if(string.indexOf(" ") != -1){
if(mc.find()){
if(i==0){
netConfig.setDns1(mc.group());
}else{
netConfig2.setDns1(mc.group());
}
}
};
}
}
}
这里有两个网卡所以循环了两次。
java自动获得电脑网络配置并存储在beanUtil中
猜你喜欢
转载自kangkang0102.iteye.com/blog/2218093
今日推荐
周排行