LoginSignup
78
69

More than 5 years have passed since last update.

UDPの疎通確認はtracerouteよりncが便利

Last updated at Posted at 2017-02-23

概要

「UDP 疎通確認」でググると、tracerouteを使った方法が最上位でヒットする。

$ traceroute -U -p <port> <hostname>
traceroute to hostname (xxx.xxx.xxx.xxx), 30 hops max, 60 byte packets
 1  * * *
 2  * * *
 3  * * *
 4  * * *
 5  * * *

しかし、tracerouteは途中の経路に依存するため、場合によっては上記のように延々と米印を見続ける羽目になる。

単純に目的のサーバ(の特定のポート)に疎通できているか知りたい場合は、ncを使ったほうが便利だ。

クライアント

下記のコマンドは、hostnameportへUDPで接続する。

$ nc -u <hostname> <port>

-uでUDPを指定している。何もつけなければTCPになる。

このままでは、接続だけしてデータは送られていないので、適当に打ち込んでEnterキーを押そう。

$ nc -u example.com 10000
Hello, World!

これで相手に"Hello, World!"が送られた。

送信元IPアドレス、送信元ポートを指定する

さらに発展的な使い方として、送信元IPアドレス、送信元ポート番号を指定できる。
このとき指定する送信元IPアドレスは、自身がすでに持っているIPアドレスに限定される。

$ nc -s <送信元IP> -p <送信元ポート> <hostname> <宛先ポート>

サーバ

ncならサーバをたてるのも一瞬だ。下記のコマンドは、自分のport(UDP)を待ち受ける。

$ nc -lu <port>

-uでUDPを指定して、何もつけなければTCPになるのは、クライアントのときと同じだ。-lはListenを示す。

サーバをたてた後、先ほどのクライアントの例のように"Hello, World!"を送ると下記の結果になる。

$ nc -lu 10000
Hello, World!

78
69
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
78
69