java获取eth0网卡ip
⑴ javaWeb开发,如何取得客户端的MAC,注意,客户端是子网的一个PC
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.net.InetAddress;
importjava.net.NetworkInterface;
/**
*与系统相关的一些常用工具方法.
*
*@authorlvbogun
*@version1.0.0
*/
publicclassSystemTool{
/**
*获取当前操作系统名称.return操作系统名称例如:windowsxp,linux等.
*/
publicstaticStringgetOSName(){
returnSystem.getProperty("os.name").toLowerCase();
}
/**
*获取unix网卡的mac地址.非windows的系统默认调用本方法获取.
*如果有特殊系统请继续扩充新的取mac地址方法.
*
*@returnmac地址
*/
(){
Stringmac=null;
BufferedReaderbufferedReader=null;
Processprocess=null;
try{
//linux下的命令,一般取eth0作为本地主网卡
process=Runtime.getRuntime().exec("ifconfigeth0");
//显示信息中包含有mac地址信息
bufferedReader=newBufferedReader(newInputStreamReader(
process.getInputStream()));
Stringline=null;
intindex=-1;
while((line=bufferedReader.readLine())!=null){
//寻找标示字符串[hwaddr]
index=line.toLowerCase().indexOf("hwaddr");
if(index>=0){//找到了
//取出mac地址并去除2边空格
mac=line.substring(index+"hwaddr".length()+1).trim();
break;
}
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(bufferedReader!=null){
bufferedReader.close();
}
}catch(IOExceptione1){
e1.printStackTrace();
}
bufferedReader=null;
process=null;
}
returnmac;
}
/**
*获取widnows网卡的mac地址.
*
*@returnmac地址
*/
(){
Stringmac=null;
BufferedReaderbufferedReader=null;
Processprocess=null;
try{
//windows下的命令,显示信息中包含有mac地址信息
process=Runtime.getRuntime().exec("ipconfig/all");
bufferedReader=newBufferedReader(newInputStreamReader(
process.getInputStream()));
Stringline=null;
intindex=-1;
while((line=bufferedReader.readLine())!=null){
System.out.println(line);
//寻找标示字符串[physical
index=line.toLowerCase().indexOf("physicaladdress");
if(index>=0){//找到了
index=line.indexOf(":");//寻找":"的位置
if(index>=0){
System.out.println(mac);
//取出mac地址并去除2边空格
mac=line.substring(index+1).trim();
}
break;
}
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(bufferedReader!=null){
bufferedReader.close();
}
}catch(IOExceptione1){
e1.printStackTrace();
}
bufferedReader=null;
process=null;
}
returnmac;
}
/**
*windows7专用获取MAC地址
*
*@return
*@throwsException
*/
()throwsException{
//获取本地IP对象
InetAddressia=InetAddress.getLocalHost();
//获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
byte[]mac=NetworkInterface.getByInetAddress(ia).getHardwareAddress();
//下面代码是把mac地址拼装成String
StringBuffersb=newStringBuffer();
for(inti=0;i<mac.length;i++){
if(i!=0){
sb.append("-");
}
//mac[i]&0xFF是为了把byte转化为正整数
Strings=Integer.toHexString(mac[i]&0xFF);
sb.append(s.length()==1?0+s:s);
}
//把字符串所有小写字母改为大写成为正规的mac地址并返回
returnsb.toString().toUpperCase();
}
}
写一个全局拦截的servlet,只要有请求的时候就调用这个类里面的获取mac地址的方法
Stringos=getOSName();
System.out.println(os);
if(os.equals("windows7")){
Stringmac=getMACAddress();
System.out.println(mac);
}elseif(os.startsWith("windows")){
//本地是windows
Stringmac=getWindowsMACAddress();
System.out.println(mac);
}else{
//本地是非windows系统一般就是unix
Stringmac=getUnixMACAddress();
System.out.println(mac);
}
记得判断一下是什么系统
⑵ linux 网卡eth0检测时没有IP地址,怎么回事
我想你的网卡IP地址的获取方式是DHCP方式,你可以设置成为static方式,方法是:执行setup命令,选择network设置这一项,来设置静态IP地址获取方式,并设置IP地址。
然后,再启动一下network服务:
service network restart
再用ifconfig命令查看一下IP地址是不是有了呢?
⑶ linux下如何查看某个网卡当前使用的IP。
1、连接上相应的linux主机,进入到等待输入shell指令的linux命令行状态下。
⑷ 用java语言怎么修改linux下eth0网卡的ip地址,请高手指点下,最好有例子....
vi /etc/sysconfig/network-scripts/ifcfg-eth0
点击i 成插入状态
添加
IPADDR=ccc.ccc.vvv.bbb
按下esc
输入:wq 回车结束(冒号也是要输入的)
需要重启服务才能设置成刚才的ip
命令 为
service network restart
那个我也是做java的
我到现在都没听过java可以改ip的
我只听过有侦测IP啊 获取ip。。这些
至于修改ip 我真没听过
你见过.net可以修改自己机子的ip的嘛。
我也没见过
⑸ linux 网卡eth0检测时没有IP地址,怎么回事
我想你的网卡IP地址的获取方式是DHCP方式,你可以设置成为static方式,方法是:执行setup命令,选择network设置这一项,来设置静态IP地址获取方式,并设置IP地址。
然后,再启动一下network服务:
service
network
restart
再用ifconfig命令查看一下IP地址是不是有了呢?
⑹ java程序改linux IP、子网和网关
???不懂 我就知道这点
ip addr add dev eth0
ip link set dev eth0
vi /etc/rc.d/rc.local ifcongfig etho IP或者写个脚本
bash
再就是改/etc/sysconfig/network-scrips/ifcfg-eth0然后 ifdown eth0 再ifup eth0 或者service network restart 重启网卡服务 就不用重启LINUX了
SETUP 或者netconfig也可以
⑺ linux 网卡eth0检测时没有IP地址,怎么回事
你没有配IP怎么会有,除非你自动获取了,
自己动手配置命令:
ifconfig
eth0
IP地址
netmask
子网掩码
自动获取ip地址:
dhclient
⑻ linux下如何获取网卡IP地址及MAC地址
一般的linux系统的话输入
/sbin/ifconfig
然后eth0一般就是你的网卡,里面找HWaddr后面就是MAC地址
⑼ java如何查询本机ip地址和mac地址
Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:
importjava.net.*;
publicclassNetInfo{
publicstaticvoidmain(String[]args){
newNetInfo().say();
}
publicvoidsay(){
try{
InetAddressi=InetAddress.getLocalHost();
System.out.println(i);//计算机名称和IP
System.out.println(i.getHostName());//名称
System.out.println(i.getHostAddress());//只获得IP
}
catch(Exceptione){e.printStackTrace();}
}
}
也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。