日期:2014-05-20 浏览次数:20775 次
package test; import java.io.IOException; import java.net.InetAddress; public class IpTest { public static void main(String[] args){ boolean key = false; do{ try { Runtime.getRuntime().exec("cmd /c start rasdial \"宽带连接\" /disconnect"); Runtime.getRuntime().exec("cmd /c start rasdial \"宽带连接\" zhanghao mima"); } catch (IOException ee) { ee.printStackTrace(); } InetAddress[] mArLocalIP = null; if(mArLocalIP == null) { try { mArLocalIP = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName()); String adress = mArLocalIP[0].toString(); System.out.println(adress); String address2 = adress.substring(16, 19); System.out.println(address2); if (address2.equals("152")){ key = true; } } catch(Exception e) { System.out.println( "getHostIP error "); } } }while(key == false); } }
import java.io.IOException; import java.net.InetAddress; public class Test { public static void main(String[] args) throws IOException, InterruptedException { InetAddress addr = InetAddress.getLocalHost(); String ip = addr.getHostAddress().toString(); System.out.println(ip); while(!"192.168.18.1".equals(ip)){ Process disconnect = Runtime.getRuntime().exec("rasdial.exe 宽带连接 /disconnect"); disconnect.waitFor(); System.out.println("断开"); Process connect = Runtime.getRuntime().exec("rasdial.exe 宽带连接 username password"); connect.waitFor(); System.out.println("连接"); ip = addr.getHostAddress().toString(); } } }