當前位置:首頁 » 幣種行情 » java獲取eth0網卡ip

java獲取eth0網卡ip

發布時間: 2022-09-10 20:26:44

⑴ 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。

熱點內容
布局區塊鏈生態 發布:2025-08-25 11:03:07 瀏覽:249
幣圈和外匯 發布:2025-08-25 11:02:36 瀏覽:872
ltc幣市值高點 發布:2025-08-25 10:47:26 瀏覽:93
夫妻炒比特幣虧20000萬 發布:2025-08-25 10:37:36 瀏覽:195
遼寧投資比特幣 發布:2025-08-25 10:20:07 瀏覽:229
比特幣VC投資人 發布:2025-08-25 10:14:42 瀏覽:895
氪石幣礦池 發布:2025-08-25 10:05:33 瀏覽:635
我的以太坊錢包 發布:2025-08-25 10:05:00 瀏覽:212
區塊鏈銅產業應用 發布:2025-08-25 10:04:19 瀏覽:281
國盾區塊鏈注冊 發布:2025-08-25 09:57:57 瀏覽:725