LoginSignup
8
5

More than 5 years have passed since last update.

ポートが開いているか確認するのに手グセで nc -vz してたら invalid option って怒られたのでいいかげん少し調べた話

Posted at

nc でやる

telnet とか curl でもいいんだけど nc が好き。

nc -vz hostname port

手グセで nc -vz というオプション付きでやってたけど invalid option って怒られるようになった。

 ❯ uname -sr; nc --version
Linux 3.10.0-123.13.2.el7.x86_64
Ncat: Version 6.40 ( http://nmap.org/ncat )

 ❯ nc -vz example.com 80
nc: invalid option -- 'z'
Ncat: Try `--help' or man(1) ncat for more information, usage options and help. QUITTING.

どうやら nmap 付属の ncat(nc) に置き換わったので、-z オプションがないようです。

ncatの派生版に関しては、こちらのサイトに詳しい。
http://www.intellilink.co.jp/article/column/security-net01.html

回避策

man をみたけど、それっぽいオプションがないのでこうする。正解がわからない。
-w でタイムアウトも指定しておく。

nc -w 1 -v hostname port < /dev/null

成功

❯ nc -w 1 -v example.com 80 < /dev/null
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Connected to 93.184.216.34:80.
Ncat: 0 bytes sent, 0 bytes received in 0.35 seconds.

成功

❯ nc -w 1 -v example.com 443 < /dev/null
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Connected to 93.184.216.34:443.
Ncat: 0 bytes sent, 0 bytes received in 0.23 seconds.

失敗

❯ nc -w 1 -v example.com 25 < /dev/null
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Connection timed out.

もう1つの方法

ググったらデバイスファイル /dev/tcp/hostname/port をつかう方法があったけど bash でしか使えないような。普段は zsh なのでドヤりたい時のために心の片隅に。

echo -n > /dev/tcp/hostname/port

参考サイト

http://www.intellilink.co.jp/article/column/security-net01.html
http://serverfault.com/questions/788934/check-if-remote-host-port-is-open-cant-use-gnu-netcat-nor-nmap-rhel-7

捗れ〜。

8
5
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
8
5