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)中對應(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ū)動程序控制。
(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ū)動程序。
虛擬網(wǎng)絡(luò)接口和真實(shí)存在的網(wǎng)絡(luò)接口在使用上是一致的。
(4) 網(wǎng)絡(luò)接口的創(chuàng)建
硬件網(wǎng)卡的網(wǎng)絡(luò)接口由驅(qū)動程序創(chuàng)建。而虛擬的網(wǎng)絡(luò)接口由系統(tǒng)創(chuàng)建或通過應(yīng)用層程序創(chuàng)建。
驅(qū)動中創(chuàng)建網(wǎng)絡(luò)接口的函數(shù)是:register_netdev(struct net_device *)或者register_netdevice(struct net_device *)。
這兩個(gè)函數(shù)的區(qū)別是:register_netdev(…)會自動生成以”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.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)文章
mybatis-plus QueryWrapper and or 連用并且實(shí)現(xiàn)分
這篇文章主要介紹了mybatis-plus QueryWrapper and or 連用并且實(shí)現(xiàn)分頁,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Java實(shí)現(xiàn)簡單的表達(dá)式計(jì)算器功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)簡單的表達(dá)式計(jì)算器功能,結(jié)合實(shí)例形式分析了Java針對輸入表達(dá)式的符號分解與數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
fastjson全局日期序列化設(shè)置導(dǎo)致JSONField失效問題解決方案
這篇文章主要介紹了fastjson通過代碼指定全局序列化返回時(shí)間格式,導(dǎo)致使用JSONField注解標(biāo)注屬性的特殊日期返回格式失效問題的解決方案2023-01-01
java利用StringTokenizer分割字符串的實(shí)現(xiàn)
利用java.util.StringTokenizer的方法,可以將一個(gè)字符串拆分為一系列的標(biāo)記,本文就來介紹一下java利用StringTokenizer分割字符串的實(shí)現(xiàn),感興趣的可以了解一下2023-10-10
Java并發(fā)(Runnable+Thread)實(shí)現(xiàn)硬盤文件搜索功能
這篇文章主要介紹了Java并發(fā)(Runnable+Thread)實(shí)現(xiàn)硬盤文件搜索,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
java 集合并發(fā)操作出現(xiàn)的異常ConcurrentModificationException
Map在遍歷時(shí)候通常 現(xiàn)獲得其鍵值的集合Set,然后用迭代器Iterator來對Map進(jìn)行遍歷。2009-06-06

