LoginSignup
1
0

More than 3 years have passed since last update.

netstat

Last updated at Posted at 2021-03-11

(よく忘れるのでメモ)
ポートの使用状況を確認する。

# netstat -na
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN
tcp        0      0 172.xxx.xxx.xxx:22          xxx.xxx.xx.xxx:50533        ESTABLISHED

「State」の意味について

ESTABLISHED・・・接続が確立したソケット
SYN_SENT・・・接続を試みているソケット
SYN_RECV・・・接続要求を受信しているソケット
FIN_WAIT1・・・切断中のソケット
FIN_WAIT2・・・リモートからの切断を待っているソケット
TIME_WAIT・・・リモートからの切断が再送されるのを待っているソケット
CLOSED・・・未使用なソケット
CLOSE_WAIT・・・リモート側から切断され,ソケットがクローズされるのを待っているソケット
LAST_ACK・・・すでに切断されており,ソケットもクローズされているソケット
CLOSING・・・ソケットが切断されているか,すべてのデータが転送されていないソケット
UNKNOWN・・・状態が不明なソケット

(参考)
【 netstat 】 ネットワーク関連の統計情報を表示する

ルーティングテーブルを確認

-n をつけると、ホスト名を名前解決せず、IPアドレスのままで表示する。
Destination が宛先に指定されている場合に Gateway に向かって転送する。

# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.31.0.1      0.0.0.0         UG        0 0          0 eth0
169.254.169.254 0.0.0.0         255.255.255.255 UH        0 0          0 eth0
172.31.0.0      0.0.0.0         255.255.240.0   U         0 0          0 eth0

-r のみだと、ホスト名を名前解決して表示させる。

# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         ip-172-31-0-1.a 0.0.0.0         UG        0 0          0 eth0
instance-data.a *               255.255.255.255 UH        0 0          0 eth0
172.31.0.0      *               255.255.240.0   U         0 0          0 eth0

netstatで出てきたIPアドレスをカウントする

ステータスがLAST_ACKなIPアドレスだけ抽出したい。

LAST_ACKとなっている部分だけ抽出
$ netstat -na | grep LAST > mmm.txt

IPアドレス部分だけ抽出
$ cat mmm | awk '{print $5}' > mmm1.txt

ポート番号だけ消しつつ、数のカウントもする
$ cut -d . -f1,2,3,4 mmm1.txt | sort | uniq -c

      2 xxx.52.93.136
      2 xxx.55.186.49
      2 xxx.55.47.205
      4 xxx.55.92.135
      →こんな風に出る
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0