import java.io.IOException; import javax.swing.JOptionPane; public class Neusoft_IP { /**
* @param args
*/
public static void main(String[] args)
{
Neusoft_IP ip = new Neusoft_IP();
int i = ip.getIp();
Object[] options={"宿舍","A102","C201","A410","B103"};
Object n=JOptionPane.showInputDialog(null, "你所在的位置?", "IP切换", JOptionPane.DEFAULT_OPTION, null, options, options[0]);
if(n=="C201"){
try {
Runtime.getRuntime().exec( "Netsh interface ip set address \"本地连接\" static 172.26.104."+i+" 255.255.255.0 172.26.104.254 1");
Runtime.getRuntime().exec( "Netsh interface ip set dns name=\"本地连接\" static addr=10.6.6.6 register=PRIMARY");
Runtime.getRuntime().exec( "Netsh interface ip add dns name=\"本地连接\" addr=202.96.128.86 index=2");}
catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getIp(){
boolean ok = true;
double num = Math.random()*1000;
int ip4 = (int)num;
while(ok){
if(ip4 <=255){
ok = false;
}
else{
ip4 = (int)(Math.random()*1000);
}
}
return ip4;
} }
上面代码可以更改电脑的IP和DNS,但是不是很完美!在IP地址表的选项为自动获取IP的时候,运行本段代码,是不可以一下子全部更改完全的。第一次是改主DNS,第二次是改IP,然后的第三次后第四次,第五次,才可能改得了副DNS,
现在向各位大大求援,如何让这段代码,在IP地址为自动获取的前提下,运行一次就可以把IP地址,主副DNS改好!
请大家帮帮忙!!!
* @param args
*/
public static void main(String[] args)
{
Neusoft_IP ip = new Neusoft_IP();
int i = ip.getIp();
Object[] options={"宿舍","A102","C201","A410","B103"};
Object n=JOptionPane.showInputDialog(null, "你所在的位置?", "IP切换", JOptionPane.DEFAULT_OPTION, null, options, options[0]);
if(n=="C201"){
try {
Runtime.getRuntime().exec( "Netsh interface ip set address \"本地连接\" static 172.26.104."+i+" 255.255.255.0 172.26.104.254 1");
Runtime.getRuntime().exec( "Netsh interface ip set dns name=\"本地连接\" static addr=10.6.6.6 register=PRIMARY");
Runtime.getRuntime().exec( "Netsh interface ip add dns name=\"本地连接\" addr=202.96.128.86 index=2");}
catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getIp(){
boolean ok = true;
double num = Math.random()*1000;
int ip4 = (int)num;
while(ok){
if(ip4 <=255){
ok = false;
}
else{
ip4 = (int)(Math.random()*1000);
}
}
return ip4;
} }
上面代码可以更改电脑的IP和DNS,但是不是很完美!在IP地址表的选项为自动获取IP的时候,运行本段代码,是不可以一下子全部更改完全的。第一次是改主DNS,第二次是改IP,然后的第三次后第四次,第五次,才可能改得了副DNS,
现在向各位大大求援,如何让这段代码,在IP地址为自动获取的前提下,运行一次就可以把IP地址,主副DNS改好!
请大家帮帮忙!!!
Netsh interface ip ... && Netsh interface ip ... && Netsh interface ip ...[/code]
两种方式连接试试
你可以先把配置单生成到一个文件中,然后用netsh -f c:\配置单.txt //导入
另外,可以用netsh -c interface dump>c:\配置单.txt //导出
Process process;
process=Runtime.getRuntime().exec( "Netsh interface ip set address \"本地连接\" static 172.26.104."+i+" 255.255.255.0 172.26.104.254 1");
process.waitFor();
process=Runtime.getRuntime().exec( "Netsh interface ip set dns name=\"本地连接\" static addr=10.6.6.6 register=PRIMARY");
process.waitFor();
process=Runtime.getRuntime().exec( "Netsh interface ip add dns name=\"本地连接\" addr=202.96.128.86 index=2");
process.waitFor();exec(String command)
在单独的进程中执行指定的字符串命令。
猜测:不能多进程同时修改ip和dns
简单开两个cmd命令行窗口 验证了下 报如下错误:
无法访问配置,因为另一配置工具正在访问它。
关闭其他窗口,然后再试一次。
程序中可以使用process的getErrorStream() 获取子进程的错误流 获取错误信息
下面的是我求的答案:
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;import javax.swing.JOptionPane;public class Neusoft_IP { /**
* @param args
*/
public static void main(String[] args) {
Neusoft_IP ip = new Neusoft_IP();
int i = ip.getIp();
Object[] options = {"宿舍","宽带","A102","A410","B103","C201"};
Object n = JOptionPane.showInputDialog(null, "你所在的位置?", "IP切换", JOptionPane.DEFAULT_OPTION, null, options, options[0]); try {
Process p = Runtime.getRuntime().exec("Netsh");
BufferedOutputStream w = new BufferedOutputStream(p.getOutputStream());
final BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
Thread t = new Thread() {
public void run() {
try {
String line;
while ((line = r.readLine()) != null) {
System.out.println(line);
}
r.close();
} catch (Exception e) { }
}
};
t.start(); w.write("interface ip\r\n".getBytes()); if ("宿舍".equals(n)) {
w.write("set address \"本地连接\" static 10.1.43.157 255.255.255.0 10.1.43.254 1\r\n".getBytes());
w.write("set dns name=\"本地连接\" static addr=172.16.3.3 register=PRIMARY\r\n".getBytes());
w.write("add dns name=\"本地连接\" addr=172.16.3.4 index=2\r\n".getBytes());
} else if ("宽带".equals(n)) {
w.write("set address \"本地连接\" dhcp\r\n".getBytes());
w.write("set dns \"本地连接\" dhcp\r\n".getBytes());
} else if ("C201".equals(n)) {
w.write(("set address \"本地连接\" static 172.26.104." + i + " 255.255.255.0 172.26.104.254 1\r\n").getBytes()); // 再设dns
w.write("set dns name=\"本地连接\" static addr=172.16.3.3 register=PRIMARY\r\n".getBytes());
w.write("add dns name=\"本地连接\" addr=172.16.3.4 index=2\r\n".getBytes());
} else if ("A102".equals(n)) {
w.write(("set address \"本地连接\" static 172.26.22." + i + " 255.255.255.0 172.26.22.254 1\r\n").getBytes()); // 再设dns
w.write("set dns name=\"本地连接\" static addr=172.16.3.3 register=PRIMARY\r\n".getBytes());
w.write("add dns name=\"本地连接\" addr=172.16.3.4 index=2\r\n".getBytes());
} else if ("A410".equals(n)) {
w.write(("set address \"本地连接\" static 172.26.63." + i + " 255.255.255.0 172.26.63.254 1\r\n").getBytes()); // 再设dns
w.write("set dns name=\"本地连接\" static addr=172.16.3.3 register=PRIMARY\r\n".getBytes());
w.write("add dns name=\"本地连接\" addr=172.16.3.4 index=2\r\n".getBytes());
} else if ("B103".equals(n)) {
w.write(("set address \"本地连接\" static 172.26.41." + i + " 255.255.255.0 172.26.41.254 1\r\n").getBytes()); // 再设dns
w.write("set dns name=\"本地连接\" static addr=172.16.3.3 register=PRIMARY\r\n".getBytes());
w.write("add dns name=\"本地连接\" addr=172.16.3.4 index=2\r\n".getBytes());
} w.flush();
w.write("quit\r\n".getBytes());
w.flush();
w.close();
} catch (IOException e) {
e.printStackTrace();
}
} public int getIp() {
return (int) (Math.random() * 255);
}}
能实现我说的所有功能!
但我认为一个好的代码,是要不断改进的,希望大家继续提供更简单实用的方法!谢谢!