WindowsADSL切换外网Ip
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
@Slf4j
public class ResetADSLNetwork {
/**
* 执行CMD命令,并返回String字符串
*/
public static String executeCmd(String strCmd) throws Exception {
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).append("\n");
}
return sbCmd.toString();
}
/**
* 连接ADSL
*/
public static boolean connAdsl(String adslTitle, String adslName, String adslPass) throws Exception {
log.debug("正在建立连接");
String adslCmd = "rasdial " + adslTitle + " " + adslName + " " + adslPass;
String tempCmd = executeCmd(adslCmd);
// 判断是否连接成功
if (tempCmd.indexOf("已连接") > 0) {
log.debug("建立连接成功");
return true;
} else {
log.debug("建立连接失败 cmd:{}", tempCmd);
return false;
}
}
/**
* 断开ADSL
*/
public static boolean cutAdsl(String adslTitle) throws Exception {
String cutAdsl = "rasdial " + adslTitle + " /disconnect";
String result = executeCmd(cutAdsl);
if (result.contains("没有连接")) {
log.debug("{}连接不存在!", adslTitle);
return false;
} else {
log.debug("{}连接已断开!", adslTitle);
return true;
}
}
/**
* 测试网络是否连接
*/
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);
}
log.debug("返回值:{}", sb);
is.close();
isr.close();
br.close();
if (!sb.toString().equals("")) {
connect = sb.toString().indexOf("TTL") > 0;
}
} catch (IOException e) {
log.error("is Connect:", e);
return false;
}
return connect;
}
private static Set<String> changeList = new HashSet<>();
public static void setChangeList(Set<String> changeList) {
ResetADSLNetwork.changeList = changeList;
}
public static String changeIp() throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("宽带连接名称:"); // 看你宽带连接的名称
String name = "adsl";
System.out.println("宽带账户:");
String username = "";
System.out.println("宽带密码:");
String password = "";
System.out.println("更换时间(单位毫秒1秒等于1000毫秒):");
String result = "";
while (result.equals("") || changeList.contains(result)) {
cutAdsl(name);
//再连,分配一个新的IP
Thread.sleep(1000);
connAdsl(name, username, password);
Thread.sleep(1000);
result = CheckNetworkUtils.checkIpV4();
}
changeList.add(result);
return result;
}
@SneakyThrows
public static void main(String[] args) {
System.out.println(changeIp());
System.out.println(changeIp());
System.out.println(changeIp());
}
}
JavaExec工具类
import com.boommanpro.aobidao.common.dto.ExecCmdResult;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
/**
* SystemUtil
* 系统工具类
*
* @author hengyumo
* @version 1.0
* @since 2020/2/4
*/
@Slf4j
public class ExecCmdUtils {
private final static int BUFFER_SIZE = 1024;
private final static String DEFAULT_ENCODING = "gbk";
private static class ProcessWorker extends Thread {
private final Process process;
private volatile int exitCode = -99;
private volatile boolean completed = false;
private volatile String output = "";
private ProcessWorker(Process process) {
this.process = process;
}
@Override
public void run() {
try (InputStreamReader reader = new InputStreamReader(
process.getInputStream(), DEFAULT_ENCODING)) {
StringBuilder log = new StringBuilder();
char[] buffer = new char[BUFFER_SIZE];
int length;
while ((length = reader.read(buffer)) != -1) {
log.append(buffer, 0, length);
}
output = log.toString();
exitCode = process.waitFor();
completed = true;
} catch (InterruptedException | IOException e) {
Thread.currentThread().interrupt();
}
}
public int getExitCode() {
return exitCode;
}
public String getOutput() {
return output;
}
public boolean isCompleted() {
return completed;
}
}
public static ExecCmdResult execCmd(String command) {
return execCmd(command, 15);
}
public static ExecCmdResult execCmd(String command, int timeoutSecond) {
long start = System.currentTimeMillis();
StringBuilder content = new StringBuilder();
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
// 合并错误输出流
processBuilder.redirectErrorStream(true);
Process process;
try {
process = processBuilder.start();
} catch (IOException e) {
log.error("execCmd error, execCmd:{}.e:", command, e);
return new ExecCmdResult(false, -1, command, e.getMessage(), System.currentTimeMillis() - start);
}
ProcessWorker processWorker = new ProcessWorker(process);
int exitCode = processWorker.getExitCode();
processWorker.start();
try {
processWorker.join(TimeUnit.SECONDS.toMillis(timeoutSecond));
if (processWorker.isCompleted()) {
content.append(processWorker.getOutput());
exitCode = processWorker.getExitCode();
} else {
process.destroy();
processWorker.interrupt();
return new ExecCmdResult(false, processWorker.exitCode, "thread exec timeout", processWorker.getOutput(), System.currentTimeMillis() - start);
}
} catch (InterruptedException e) {
processWorker.interrupt();
}
return new ExecCmdResult(exitCode == 0, exitCode, command, content.toString(), System.currentTimeMillis() - start);
}
}
use demo
ExecCmdUtils.execCmd("taskkill /f /t /im chrome.exe");
ExecCmdUtils.execCmd("taskkill /f /t /im chromedriver.exe");