软讯网络 > 编程语言 > Java > 自动设置WindowsIP的程序SetupIP
【标 题】:自动设置WindowsIP的程序SetupIP
【关键字】:
WindowsIP,SetupIP
【来 源】:http://blog.csdn.net/JavaWHB/archive/2007/02/16/1511037.aspx
自动设置WindowsIP的程序SetupIP
开发原因: 上大学时使用教育网,抢IP是个令人头痛的问题,昨天还能用的IP今天早上起来就被别人占了,十分郁闷,于是做了一个自动试设IP的程序。原理很简单,使用了Windows的两个程序:netsh和ipconfig,使用netsh设置IP,如果成功了,用ipconfig就会得到有效的IP,否则就是0.0.0.0的无效IP,根据这个判断是否成功设置。
使用方法:需更改“String todo = "netsh interface ip set address 本地连接 static 222.28.34.%1 255.255.255.0 222.28.34.1 0";”和代码中的IP地址换成你机器所用的网段。
适用对象:Java初学者。
注:该程序可很容易移植到其他操作系统,只要替换其中的本机程序就行。
//忘忧草工作室
//2007-2-16
import java.io.*;
public class SetupIP {
public static void main(String[] args) {
String todo = "netsh interface ip set address 本地连接 static 222.28.34.%1 255.255.255.0 222.28.34.1 0";
String cmd = null;
try {
for (int i = 254; i > 1; i --) {
cmd = todo.replaceAll("%1",String.valueOf(i));
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
Process pp = Runtime.getRuntime().exec("ipconfig");
pp.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(pp.getInputStream()));
String s = null;
StringBuffer ss = new StringBuffer();
while ((s = stdInput.readLine()) != null) {
ss.append(s);
}
if (ss.indexOf("222.28.34") > 0) {
System.out.println("当前IP设置为:222.28.34." + String.valueOf(i));
System.exit(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
【相关文章】
没有相关文章