java獲取linux服務(wù)器上的IP操作
在編碼過(guò)程中需要獲取本地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 UNKNOWNlink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host loinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2:
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000link/ether 00:50:56:a2:0d:1b brd ff:ff:ff:ff:ff:ffinet 10.12.8.243/24 brd 10.12.8.255 scope global eth0inet6 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的簡(jiǎn)寫(xiě),一般用于以太網(wǎng)接口。
wifi0:wifi是無(wú)線局域網(wǎng),因此wifi0一般指無(wú)線網(wǎng)絡(luò)接口。
ath0: Atheros的簡(jiǎn)寫(xiě),一般指Atheros芯片所包含的無(wú)線網(wǎng)絡(luò)接口。
lo: local的簡(jiǎn)寫(xiě),一般指本地環(huán)回接口。
(2) 網(wǎng)絡(luò)接口如何工作
網(wǎng)絡(luò)接口是用來(lái)發(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)建或通過(guò)應(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(…)也是通過(guò)調(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)生的, 它們將通過(guò)外出鏈的’lo’接口,然后返回進(jìn)入鏈的’lo’接口。
其實(shí)getLocalHost方法獲取的是lo接口對(duì)應(yīng)的IP地址,了解了上述問(wèn)題那java編碼如何獲取正確的地址呢?
java為了方便網(wǎng)絡(luò)編程,提供了表示IP地址的類、表示網(wǎng)絡(luò)接口(這個(gè)接口是指網(wǎng)卡)的類,表示網(wǎng)絡(luò)連接接口的類,例如InetAddress,但是測(cè)試發(fā)現(xiàn)NetworkInterface類同樣提供了獲取本地計(jì)算機(jī)網(wǎng)絡(luò)接口相關(guān)的信息的方法。盡管InetAddress類提供獲取IP地址的方法,但是要想獲取本機(jī)的網(wǎng)絡(luò)接口的詳細(xì)信息,還需要依賴NetworkInterface接口中的方法。測(cè)試發(fā)現(xiàn)下面方法可以獲得服務(wù)器對(duì)應(yīng)的IP地址,在linux服務(wù)器上和本地測(cè)試通過(guò)
(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.1System.out.println('loop addr:' + inetAddress);} else if (inetAddress.isLinkLocalAddress()) {//169.254.x.xSystem.out.println('link addr:' + inetAddress);} else { //非鏈接和回路真實(shí)ipSystem.out.println('ip:' + inetAddress);} } }
結(jié)果:
loop addr:/127.0.0.1loop addr:/0:0:0:0:0:0:0:1ip:/192.168.10.89
以上這篇java獲取linux服務(wù)器上的IP操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 初學(xué)者學(xué)習(xí)Python好還是Java好2. Java面向?qū)ο蠡A(chǔ)教學(xué)(三)3. python裝飾器三種裝飾模式的簡(jiǎn)單分析4. Python實(shí)現(xiàn)迪杰斯特拉算法過(guò)程解析5. Python字符串函數(shù)strip()原理及用法詳解6. python使用ctypes庫(kù)調(diào)用DLL動(dòng)態(tài)鏈接庫(kù)7. python中sklearn的pipeline模塊實(shí)例詳解8. python gstreamer實(shí)現(xiàn)視頻快進(jìn)/快退/循環(huán)播放功能9. Python如何進(jìn)行時(shí)間處理10. 詳解Python模塊化編程與裝飾器
