国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

Linux通過命令僅獲取IP地址的方法

2020-10-28 18:44:40
字體:
來源:轉載
供稿:網友

一同事的朋友正在參加筆試,遇到這么一個問題讓他幫忙解決,結果同事又找到我幫他搞定。真是感慨:通訊發達在某些方面來說,真不知是不是好事啊!題目大致如下所示,一般我們使用ifconfig查看網卡信息,請問你可以通過什么命令,讓其只輸出IP地址192.168.42.128

看似簡單的問題,實現起來也不是太簡單。看看下面的思路吧

[root@DB-Server ~]# ifconfig eth0eth0   Link encap:Ethernet HWaddr 00:0C:29:9E:70:0E      inet addr:192.168.42.128 Bcast:192.168.42.255 Mask:255.255.255.0     inet6 addr: fe80::20c:29ff:fe9e:700e/64 Scope:Link     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1     RX packets:135 errors:0 dropped:0 overruns:0 frame:0     TX packets:216 errors:0 dropped:0 overruns:0 carrier:0     collisions:0 txqueuelen:1000      RX bytes:14062 (13.7 KiB) TX bytes:26007 (25.3 KiB)[root@DB-Server ~]# ifconfig eth0 | grep "inet addr"     inet addr:192.168.42.128 Bcast:192.168.42.255 Mask:255.255.255.0

到這一步非常簡單,接下來就需要借助awk來實現了,如下所示,到此問題解決。

[root@DB-Server ~]# ifconfig eth0 | grep "inet addr" | awk '{ print $2}'addr:192.168.42.128[root@DB-Server ~]# ifconfig eth0 | grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}'192.168.42.128

PS: 獲取Linux下的IP地址

 /** * 獲取Linux下的IP地址 * * @return IP地址 * @throws SocketException */public static String getLinuxLocalIp() throws SocketException {  String ip = "";  try {    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();         en.hasMoreElements();) {      NetworkInterface intf = en.nextElement();      String name = intf.getName();      if (!name.contains("docker") && !name.contains("lo")) {        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();             enumIpAddr.hasMoreElements();) {          InetAddress inetAddress = enumIpAddr.nextElement();          if (!inetAddress.isLoopbackAddress()) {            String ipaddress = inetAddress.getHostAddress().toString();            if (!ipaddress.contains("::") && !ipaddress.contains("0:0:")                && !ipaddress.contains("fe80")) {              ip = ipaddress;            }          }        }      }    }  } catch (SocketException ex) {    System.out.println("獲取ip地址異常");    ex.printStackTrace();  }  System.out.println("IP:" + ip);  return ip;}

總結

以上所述是小編給大家介紹的Linux通過命令僅獲取IP地址的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 航空| 新密市| 安泽县| 兴城市| 池州市| 小金县| 瑞金市| 沧州市| 荣昌县| 香河县| 江华| 河津市| 上杭县| 夏津县| 长泰县| 修武县| 平山县| 台湾省| 奉新县| 新河县| 襄樊市| 恩平市| 穆棱市| 灵武市| 阳高县| 鄂托克前旗| 光山县| 邹城市| 道真| 吴川市| 孟津县| 临安市| 荣成市| 阳曲县| 临沭县| 右玉县| 确山县| 庐江县| 三穗县| 临夏市| 大港区|