如何用java来读取LINUX系统的IP值
时间:2008-11-17 来源:hkebao
如果用如下的代码来linux中运行的话:
try {
InetAddress inet = InetAddress.getLocalHost();
serverip = inet.getHostName();
} catch(Exception e) {
System.out.println(e.toString());
}
会输出如下的东西:127.0.0.1
不能输出实际的IP地址哦!
我于是就请问了一下其他的高手:其中有个高手说可以用linux中的系统命令来做具体如下:
import java.io.*;
public class DT {
public static void main(String[] args) throws IOException
{
String command="ipconfig";//系统命令哦
Runtime r=Runtime.getRuntime();
Process p=r.exec(command);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
while(null!=(inline=br.readLine())){
sb.append(inline).append("\n");
}
System.out.println(sb.toString());
}
}
这样的话是可以将所读的命令出来。但是这样比较麻烦哦。不可用!
之后在网上查找了些资料:可以用这种方法来解决哦!
我在linux中试了一下可以得到我的真实IP值 哦!
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class GetIp {
public static void main(String[] args){
String ip = null;
GetIp test = new GetIp();
try{
ip = test.getLocalSiteIP();
}catch(Exception e){
System.out.print(e.toString());
}
System.out.print(ip); //得到192.168.100.90
}
private String getLocalSiteIP() throws Exception {
String siteString = "";
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
InetAddress ip = (InetAddress) ni.getInetAddresses().nextElement();
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() &&
ip.getHostAddress().indexOf(":") == -1) {
siteString = ip.getHostAddress();
}
}
return siteString;
}
}
下面这个代码比较实用哦。大家 如果 遇到了要用JAVA读写linux中的IP的话可以用哦!肯定是可以的啊!
那以后如果想读linux中的IP就可以直接用这段代码了哦!比较方便一点!
现在来看一个高手写的代码他是通过linux自带的命令来进行读写的哦!这样的话可以linux调用系统命令哦!
看代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.ProcessBuilder;
public class Test {
public static String getMACAddress() {
String address = "";
String os = System.getProperty("os.name");
System.out.println(os);
if (os != null) {
if (os.startsWith("Windows")) {
try {
ProcessBuilder pb = new ProcessBuilder("ipconfig","/all");
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("IP Address") != -1) {
int index = line.indexOf(":");
address = line.substring(index + 1);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
System.out.print(e.toString());
}
}
else if(os.startsWith("Linux")){
try {
ProcessBuilder pb = new ProcessBuilder("ifconfig");
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line=br.readLine())!=null){
int index=line.indexOf("硬件地址");
if(index!=-1){
address=line.substring(index+4);
break;
}
}
br.close();
return address.trim();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return address;
}
public static void main(String[] args) {
System.out.println("" + Test.getMACAddress());
}
}
可以得到一行字筗串出来:
"inet addr:192.168.100.90 Bcaset :192.168.100.255 Mask 255.255.255.0"
现在来用JAVA对字筗串的分割来处理看看如何来分割我们所得到 的IP吧呵呵:
来下面的代码就可以分割这行URL了!
import java.util.StringTokenizer;
public class SplitStr {
public static void main(String[] args){
String str = "inet addr:192.168.100.90 B cast:192.168.100.255 Mask:255.255.255.0";
StringTokenizer commaTo = new StringTokenizer(str, ":");
String[] re = new String[commaTo.countTokens()];
for (int k=0;k<re.length;k++) {
re[k] = commaTo.nextToken();
}
StringTokenizer commaTos = new StringTokenizer(re[1], " ");
String[] res = new String[commaTos.countTokens()];
for (int k=0;k<res.length;k++) {
res[k] = commaTos.nextToken();
}
System.out.print(res[0]);
}
}
可以得到:192.168.100.90
好了,现在可以读取出来linux形式了!
try {
InetAddress inet = InetAddress.getLocalHost();
serverip = inet.getHostName();
} catch(Exception e) {
System.out.println(e.toString());
}
会输出如下的东西:127.0.0.1
不能输出实际的IP地址哦!
我于是就请问了一下其他的高手:其中有个高手说可以用linux中的系统命令来做具体如下:
import java.io.*;
public class DT {
public static void main(String[] args) throws IOException
{
String command="ipconfig";//系统命令哦
Runtime r=Runtime.getRuntime();
Process p=r.exec(command);
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
while(null!=(inline=br.readLine())){
sb.append(inline).append("\n");
}
System.out.println(sb.toString());
}
}
这样的话是可以将所读的命令出来。但是这样比较麻烦哦。不可用!
之后在网上查找了些资料:可以用这种方法来解决哦!
我在linux中试了一下可以得到我的真实IP值 哦!
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class GetIp {
public static void main(String[] args){
String ip = null;
GetIp test = new GetIp();
try{
ip = test.getLocalSiteIP();
}catch(Exception e){
System.out.print(e.toString());
}
System.out.print(ip); //得到192.168.100.90
}
private String getLocalSiteIP() throws Exception {
String siteString = "";
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
InetAddress ip = (InetAddress) ni.getInetAddresses().nextElement();
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() &&
ip.getHostAddress().indexOf(":") == -1) {
siteString = ip.getHostAddress();
}
}
return siteString;
}
}
下面这个代码比较实用哦。大家 如果 遇到了要用JAVA读写linux中的IP的话可以用哦!肯定是可以的啊!
那以后如果想读linux中的IP就可以直接用这段代码了哦!比较方便一点!
现在来看一个高手写的代码他是通过linux自带的命令来进行读写的哦!这样的话可以linux调用系统命令哦!
看代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.ProcessBuilder;
public class Test {
public static String getMACAddress() {
String address = "";
String os = System.getProperty("os.name");
System.out.println(os);
if (os != null) {
if (os.startsWith("Windows")) {
try {
ProcessBuilder pb = new ProcessBuilder("ipconfig","/all");
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("IP Address") != -1) {
int index = line.indexOf(":");
address = line.substring(index + 1);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
System.out.print(e.toString());
}
}
else if(os.startsWith("Linux")){
try {
ProcessBuilder pb = new ProcessBuilder("ifconfig");
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line=br.readLine())!=null){
int index=line.indexOf("硬件地址");
if(index!=-1){
address=line.substring(index+4);
break;
}
}
br.close();
return address.trim();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return address;
}
public static void main(String[] args) {
System.out.println("" + Test.getMACAddress());
}
}
可以得到一行字筗串出来:
"inet addr:192.168.100.90 Bcaset :192.168.100.255 Mask 255.255.255.0"
现在来用JAVA对字筗串的分割来处理看看如何来分割我们所得到 的IP吧呵呵:
来下面的代码就可以分割这行URL了!
import java.util.StringTokenizer;
public class SplitStr {
public static void main(String[] args){
String str = "inet addr:192.168.100.90 B cast:192.168.100.255 Mask:255.255.255.0";
StringTokenizer commaTo = new StringTokenizer(str, ":");
String[] re = new String[commaTo.countTokens()];
for (int k=0;k<re.length;k++) {
re[k] = commaTo.nextToken();
}
StringTokenizer commaTos = new StringTokenizer(re[1], " ");
String[] res = new String[commaTos.countTokens()];
for (int k=0;k<res.length;k++) {
res[k] = commaTos.nextToken();
}
System.out.print(res[0]);
}
}
可以得到:192.168.100.90
好了,现在可以读取出来linux形式了!
相关阅读 更多 +