2009-09-12

UPnPをLinuxで利用(idgctlを使う)

グローバルIPアドレスを取得する場合、直接ルータから取得する方法、外部のサーバから取得する方法等あるが、一番手っ取り早いのはUPnPを使う方法だ。

Windowsだとフリーソフトがいくつかあるが、機能が限られていたり、使いにくかったりする。

・WindowsのUPnPソフト(ポート解放)
UPnPCJ
UPnP ポート開放ツール
UPnP Port Mapper


Linuxの場合、perlのUPnPモジュールのigdctl.plが便利。

idgctlのダウンロードはココから。

■インストール(SheevaPlugの場合)
YAMLが必要。SeevaPlugの場合makeもデフォルトだとない。
apt-get -y install make
cpan -i YAML
cpan -i Net::UPnP::ControlPoint


■使い方
USAGE
./igdctl.pl [-h|-p|-r|-a|-g|-R|-l|-c|--enable|--disable]
[-d DEVICE] [-E IP] [-e PORT] [-I IP] [-i PORT]
[-P PROTOCOL] [-D DURATION] [-A ACTIVE]

Example:
./igdctl.pl -r

Actions:
-h, --help : Displays this help text.
-v, --verbose : Verbose mode.
-p, --print : Prints connection information avaleble.
--enable : Enable internet access if supported.
--disable : Disable internet access if supported.
-r, --reconnect : Triggers a reconnect.
-a, --add-port : Adds or overwrites a port mapping entry with the
same internal client address.
-e, -I, -i, -P are needed, -E, -D, -A are
optional.
-g, --get-port : Gets a port mapping entry by remote host,
port and protocol. -e, -P are needed, -E is
optional.
-R, --remove-port : Removes a port mapping entry.
-e, -P are needed, -E is optional.
-l, --list-ports : Lists all port mapping entries. If -I was
specified, only entries by a given IP are shown.
-c, --clear-ports : Removes all port mapping entries. If -I was
specified, only entries by a given IP are
removed.

Options:
-d, --device=DEV : specifies the device number when more then one
devices are avaleble.
-E, --external-ip=IP : specifies a remote host. Wildcards are supported.
-e, --external-port=PORT : specifies an external port number.
-I, --internal-ip=IP : specifies a client ip address.
-i, --internal-port=PORT : specifies a client port number.
-P, --protocol=PROTOCOL : specifies a protocol. TCP and UDP are allowed.
-D, --duration=DURATION : specifies a number of seconds until a port mapping
entry expires.
-A, --active=ACTIVE : Specifies whether a port mapping entry is enabled.
Values 0 and 1 are allowed.


以下のように表示される。

root@debian:~/upnp# ./igdctl.pl -l
ACTIVE REMOTE HOST EXTERNAL PORT CLIENT HOST INTERNAL PORT LEASE TIME
1 * 33*** 192.168.1.** 13*** 0


・リンク
Geekなページ:perlネットワークプログラミング

4 comments:

Anonymous said...

Trying to add a port mapping entry ...
Operation failed. ERR 500
ってなるんですけど、500番てなんなんでしょうか??

yo-tei said...

コマンドは何を使われたのでしょうか?ポートマッピングエントリが0ってことはないですよね。
とりあえず./igdctl.pl -pが使えるか確認してみてはいかがでしょうか?

Anonymous said...

コマンドは
./igdctl.pl -v -a -e 38002 -I 192.168.100.50 -i 38002 -P TCP
です。
Scanning for devices ...
Found 1 device.
Trying to add a port mapping entry ...
Operation failed. (ERR 500)だそうです。

./igdctl.pl -pは
total bytes sent 10000.00Bytes
total packets sent 0
recievedも同じ。
でした。
./igdctl.pl -lとかもちゃんと表示されるし、windowsでUPNPCJ使うとちゃんと開くのでルーターは問題ない気がするんですけど。。

yo-tei said...

ちょっとソースを調べて見ましたが、durationを入れないとエラーになるようですよ。
↓な感じで通ると思います♪
./igdctl.pl -v -a -e 38002 -I 192.168.100.50 -i 38002 -P TCP -D 1
root@debian:~/upnp# ./igdctl.pl -v -a -e 38002 -I 192.168.100.50 -i 38002 -P TCP -D 1

++
Scanning for devices ...
Found 1 device.

Trying to add a port mapping entry ...
Done (OK 200)

Command successful.
++