netstat

了解netstat -rn的程序


更新記錄

item note
20160811 第一版

目錄


netstat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Usage: netstat [-laentuwxrWp]

Display networking information

Options:
-l Display listening server sockets
-a Display all sockets (default: connected)
-e Display other/more information
-n Don't resolve names
-t Tcp sockets
-u Udp sockets
-w Raw sockets
-x Unix sockets
-r Display routing table
-W Display with no column truncation
-p Display PID/Program name for sockets

netstat -rn

  • netstat -rn 及 route -rn 兩個都是相同的結果
  • 直接解析/proc/net/route

/proc/net/route

Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
eth0 00000000 0116A8C0 0003 0 0 0 00000000 0 0 0
eth0 0016A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
  • Flags
    • An indicator of a number of route attributes
    • include/linux/route.h
1
2
3
4
5
6
#define RTF_UP          0x0001          // route usable
#define RTF_GATEWAY 0x0002 // destination is a gateway
#define RTF_HOST 0x0004 // host entry (net otherwise)
#define RTF_REINSTATE 0x0008 // reinstate route after tmout
#define RTF_DYNAMIC 0x0010 // created dyn. (by redirect)
#define RTF_MODIFIED 0x0020 // modified dyn. (by redirect)
  • netstat -rn

  • Kernel IP routing table

Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.22.1 0.0.0.0 UG 0 0 0 eth0
192.168.22.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
  • Gateway
    如果顯示 0.0.0.0 表示該路由是直接由本機傳送,亦即可以透過區域網路的 MAC 直接傳訊

  • Commonly Seen Routing Table Flags

    • U
      The route is active (up).
    • H
      The route destination is a single host.
    • G
      Send anything for this destination on to this gateway, which will figure out from there where to send it.
      需要透過外部的主機 (gateway) 來轉遞封包

參考來源