自动设置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初学者。

注:该程序可很容易移植到其他操作系统,只要替换其中的本机程序就行。

 


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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值