久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

java獲取linux服務(wù)器上的IP操作

瀏覽:57日期:2022-08-26 16:13:04

在編碼過程中需要獲取本地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的簡寫,一般用于以太網(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)中對應(yīng)于一個(gè)struct net_device結(jié)構(gòu)體,包含name,mac,mask,mtu…信息。

每個(gè)硬件網(wǎng)卡(一個(gè)MAC)對應(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(…)會自動(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接口對應(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ù)器對應(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ǔ)充知識: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操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 西和县| 思南县| 文安县| 南阳市| 宁安市| 象山县| 普安县| 方山县| 丹东市| 晋城| 房产| 边坝县| 信宜市| 田东县| 新安县| 通州市| 静宁县| 宜章县| 南开区| 合江县| 本溪市| 晋江市| 潼南县| 玉田县| 柳林县| 邹城市| 张掖市| 沈阳市| 内乡县| 和平县| 福泉市| 镇远县| 西畴县| 金沙县| 恩施市| 神农架林区| 南和县| 甘孜县| 弋阳县| 兴化市| 三河市|