多網絡卡下獲取本機IP的方式。

2021-10-01 23:35:27 字數 1015 閱讀 8974

多網絡卡下獲取本機ip的方式。

方式一:

public class networkutils catch (socketexception e) {

log.error("獲取本地ip 失敗:" + e);

return localhostip;

方式二:

private static string getinetaddress()

try//取得本機的所有網絡卡

enumerationinte***ces = networkinte***ce.getnetworkinte***ces();

inetaddress address = null;

while (inte***ces.hasmoreelements())

networkinte***ce ni = inte***ces.nextelement();

enumerationaddresses = ni.getinetaddresses();

while (addresses.hasmoreelements())

address = addresses.nextelement();

//僅僅取第乙個網絡卡的ip位址就直接返回, 如果是多個網絡卡只返回第乙個網絡卡的ip位址

if (!address.isloopbackaddress() && address.gethostaddress().indexof(":") == -1)

return address.gethostaddress();

logger.warn("[inetaddressutil] can not get the server ip address.");

return null;

catch (throwable t)

logger.warn("[inetaddressutil] get the server ip address failed.", t);

return null;

Python獲取本機IP(多網絡卡)

python獲取本機ip位址的一般方法為 1 2 3 importsocket ip socket.gethostbyname socket.gethostname 通過gethostname獲取主機名,再用gethostbyname將主機名轉換為ip位址。那麼,問題來了。如果主機有多個網絡卡 ip...

linux獲取本機網絡卡ip位址

include include include include include include include include include define eth name eth0 int main strncpy ifr.ifr name,eth name,ifnamsiz ifr.ifr n...

Linux下 獲取本機ip位址

查ip資訊可以使用ifconfig,但有時候我們需要使用指令碼語句來進行自動獲取。方法一 ifconfig a grep inet grep v 127.0.0.1 grep v inet6 awk tr d addr 當前語句可以排查出除了127.0.0.1之外的所有ip位址,但有些時候我們的虛擬...