java獲取linux服務(wù)器上的IP操作
在編碼過程中需要獲取本地IP地址,首先使用的是下面的方法,在Windows環(huán)境正常,但是linux服務(wù)器上就獲取不到,
public static String getIpAddress() { String hostAddress = ""; try { InetAddress address = InetAddress.getLocalHost(); hostAddress = address.getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } return hostAddress; }
這樣在linux上依然獲取到的是127.0.0.1,
查詢服務(wù)器上面IP發(fā)現(xiàn):
[mm_cbms1@localhost ~]$ ip address
1:
lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2:
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:50:56:a2:0d:1b brd ff:ff:ff:ff:ff:ff
inet 10.12.8.243/24 brd 10.12.8.255 scope global eth0
inet6 fe80::250:56ff:fea2:d1b/64 scope link
valid_lft forever preferred_lft forever
這里首先要了解上面列出的接口中的含義:
1、linux的網(wǎng)絡(luò)接口之掃盲
(1) 網(wǎng)絡(luò)接口的命名
這里并不存在一定的命名規(guī)范,但網(wǎng)絡(luò)接口名字的定義一般都是要有意義的。例如:
eth0: ethernet的簡寫,一般用于以太網(wǎng)接口。
wifi0:wifi是無線局域網(wǎng),因此wifi0一般指無線網(wǎng)絡(luò)接口。
ath0: Atheros的簡寫,一般指Atheros芯片所包含的無線網(wǎng)絡(luò)接口。
lo: local的簡寫,一般指本地環(huán)回接口。
(2) 網(wǎng)絡(luò)接口如何工作
網(wǎng)絡(luò)接口是用來發(fā)送和接受數(shù)據(jù)包的基本設(shè)備。
系統(tǒng)中的所有網(wǎng)絡(luò)接口組成一個(gè)鏈狀結(jié)構(gòu),應(yīng)用層程序使用時(shí)按名稱調(diào)用。
每個(gè)網(wǎng)絡(luò)接口在linux系統(tǒng)中對(duì)應(yīng)于一個(gè)struct net_device結(jié)構(gòu)體,包含name,mac,mask,mtu…信息。
每個(gè)硬件網(wǎng)卡(一個(gè)MAC)對(duì)應(yīng)一個(gè)網(wǎng)絡(luò)接口,其工作完全由相應(yīng)的驅(qū)動(dòng)程序控制。
(3) 虛擬網(wǎng)絡(luò)接口
虛擬網(wǎng)絡(luò)接口的應(yīng)用范圍非常廣泛。最著名的當(dāng)屬“l(fā)o”了,基本上每個(gè)linux系統(tǒng)都有這個(gè)接口。
虛擬網(wǎng)絡(luò)接口并不真實(shí)地從外界接收和發(fā)送數(shù)據(jù)包,而是在系統(tǒng)內(nèi)部接收和發(fā)送數(shù)據(jù)包,因此虛擬網(wǎng)絡(luò)接口不需要驅(qū)動(dòng)程序。
虛擬網(wǎng)絡(luò)接口和真實(shí)存在的網(wǎng)絡(luò)接口在使用上是一致的。
(4) 網(wǎng)絡(luò)接口的創(chuàng)建
硬件網(wǎng)卡的網(wǎng)絡(luò)接口由驅(qū)動(dòng)程序創(chuàng)建。而虛擬的網(wǎng)絡(luò)接口由系統(tǒng)創(chuàng)建或通過應(yīng)用層程序創(chuàng)建。
驅(qū)動(dòng)中創(chuàng)建網(wǎng)絡(luò)接口的函數(shù)是:register_netdev(struct net_device *)或者register_netdevice(struct net_device *)。
這兩個(gè)函數(shù)的區(qū)別是:register_netdev(…)會(huì)自動(dòng)生成以”eth”作為打頭名稱的接口,而register_netdevice(…)需要提前指定接口名稱.事實(shí)上,register_netdev(…)也是通過調(diào)用register_netdevice(…)實(shí)現(xiàn)的。
2、LINUX中的lo(回環(huán)接口)
1) 什么是LO接口?
在LINUX系統(tǒng)中,除了網(wǎng)絡(luò)接口eth0,還可以有別的接口,比如lo(本地環(huán)路接口)。
2) LO接口的作用是什么?
假如包是由一個(gè)本地進(jìn)程為另一個(gè)本地進(jìn)程產(chǎn)生的, 它們將通過外出鏈的'lo'接口,然后返回進(jìn)入鏈的'lo'接口。
其實(shí)getLocalHost方法獲取的是lo接口對(duì)應(yīng)的IP地址,了解了上述問題那java編碼如何獲取正確的地址呢?
java為了方便網(wǎng)絡(luò)編程,提供了表示IP地址的類、表示網(wǎng)絡(luò)接口(這個(gè)接口是指網(wǎng)卡)的類,表示網(wǎng)絡(luò)連接接口的類,例如InetAddress,但是測試發(fā)現(xiàn)NetworkInterface類同樣提供了獲取本地計(jì)算機(jī)網(wǎng)絡(luò)接口相關(guān)的信息的方法。盡管InetAddress類提供獲取IP地址的方法,但是要想獲取本機(jī)的網(wǎng)絡(luò)接口的詳細(xì)信息,還需要依賴NetworkInterface接口中的方法。測試發(fā)現(xiàn)下面方法可以獲得服務(wù)器對(duì)應(yīng)的IP地址,在linux服務(wù)器上和本地測試通過
(1)
public static String getInet4Address() { Enumeration<NetworkInterface> nis; String ip = null; try { nis = NetworkInterface.getNetworkInterfaces(); for (; nis.hasMoreElements();) { NetworkInterface ni = nis.nextElement(); Enumeration<InetAddress> ias = ni.getInetAddresses(); for (; ias.hasMoreElements();) { InetAddress ia = ias.nextElement(); //ia instanceof Inet6Address && !ia.equals("") if (ia instanceof Inet4Address && !ia.getHostAddress().equals("127.0.0.1")) { ip = ia.getHostAddress(); } } } } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ip; }
(2)
public static InetAddress getCurrentIp() { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement(); Enumeration<InetAddress> nias = ni.getInetAddresses(); while (nias.hasMoreElements()) { InetAddress ia = (InetAddress) nias.nextElement(); if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) { return ia; } } } } catch (SocketException e) { logger.error(e.getStackTrace()); } return null; }
上述兩個(gè)方法都可以獲取正確的IP地址,具體NetworkInterface的使用還需要以后應(yīng)用到了進(jìn)行深入研究一下
補(bǔ)充知識(shí):Java獲取所有網(wǎng)卡IP地址
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (inetAddress.isLoopbackAddress()) {//回路地址,如127.0.0.1 System.out.println("loop addr:" + inetAddress); } else if (inetAddress.isLinkLocalAddress()) {//169.254.x.x System.out.println("link addr:" + inetAddress); } else { //非鏈接和回路真實(shí)ip System.out.println("ip:" + inetAddress); } } }
結(jié)果:
loop addr:/127.0.0.1
loop addr:/0:0:0:0:0:0:0:1
ip:/192.168.10.89
以上這篇java獲取linux服務(wù)器上的IP操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用開發(fā)環(huán)境application.properties問題
這篇文章主要介紹了SpringBoot使用開發(fā)環(huán)境application.properties問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07一個(gè)簡單的SpringBoot項(xiàng)目快速搭建詳細(xì)步驟
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,下面這篇文章主要給大家介紹了一個(gè)簡單的SpringBoot項(xiàng)目快速搭建詳細(xì)步驟,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08spring?boot集成WebSocket日志實(shí)時(shí)輸出到web頁面
這篇文章主要為大家介紹了spring?boot集成WebSocket日志實(shí)時(shí)輸出到web頁面展示的詳細(xì)操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03java之static關(guān)鍵字用法實(shí)例解析
這篇文章主要介紹了java之static關(guān)鍵字用法實(shí)例解析,包括了static關(guān)鍵字的原理講解及用法分析,并附帶了實(shí)例說明,需要的朋友可以參考下2014-09-09springboot整合vue實(shí)現(xiàn)上傳下載文件
這篇文章主要為大家詳細(xì)介紹了springboot整合vue實(shí)現(xiàn)上傳下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11