R.A. Epigonos et al.

[netcat] 簡単な HTTP サーバ

netcat/nc をインストールして使うまでの記録。

パッケージ名に netcat の含まれるパッケージを探すと以下のパッケージが見つかる。

$ apt-cache --names-only search netcat
netcat6 - TCP/IP swiss army knife with IPv6 support
netcat - TCP/IP swiss army knife -- transitional package
netcat-traditional - TCP/IP swiss army knife
netcat-openbsd - TCP/IP swiss army knife

netcat-openbsd をインストール。

# apt-get install netcat-openbsd 

ローカルループバックアドレスの8080番をリッスンするには以下のようにする。

$ nc.openbsd -vv -l 127.0.0.1 8080;
Listening on [127.0.0.1] (family 0, port 8080)

この後、リッスンするアドレスに対して適当なクライアントで接続する。以下は wget を使う例。

$ wget --quiet --debug --no-config --execute=netrc=off --output-document /dev/null --timeout 10 'http://127.0.0.1:8080/'
Setting --no-config (noconfig) to 1
Setting netrc (netrc) to off
Setting --output-document (outputdocument) to /dev/null
Setting --timeout (timeout) to 10
DEBUG output created by Wget **** on linux-gnu.

URI encoding = ‘UTF-8’
Created socket 5.
Releasing 0x00000000016d9f40 (new refcount 0).
Deleting unused 0x00000000016d9f40.

---request begin---
GET / HTTP/1.1
User-Agent: Wget/**** (linux-gnu)
Accept: */*
Host: 127.0.0.1:8080
Connection: Keep-Alive

---request end---
Closed fd 5
Closed fd 5
Releasing 0x00000000016da290 (new refcount 0).
Deleting unused 0x00000000016da290.

netcat 側の出力は以下のように変わる。このやり方だと一つの接続を処理すると終了するので、ずっとモニタしておきたいケースでは不便。

$ nc.openbsd -vv -l 127.0.0.1 8080;
Listening on [127.0.0.1] (family 0, port 8080)
Connection from [127.0.0.1] port 8080 [tcp/http-alt] accepted (family 2, sport 51175)
GET / HTTP/1.1
User-Agent: Wget/**** (linux-gnu)
Accept: */*
Host: 127.0.0.1:8080
Connection: Keep-Alive

ずっとモニタする方針の一つに何度も netcat を起動する方法がある。

$ while :; do nc.openbsd -vv -l 127.0.0.1 8080; done
Listening on [127.0.0.1] (family 0, port 8080)

この状態で wget を使って 2 回接続する。

$ wget --quiet --debug --no-config --execute=netrc=off --output-document /dev/null --timeout 10 --tries 2 'http://127.0.0.1:8080/'
Setting --no-config (noconfig) to 1
Setting netrc (netrc) to off
Setting --output-document (outputdocument) to /dev/null
Setting --timeout (timeout) to 10
Setting --tries (tries) to 2
DEBUG output created by Wget **** on linux-gnu.

URI encoding = ‘UTF-8’
Created socket 5.
Releasing 0x0000000001b53f50 (new refcount 0).
Deleting unused 0x0000000001b53f50.

---request begin---
GET / HTTP/1.1
User-Agent: Wget/**** (linux-gnu)
Accept: */*
Host: 127.0.0.1:8080
Connection: Keep-Alive

---request end---
Closed fd 5
Created socket 5.
Releasing 0x0000000001b542a0 (new refcount 0).
Deleting unused 0x0000000001b542a0.

---request begin---
GET / HTTP/1.1
User-Agent: Wget/**** (linux-gnu)
Accept: */*
Host: 127.0.0.1:8080
Connection: Keep-Alive

---request end---
Closed fd 5

netcat 側の出力は以下のように変わる。

$ while :; do nc.openbsd -vv -l 127.0.0.1 8080; done
Listening on [127.0.0.1] (family 0, port 8080)
Connection from [127.0.0.1] port 8080 [tcp/http-alt] accepted (family 2, sport 51204)
GET / HTTP/1.1
User-Agent: Wget/**** (linux-gnu)
Accept: */*
Host: 127.0.0.1:8080
Connection: Keep-Alive

Listening on [127.0.0.1] (family 0, port 8080)
Connection from [127.0.0.1] port 8080 [tcp/http-alt] accepted (family 2, sport 51205)
GET / HTTP/1.1
User-Agent: Wget/**** (linux-gnu)
Accept: */*
Host: 127.0.0.1:8080
Connection: Keep-Alive

Listening on [127.0.0.1] (family 0, port 8080)

netcat を起動しっぱなしにして、複数の接続を処理させることも可能。これを行うには以下のように -k オプションを使う。

$ nc.openbsd -vv -k -l 127.0.0.1 8080;
Listening on [127.0.0.1] (family 0, port 8080)

同様に wget で 2 回接続する。

$ wget --quiet --debug --no-config --execute=netrc=off --output-document /dev/null --timeout 10 --tries 2 'http://127.0.0.1:8080/'
Setting --no-config (noconfig) to 1
Setting netrc (netrc) to off
Setting --output-document (outputdocument) to /dev/null
Setting --timeout (timeout) to 10
Setting --tries (tries) to 2
DEBUG output created by Wget **** on linux-gnu.

URI encoding = ‘UTF-8’
Created socket 5.
Releasing 0x0000000000b4ff50 (new refcount 0).
Deleting unused 0x0000000000b4ff50.

---request begin---
GET / HTTP/1.1
User-Agent: Wget/**** (linux-gnu)
Accept: */*
Host: 127.0.0.1:8080
Connection: Keep-Alive

---request end---
Closed fd 5
Created socket 5.
Releasing 0x0000000000b502a0 (new refcount 0).
Deleting unused 0x0000000000b502a0.

---request begin---
GET / HTTP/1.1
User-Agent: Wget/**** (linux-gnu)
Accept: */*
Host: 127.0.0.1:8080
Connection: Keep-Alive

---request end---
Closed fd 5

netcat 側の出力は以下のように変わる。

$ nc.openbsd -vv -k -l 127.0.0.1 8080;
Listening on [127.0.0.1] (family 0, port 8080)
Connection from [127.0.0.1] port 8080 [tcp/http-alt] accepted (family 2, sport 51210)
GET / HTTP/1.1
User-Agent: Wget/**** (linux-gnu)
Accept: */*
Host: 127.0.0.1:8080
Connection: Keep-Alive

Connection closed, listening again.
Connection from [127.0.0.1] port 8080 [tcp/http-alt] accepted (family 2, sport 51211)
GET / HTTP/1.1
User-Agent: Wget/**** (linux-gnu)
Accept: */*
Host: 127.0.0.1:8080
Connection: Keep-Alive

Connection closed, listening again.

リファレンス

  1. Debian -- sid の netcat パッケージに関する詳細
  2. Debian -- sid の netcat-openbsd パッケージに関する詳細
  3. Debian -- sid の netcat-traditional パッケージに関する詳細

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2008-01-24T16:51:11+09:00
  2. Modified: 2008-01-24T16:51:11+09:00
  3. Generated: 2023-08-27T23:09:16+09:00