- 博客(0)
- 资源 (2)
- 收藏
- 关注
查看本机网络信息
利用多进程,调用DOS命令查看网络信息
public class ipconfig extends JFrame{
static TextField ip_text=new TextField(15);
static TextField mac_text=new TextField(15);
static TextField subnetmask_text=new TextField(15);
static TextField gateway_text=new TextField(15);
static TextField dns_text=new TextField(15);
static JPanel subnetmask=new JPanel(new FlowLayout());
static JPanel gateway=new JPanel(new FlowLayout());
static JPanel IP=new JPanel(new FlowLayout());
static JPanel dns=new JPanel(new FlowLayout());
public static void main(String[] args) throws UnknownHostException {
ipconfig f=new ipconfig();
f.setTitle("查看本机网络设置QQ125004485");
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocation(400,400);
f.setSize(new Dimension(200,300));
// gotoUrlWindows("http://ygsn.taobao.com/");
//gotoUrlWindows("www.sohu.com");
f.setResizable(false);
f.setLayout(new GridLayout(6,1));
//IP文本框
setCFont(IP);
IP.add(new Label("IP:"));
IP.add(ip_text);
ip_text.setText(getLocalIp());
//MAC 文本框
JPanel MAC=new JPanel(new FlowLayout());
setCFont(MAC);
MAC.add(new Label("MAC:"));
MAC.add(mac_text);
mac_text.setText(getMacAddressIP(ip_text.getText()));
//子网掩码
setCFont(subnetmask);
subnetmask.add(new Label("子网掩码:"));
subnetmask.add(subnetmask_text);
subnetmask_text.setText(getSubnetMask());
//网关
setCFont(gateway);
gateway.add(new Label("网关:"));
gateway.add(gateway_text);
gateway_text.setText(getGateWay());
//DNS
setCFont(dns);
dns.add(new Label("DNS:"));
dns.add(dns_text);
dns_text.setText(getDns());
//加载文本框
f.add(subnetmask);
f.add(MAC);
f.add(IP);
f.add(gateway);
f.add(dns);
f.pack();
}
// Windows platform goto url
private static void gotoUrlWindows(String url){
String cmd = "rundll32 url.dll,FileProtocolHandler " + url;
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
}
//取IP
private static String getLocalIp() throws UnknownHostException
{
return InetAddress.getLocalHost().getHostAddress();
}
//设置字体
private static void setCFont(Container c)
{
c.setFont(new Font( "宋体 ",Font.BOLD,15));
}
public static String getMacAddressIP(String remotePcIP) {
String str = "";
String macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
macAddress = str.substring(
str.indexOf("MAC Address") + 14, str.length());
break;
}
}
}
} catch (IOException ex) {
}
return macAddress;
}
public static String getSubnetMask() {
String str = "";
String subnetmask = "";
try {
Process pp = Runtime.getRuntime().exec("ipconfig /all");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("Subnet Mask") > -1) {
subnetmask = str.substring(
str.indexOf("Subnet Mask") + 36, str.length());
break;
}
}
}
} catch (IOException ex) {
}
return subnetmask;
}
public static String getGateWay() {
String str = "";
String gateway = "";
try {
Process pp = Runtime.getRuntime().exec("ipconfig /all");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("Default Gateway") > -1) {
gateway = str.substring(
str.indexOf("Default Gateway") + 36, str.length());
break;
}
}
}
} catch (IOException ex) {
}
return gateway;
}
public static String getDns() {
String str = "";
String dns = "";
try {
Process pp = Runtime.getRuntime().exec("ipconfig /all");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("DNS Servers") > -1) {
dns = str.substring(
str.indexOf("DNS Servers") + 36, str.length());
break;
}
}
}
} catch (IOException ex) {
}
return dns;
}
}
2012-03-03
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人