如何格式化 WHOIS 响应

How to format WHOIS response

我需要实施一个 "whois" 服务,该服务将监听 whois 查询 (whois -h ) 并将响应来自 phpipam API 的信息。我正在使用启动脚本的 xinetd 监听端口 43。 我的问题是,当我从服务器发送响应时,服务器在第一行换行后重置连接。出于测试目的,我只是尝试发送两条回声线,结果是一样的。

脚本:

#!/bin/bash
echo "One"
echo "Two"

lsof:

# lsof -i | grep whois
xinetd    17696       root    6u  IPv4  91809      0t0  TCP *:whois (LISTEN)

whois 查询(来自客户端):

# whois -h 192.168.4.129 44.44.44.44
One
fgets: Connection reset by peer

tcpdump:

# tcpdump -n -i any -X port 43

16:34:01.162440 IP 192.168.4.128.49374 > 192.168.4.129.nicname: Flags [S],     seq 1949234703, win 29200, options [mss 1460,sackOK,TS val 337480796 ecr 0,nop,wscale 7], length 0
    0x0000:  4500 003c 7a53 4000 4006 3617 c0a8 0480  E..<zS@.@.6.....
    0x0010:  c0a8 0481 c0de 002b 742e f60f 0000 0000  .......+t.......
    0x0020:  a002 7210 8a80 0000 0204 05b4 0402 080a  ..r.............
    0x0030:  141d 8c5c 0000 0000 0103 0307 0000 0000  ...\............
    0x0040:  0000 0000 0000 0000 0000 0000            ............
16:34:01.162916 IP 192.168.4.129.nicname > 192.168.4.128.49374: Flags [S.], seq 3654394026, ack 1949234704, win 28960, options [mss 1460,sackOK,TS val 25631110 ecr 337480796,nop,wscale 7], length 0
    0x0000:  4500 003c 0000 4000 4006 b06a c0a8 0481  E..<..@.@..j....
    0x0010:  c0a8 0480 002b c0de d9d1 a0aa 742e f610  .....+......t...
    0x0020:  a012 7120 eb31 0000 0204 05b4 0402 080a  ..q..1..........
    0x0030:  0187 1986 141d 8c5c 0103 0307 0000 0000  .......\........
    0x0040:  0000 0000 0000 0000 0000 0000            ............
16:34:01.162946 IP 192.168.4.128.49374 > 192.168.4.129.nicname: Flags [.], ack 1, win 229, options [nop,nop,TS val 337480797 ecr 25631110], length 0
    0x0000:  4500 0034 7a54 4000 4006 361e c0a8 0480  E..4zT@.@.6.....
    0x0010:  c0a8 0481 c0de 002b 742e f610 d9d1 a0ab  .......+t.......
    0x0020:  8010 00e5 8a78 0000 0101 080a 141d 8c5d  .....x.........]
    0x0030:  0187 1986 0000 0000 0000 0000 0000 0000  ................
    0x0040:  0000 0000                                ....
16:34:01.163348 IP 192.168.4.128.49374 > 192.168.4.129.nicname: Flags [P.], seq 1:14, ack 1, win 229, options [nop,nop,TS val 337480798 ecr 25631110], length 13
    0x0000:  4500 0041 7a55 4000 4006 3610 c0a8 0480  E..AzU@.@.6.....
    0x0010:  c0a8 0481 c0de 002b 742e f610 d9d1 a0ab  .......+t.......
    0x0020:  8018 00e5 8a85 0000 0101 080a 141d 8c5e  ...............^
    0x0030:  0187 1986 3434 2e34 342e 3434 2e34 340d  ....44.44.44.44.
    0x0040:  0a00 0000 0000 0000 0000 0000 0000 0000  ................
    0x0050:  00                                       .
16:34:01.164122 IP 192.168.4.129.nicname > 192.168.4.128.49374: Flags [.], ack 14, win 227, options [nop,nop,TS val 25631110 ecr 337480798], length 0
    0x0000:  4500 0034 b92d 4000 4006 f744 c0a8 0481  E..4.-@.@..D....
    0x0010:  c0a8 0480 002b c0de d9d1 a0ab 742e f61d  .....+......t...
    0x0020:  8010 00e3 8a2c 0000 0101 080a 0187 1986  .....,..........
    0x0030:  141d 8c5e 0000 0000 0000 0000 0000 0000  ...^............
    0x0040:  0000 0000                                ....
16:34:01.165523 IP 192.168.4.129.nicname > 192.168.4.128.49374: Flags [P.], seq 1:5, ack 14, win 227, options [nop,nop,TS val 25631110 ecr 337480798], length 4
    0x0000:  4500 0038 b92e 4000 4006 f73f c0a8 0481  E..8..@.@..?....
    0x0010:  c0a8 0480 002b c0de d9d1 a0ab 742e f61d  .....+......t...
    0x0020:  8018 00e3 d5a7 0000 0101 080a 0187 1986  ................
    0x0030:  141d 8c5e 4f6e 650a 0000 0000 0000 0000  ...^One.........
    0x0040:  0000 0000 0000 0000                      ........
16:34:01.165844 IP 192.168.4.128.49374 > 192.168.4.129.nicname: Flags [.], ack 5, win 229, options [nop,nop,TS val 337480800 ecr 25631110], length 0
    0x0000:  4500 0034 7a56 4000 4006 361c c0a8 0480  E..4zV@.@.6.....
    0x0010:  c0a8 0481 c0de 002b 742e f61d d9d1 a0af  .......+t.......
    0x0020:  8010 00e5 8a78 0000 0101 080a 141d 8c60  .....x.........`
    0x0030:  0187 1986 0000 0000 0000 0000 0000 0000  ................
    0x0040:  0000 0000                                ....
16:34:01.165914 IP 192.168.4.129.nicname > 192.168.4.128.49374: Flags [R.], seq 5, ack 14, win 227, options [nop,nop,TS val 25631111 ecr 337480798], length 0
    0x0000:  4500 0034 b92f 4000 4006 f742 c0a8 0481  E..4./@.@..B....
    0x0010:  c0a8 0480 002b c0de d9d1 a0af 742e f61d  .....+......t...
    0x0020:  8014 00e3 8a23 0000 0101 080a 0187 1987  .....#..........
    0x0030:  141d 8c5e 0000 0000 0000 0000 0000 0000  ...^............
    0x0040:  0000 0000                                ....
16:34:01.166350 IP 192.168.4.129.nicname > 192.168.4.128.49374: Flags [R], seq 3654394031, win 0, length 0
    0x0000:  4500 0028 826a 4000 4006 2e14 c0a8 0481  E..(.j@.@.......
    0x0010:  c0a8 0480 002b c0de d9d1 a0af 0000 0000  .....+..........
    0x0020:  5004 0000 ea03 0000 0000 0000 0000 0000  P...............
    0x0030:  0000 0000 0000 0000 0000 0000 0000       ..............

如果我尝试使用 telnet 连接到 43,一切正常:

# telnet 192.168.4.129 43
Trying 192.168.4.129...
Connected to 192.168.4.129.
Escape character is '^]'.
One
Two
Connection closed by foreign host.

更新:

像这样循环工作正常

while read line; do
    echo "One"
    echo "Two"
    break
done

但我仍然无法理解为什么仅通过两条回声线就无法正常工作,或者即使我尝试 运行 带有计数器的 while 循环:

#!/bin/bash
COUNT=0
while [  $COUNT -lt 5 ]; do
    echo The counter is $COUNT
    let COUNT=COUNT+1
done

它在第一个循环后停止:

# whois -h 192.168.4.129 44.44.44.44
The counter is 0
fgets: Connection reset by peer

也许你有什么想法,这两个循环之间的reason/difference是什么?

我的/etc/xinetd.d/whoistest:

service whois
{
    port            = 43
    flags           = REUSE
    socket_type     = stream
    protocol        = tcp
    user            = root
    wait            = no
    type            = UNLISTED
    server          = /path/to/shell/script
    disable         = no
    log_type        = SYSLOG authpriv
    log_on_failure  += USERID
}

如有任何支持,我们将不胜感激。

您需要确保您的 xinetd 配置正确。您的脚本还需要处理来自 whois 客户端的输入。这有效:

#!/bin/bash
while read line; do
    echo $line
    echo "foo"
    break
done

这会读取您的台词,回显它,然后再做一些事情。随意用 echo 替换 echo $line 任何你想要的。

我必须像这样配置 xinetd 才能工作:

service whois
 {
     port = 43
     flags = REUSE
     socket_type = stream
     protocol = tcp
     user = root
     wait = no
     type = UNLISTED
     server = /tmp/w.sh
     disable = no
 }

希望这对您有所帮助。如果您的 xinetd 服务配置错误,它似乎只是断开连接。如果您的服务器实际上没有监听来自客户端的输入,您将因对等错误而重置连接。

汉奴