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);
}
}
这样的话出现太多的DOS窗口了差点死机 有没有什么好的方法呢
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);
}
}
这样的话出现太多的DOS窗口了差点死机 有没有什么好的方法呢
1.能不能在cmd /c start rasdial \"宽带连接\" zhanghao mima后面紧接着执行exit关闭dos
类似cmd /c start rasdial \"宽带连接\" zhanghao mima;exit
2.sleep一段时间,不然机器受不鸟
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();
}
}
}大概逻辑是这样,楼主要比较第三段IP,那就把while判断那改一改就行。
另外每连一次要sleep一会吧,如果连的太频可能会被断网哦。