2012年5月27日日曜日

wget の '--spider' オプションはHEADメソッド

こちら wget –spider = Head Request : alexking.org の記事について。


自分の場合、ブックマーク(リンク集)のリンク切れをまとめてチェックしたいときにwget の '--spider'オプションを使う。このような利用方法のときは特に気にすることはないのだが、何らかのWebアプリケーションを実行したい場合には注意が必要とのこと。

というのは 'wget --spider' がHEADメソッドを実行するよう実装されているので、Webアプリのコードが実行されないことがあるらしい(サーバーの環境や設定に依存するみたいだが)。

GETでアクセスしつつ、ダウンロードされたファイルを自動的に削除したい場合には、'--delete-after'オプションを使うのがよい。

NetHogs - プロセスごとのデータ転送量をtop風に表示

既存のネットワークアプリの転送量を調べるときに使ったソフト。サーバーでこの NetHogs を起動しておいて、クライアント側でアプリのシナリオとかユースケースを1つ実行。サーバーのほうの画面で何バイト送受信したかを見て記録しておく。「なるほどこのシナリオの場合はx[KB]くらい通信するのか」と、だいたいの数字が分かる。

特徴は、転送速度(KB/sec)だけでなく、転送量の累積値が表示されるところ。たぶん初期状態では KB/sec の表示になっているが、実行中に m キーを押せば表示モードが KB/sec, KB, MB, B とサイクリックに切り替わる。

インストールに関しては、libpcap と ncurses に依存している。今回の環境(CentOS-6.0)では、epelリポジトリから yum でインストールできたので特に問題無し。


表示の例

テキトーに sshd と apache にアクセスした場合のキャプチャを貼り付けておく。

まずは Byte単位の表示。

プロセスのIDごとに、USER, PROGRAM, DEV, SENT, RECEIVED が表示される。シンプル。

それから KB/sec 単位の表示。


Windows バッチファイル FOR文での「コマンド置換」

Windowsの .bat ファイルを書いた。そして、dir等のコマンドの出力を、FOR文のパラメータとして指定する方法を知った。


.batファイルの例
拡張子 xml のファイル名を降順ソートで列挙し、後続のコマンド(ここでは単なる echo)に渡すFOR文。

@echo off
for /F "usebackq" %%file IN (`DIR /B /-N *.xml`) DO @echo %%file


Unix系のシェルで言うところのコマンド置換(command substitution)。バッククォート(`)を使う点は共通だ。


2012年3月18日日曜日

ルーティングテーブル(FIB)、ルーティングキャッシュ

LinuxではルーティングテーブルのことをFIB(Forwarding Information Base)と呼ぶ。一度参照されたら routing cache にキャッシュされる。

これらを表示するコマンドは何通りかある。

route コマンド
# route -F
Kernel IP routing table
Destination     Gateway         Genmask          Flags Metric Ref      Use Iface
172.16.78.0     *               255.255.255.0    U     0      0          0 eth0
(略)
# route -C
Kernel IP routing cache
Source          Destination     Gateway         Flags Metric Ref   Use Iface
192.168.105.1   192.168.105.130 192.168.105.130 il    0      0      29 lo
(略)
netstat コマンド
# netstat -r
Kernel IP routing table
(略)
# netstat -rC
Kernel IP routing cache
(略)
ip コマンド
# ip route show
172.168.78.0/24 dev eth0  proto kernel   scope link  src 172.16.78.130
(略)
# ip route show cached
192.168.105.128 from 192.168.105.130 dev eth2
    cache  mtu 1500 advmss 1460 hoplimit 64
(略)

Netfilter Architecture

古いけどわかりやすいドキュメントが netfilter.org の HOWTO の中にあった。

NICとプロセス、ルーティングコードのあいだにフックが5個仕掛けてあって、通過するパケットに関数が作用する。

Linux netfilter Hacking HOWTO: Netfilter Architecture

次のドキュメントも参考になる。

Linux network stack walkthrough

2012年3月4日日曜日

GPG で表示される "2048R" や "usage: SC", "usage: E" の意味

GnuPGで '--edit-key' オプションを使うと鍵の管理作業のためのメニューが出てくる。

$ gpg --edit-key 549B5813
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There in NO WARRANTY, to the extent permitted by law.

Secret key is available.

pub 2048R/549B5813  created: 2012-01-22  expires: never     usage: SC
                    trust: ultimate      validity: ultimate
sub 2048R/6878DE41  created: 2012-01-22  expires: never     usage: E
sub 2048g/60ACA1A9  created: 2012-01-22  expires: never     usage: E
sub 2048D/29641C08  created: 2012-01-22  expires: never     usage: S
(略)

ここで表示される "2048R" とか "usage: SC" の意味を理解しないでいるとまずそうだから調べてみた、という話。


2048R

これは "The GNU Privacy Handbook" に書いてあった。

The public key is displayed along with an indication of whether or not the private key is available. Information about each component of the public key is then listed. The first column indicates the type of the key. The keyword pub identifies the public master signing key, and the keyword sub identifies a public subordinate key. The second column indicates the key's bit length, type, and ID. The type is D for a DSA key, g for an encryption-only ElGamal key, and G for an ElGamal key that may be used for both encryption and signing. The creation date and expiration date are given in columns three and four. The user IDs are listed following the keys.

要するに数字の部分が鍵のビット長、残りのアルファベット1文字が鍵の種類を表す。R なら RSA、D なら DSA、g なら 暗号化専用の ElGamal、G なら 暗号化と署名両方に使える ElGamal(このハンドブックは1999年の古いものなのでRSAについて言及していないが)。

"2048g"


usage: SC とか

なんとなく "S" は Sign で "E" は Encrypt だろうと予想が付いているものの、調べてみると同じ疑問をもった人がいた。

How are the GPG usage flags defined in the key details listing? - Unix and Linux - Stack Exchange

回答によれば "S" が "for signing", "E" が "for encrypting", "C" が "Creating a certificate"(たとえば Revocation certificate: 失効証明書とか?), "A" が "authentication"とのこと(SSHやTLSにおける認証で使えるらしい)。


2012年3月1日木曜日

dd コマンドでファイルを読み書き

ddコマンドはディスクのバックアップやダンプに使われることが多いので、コピー専用のコマンドだと思っていたことがある。

実際はそうではなくて、もっと軽い用途、たとえば標準入力から読み込んだ内容をファイルに書き込んだり、ファイルを読み込んで標準出力に出力したりすることもできる。


入力した内容をファイルに出力
'of'オプションで出力先を指定しつつ ddを実行すると入力待ちになる。そこで任意の文字とEOF(Ctrl-D)を入力すれば、ファイルができる。
$ dd of=hello.txt
hello, world
0+1 records in
0+1 records out
13 bytes transferred in 2.835518 secs (5 bytes/sec)
$ cat hello.txt
hello, world
$
ファイルの内容を表示
'if'でファイルを指定するだけ。
$ dd if=hello.txt 
hello, world
0+1 records in
0+1 records out
13 bytes transferred in 0.000138 secs (94173 bytes/sec)
$
ディスクなどのブロックデバイスファイルの場合も同様に表示できる。ただしサイズが大きいので 'bs' や 'count' オプションで調節する。
$ sudo dd if=/dev/disk0 bs=5 count=1  # MacOS requires root privilege to read /dev/disk0
?1???1+0 records in
1+0 records out
5 bytes transferred in 0.000334 secs (14969 bytes/sec)
$