Package Com.hua;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.util.Scanner; public class Connectnetwork {/** * executes the cmd command and returns a string string */public static string Executecmd (String strcmd) throws Ex
ception {Process p = runtime.getruntime (). EXEC ("cmd/c" + strcmd);
StringBuilder sbcmd = new StringBuilder ();
BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream (), "GB2312"));
String Line;
while (line = Br.readline ())!= null) {sbcmd.append (line + \ n));
return sbcmd.tostring (); /** * Connect ADSL/public static Boolean CONNADSL (String adsltitle, String adslname, String adslpass) throws Excepti
On {System.out.println ("connection is being established.");
String adslcmd = "rasdial" + Adsltitle + "" + Adslname + "" + adslpass;
String tempcmd = Executecmd (adslcmd);
Determines whether the connection succeeds if (Tempcmd.indexof (connected) > 0) {System.out.println ("The connection was successfully established."); return true;
else {System.err.println (tempcmd);
SYSTEM.ERR.PRINTLN ("Establish connection failed");
return false; }/** * Disconnect ADSL/public static Boolean CUTADSL (String adsltitle) throws Exception {string cutadsl = "rasdial
"+ Adsltitle +"/disconnect ";
String result = Executecmd (CUTADSL);
if (Result.indexof ("no Connection")!=-1) {System.err.println (adsltitle + "Connection does not exist!");
return false;
else {System.out.println ("disconnected");
return true;
}/** * Test network connectivity/public static Boolean Isconnect () {Boolean connect = false;
Runtime Runtime = Runtime.getruntime ();
Process process;
try {process = Runtime.exec ("ping" + "www.baidu.com");
InputStream is = Process.getinputstream ();
InputStreamReader ISR = new InputStreamReader (IS);
BufferedReader br = new BufferedReader (ISR);
String line = null;
StringBuffer sb = new StringBuffer (); while (LinE = Br.readline ())!= null) {sb.append (line);
} System.out.println ("Return value is:" +SB);
Is.close ();
Isr.close ();
Br.close ();
if (null!= SB &&!sb.tostring (). Equals ("")) {String logstring = "";
if (sb.tostring (). IndexOf ("TTL") > 0) {//network unblocked connect = true;
else {//network not unblocked connect = false;
A catch (IOException e) {e.printstacktrace ());
return connect; }//test code public static void Main (string[] args) throws Interruptedexception, Exception {Scanner sc = new Scanner (S
ystem.in); SYSTEM.OUT.PRINTLN ("Broadband Connection Name:");
Look at you. The name of the broadband connection String name = Sc.next ();
System.out.println ("Broadband Account:");
String username = Sc.next ();
SYSTEM.OUT.PRINTLN ("Broadband Password:"); String password = sc.next ();
String adsl= "broadband connection";
while (True) {Boolean connect = Isconnect ();
Thread.Sleep (100000);//in milliseconds, I set the 100 seconds. Look at yourself. Change if (!connect) {System.out.println ("No network, dialing on");
CONNADSL (Name,username,password); }
}
}
}
/**
* Automatic replacement of IP
* * */
public void Changeip () throws exception{
Scanner sc = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Broadband Connection Name:"); Look at you. The name of the broadband connection
String name = Sc.next ();
System.out.println ("Broadband Account:");
String username = Sc.next ();
SYSTEM.OUT.PRINTLN ("Broadband Password:");
String password = sc.next ();
SYSTEM.OUT.PRINTLN ("Replacement time (1 seconds in milliseconds equals 1000 milliseconds):");
int wait = Sc.nextint ();
while (true) {
CONNADSL (name, username, password);
Thread.Sleep (wait);
CUTADSL (name);
Thread.Sleep (wait);
Again, assign a new IP
CONNADSL (name, username, password);
}