(转)JAVA实现Windows拨号、IP切换
原理:
通过调用windows下的dos命令实现拨号
PS:连接名称获取不一定都是适用,但苦于知道的dos命令太少了,只能将就这么用着。
如有更好的方法,烦请不吝赐教。
public class ConnectNetWork {
public static void main(String[] args) throws Exception {
String adslTitle = getConnectName();
String adslName = "************";
String adslPsw = "*******";
connAdsl(adslTitle, adslName, adslPsw);
Thread.sleep(1000);
disconectAdsl(adslTitle);
Thread.sleep(1000);
connAdsl(adslTitle, adslName, adslPsw); // 再连,分配一个新的IP
}
private static String executeCmd(String str) throws Exception {
Process p = Runtime.getRuntime().exec("cmd /c " + str);
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream(), "gbk"));
String line=null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
p.destroy();
return sb.toString();
}
private static boolean connAdsl(String adslTitle, String adslName,
String adslPsw) throws Exception {
String adslCmd = "rasdial " + adslTitle + " " + adslName + " "
+ adslPsw;
String tempCmd = executeCmd(adslCmd);
// 判断连接是否成功
if (tempCmd.indexOf("已连接") > 0 || tempCmd.indexOf("已经连接") > 0) {
System.out.println("已成功建立连接.");
return true;
} else {
System.err.println(tempCmd);
return false;
}
}
private static boolean disconectAdsl(String adslTitle) throws Exception {
String cutAdsl = "rasdial " + adslTitle + " /disconnect";
String result = executeCmd(cutAdsl);
if (result.indexOf("没有连接") != -1) {
System.err.println(adslTitle + "连接不存在!");
return false;
} else {
System.out.println("连接已断开");
return true;
}
}
private static String getConnectName() throws Exception {
String reponse = executeCmd("ipconfig /all");
String temp ="描述. . . . . . . . . . . . . . . :";
int begin = reponse.indexOf(temp)+temp.length();
int last = begin+5;
return reponse.substring(begin,last);//返回名称
}
}
(转)JAVA实现Windows拨号、IP切换的更多相关文章
- java获取本机IP地址
转载自:http://blog.csdn.net/thunder09/article/details/5360251 在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一 ...
- windows JDK 版本切换
windows JDK 版本切换1. HKEY_LOCAL_MACHINE“SOFTWARE“JavaSoft“Java Runtime Environment“CurrentVersion, 把这个 ...
- Java读写Windows共享文件夹 .
版权声明:本文为博主原创文章,未经博主允许不得转载. 项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片.文件等.那么如何使用Java读写Windows共享文件夹呢? Java可以使用JCIF ...
- Web Performance Test : IP切换/IP欺骗
译者前言 本文翻译自<Visual Studio Performance Testing Quick Reference Guide 3.6> 有关于IP Switching的章节.< ...
- 使用Java修改Windows注册表
使用Java修改Windows注册表,使用最基本的就是cmd命令. 事例和运行结果如下所示: package day01; import java.io.IOException; /* 1,reg a ...
- Java 修改Windows注册表,以实现开机自启动应用程序。
使用Java修改Windows注册表,使用最基本的就是cmd命令. 事例和运行结果如下所示: package day01; import java.io.IOException; /* 1,reg a ...
- Selenium2学习-001-Selenium2 WebUI自动化Java开发 Windows 环境配置
此文主要介绍 Selenium2 WebUI自动化Java开发 Windows 环境配置,供各位亲们参考,若有不足之处,敬请各位大神指正,非常感谢! 所需软件列表如下所示: 所属分类 具体名称 备注 ...
- 彻底理解Cisco/Linux/Windows的IP路由
-1.只要理解实质,名称并不重要! 很多使用Linux的网络高手在面对Cisco管理员的诸如管理距离,路由度量等词汇时,还没有PK就自觉败下阵来了.我觉得这实在太可惜了,大家本是一家,为何这么为难对方 ...
- Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例【附详细代码】
http://blog.csdn.net/xiefu5hh/article/details/51707529 Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例[附 ...
随机推荐
- 异步加载JS的4种方式(详解)
方案1:$(document).ready <!DOCTYPE html> <html> <head> <script src="http://co ...
- CentOS的网络配置(终端环境)
在虚拟机中安装了Server版的CentOS,用于做Hadoop相关的实验.Server版的CentOS默认是没有桌面环境的,eth0的网络默认也没有开启,需要我们手动启动网络. 关键配置说明 网络和 ...
- GaugeControl 数字时钟,温度计,仪表盘
https://documentation.devexpress.com/#WindowsForms/CustomDocument18217 This topic will guide you thr ...
- CSS背景样式
CSS是级联样式表,用来表现HTML等文件样式的语言,CSS能够真正做到网页的表现与内容分离的设计语言,也就是说,做好了一款网页,可以通过另一个后缀名是css的文件进行修改其中的样式,不过在html的 ...
- 《第一本docker书》第4章 使用docker镜像和仓库 读书笔记
docker最底端是一个引导文件系统,即bootfs. 第二层是root文件系统rootfs,位于引导文件系统之上. 在传统的Linux引导过程中,root文件系统会最先以只读的方式加载,当引导结束并 ...
- Python之SQLAlchemy学习
alchemy 英[ˈælkəmi] 美[ˈælkəmi] n. 炼金术; 炼丹术; (改变事物.物质的)魔力(或方法) ; (事物.物质的) 神秘变化; SQLAlchemy是Python中最有名的 ...
- xcode7 打开工程错误 This Document requires xcode8.0 or later.
xcode7 打开工程错误 This Document requires xcode8.0 or later. 场景: xcode7创建的工程,后来安装了xcode8.0,用8打开7的工程跑了一下: ...
- jquery checkbox勾选/取消勾选的诡异问题
<form> 你爱好的运动是?<input type="checkbox" id="CheckedAll" />全选/全不选<br ...
- 6/8/9/10/11 Sprint2 看板和燃尽图
端午放假,大家都回家了 页面模块的大体设计,因所找的资料不全,还待改善
- vs2012中VC连接mysql
在MySQL的安装目录下你会找到include和lib文件夹(完全安装模式下),里面分别是C接口的头文件和库文件,库文件只用libmysql.lib就好了,即把libmysql.lib拷贝到了VC++ ...