如何仅通过脚本的IP地址和掩码获取所有网络接口?的Linux

时间:2018-11-03 12:31:49

标签: bash shell networking ubuntu-16.04 ifconfig

ifconfig显示太多信息。我需要知道所有网络接口,无论它们是否可用,并将它们与IP地址和掩码配对。

如果ficonfig返回

from django.db import models
from oauth2_provider.models import AbstractApplication

class CustomApplication(AbstractApplication):
    application_foo = models.CharField(max_length=100, blank=True)

示例输出:

enp9s0    Link encap:Ethernet  HWaddr 54:be:f7:5c:99:0d  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:18 

lo        Link encap:Локальная петля (Loopback)  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:256 errors:0 dropped:0 overruns:0 frame:0
          TX packets:256 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:19992 (19.9 KB)  TX bytes:19992 (19.9 KB)

wlp8s0    Link encap:Ethernet  HWaddr f8:2f:a8:f3:1b:31  
          inet addr:192.168.0.102  Bcast:192.168.0.255 Mask:255.255.255.0
          inet6 addr: fe80::820d:4dd0:b3f9:10ed/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:241 errors:0 dropped:0 overruns:0 frame:4220
          TX packets:301 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:101434 (101.4 KB)  TX bytes:101132 (101.1 KB)
          Interrupt:17

2 个答案:

答案 0 :(得分:1)

ifconfig | awk '/^[^ ]/{i=$1; getline; if($1=="inet") {print i": "$2,$NF} else {print i": addr:- Mask:-"}}'

输出:

enp9s0: addr:- Mask:-
lo: addr:127.0.0.1 Mask:255.0.0.0
wlp8s0: addr:192.168.0.102 Mask:255.255.255.0

顺便说一句:在Ubuntu中,ifconfig已过时,并被iproute2取代。

答案 1 :(得分:0)

only ip:
ip a|grep -v inet6|grep inet|awk '{print $2}'|sed -e 's/\// /g' |awk '{print $1}'
ip with mask:
ip a|grep -v inet6|grep inet|awk '{print $2}'