UPnP/SSDP を使用してルーターから WAN IP を取得する方法

UPnP/SSDP を使用してルーターから WAN IP を取得する方法

UPnP/SSDP を使用してルーターに WAN IP を報告させる方法を探していますが、これまでのところ、ネットワーク上の UPnP をサポートするインターネット アクセス デバイスのリストを取得することすらできませんでした。送信するリクエストは次のとおりです。

$ cat request.txt
M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1900
MAN: "ssdp:discover"
MX: 3
ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1

次のコマンドを使用します:

$ nc -uvv 239.255.255.250 1900 < request.txt
Connection to 239.255.255.250 1900 port [udp/ssdp] succeeded!

接続が成功したというメッセージが表示されるだけで、その後は何も起こりません...

どなたかアドバイスをいただけませんか(メモリアドレス(お願いします!)私が何を間違っているのか教えてください。

アップデート:さて、応答を取得するために使用してみましたtcpdump。次のような結果が返ってきました:

$ sudo tcpdump -vv -A -s 0 -i en1 udp port 1900 and host 239.255.255.250 
NOTIFY * HTTP/1.1
Host: 239.255.255.250:1900
Cache-Control: max-age=60
Location: http://192.168.1.1:1780/InternetGatewayDevice.xml
NTS: ssdp:alive
Server: POSIX, UPnP/1.0 linux/5.100.104.2 
NT: urn:schemas-upnp-org:device:InternetGatewayDevice:1

次のステップは、tcpdump の出力を解析し、ヘッダーを含まないすべての応答をフィルタリングしてNT: urn:schemas-upnp-org:device:InternetGatewayDevice:1から、ルーターに実際の SOAP 要求を送信することだと思います。

答え1

tcpdump返信を表示するには、または同様のものを使用する必要があります。

ncリクエストを送信したエンドポイントからの応答を探しています。ただし、リクエストを送信したエンドポイントは、汎用ブロードキャスト ターゲットです。応答は、汎用ブロードキャスト ターゲットからではなく、応答した特定のデバイスから送信されます。

の出力が示すようにnc、ブロードキャスト ターゲットに接続されています。そのため、応答デバイスからの応答は表示されません。

答え2

このタスクはかなり古いものですが、私が探していた答えを自分で投稿します。

UPnP 応答で場所が投稿されました "http://192.168.1.1:1780/インターネットゲートウェイデバイス.xml

そこで、データ形式に関する詳細情報を取得できます。私のルーターの場合: xml へのパスがさらにあります。それらを呼び出すと、UPnP 情報がさらにあります。結果は次のとおりです: 呼び出しました:

POST /upnp/control?WANIPConnection HTTP/1.1
Host: 192.168.1.1
SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"
Accept-Language: de-de;q=1, de;q=0.5
Accept-Encoding: gzip
Content-Type: text/xml; charset="utf-8"
User-Agent: gupnp-universal-cp GUPnP/0.20.10 DLNADOC/1.50
Connection: Keep-Alive
Content-Length: 281

<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:GetExternalIPAddress></s:Body></s:Envelope>

そして答えは次のようになります:

HTTP/1.1 200 OK
EXT:
Content-Type: text/xml; charset="utf-8"
Date: Tue, 04 Aug 2015 23:55:01 GMT
Server: servername/2.0 UPnP/1.0 UPnP-Device-Host/1.0
Content-Length: 380

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <s:Body>
        <u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
            <NewExternalIPAddress>123.123.123.123</NewExternalIPAddress>
        </u:GetExternalIPAddressResponse>
    </s:Body>
</s:Envelope>

「UPnP Inspector」に大いに助けられました

答え3

使用できますsocat

$ socat -T1 STDIO UDP4-DATAGRAM:239.255.255.250:1900 < request.txt

答え4

完全に動作する BASH スクリプト:

#!/usr/bin/env bash
function wan_ip_connection() {
    local host=$1
    result=$( curl -s "http://$host/upnp/control?WANIPConnection" \
       -H 'SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"' \
       --data-binary '<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:GetExternalIPAddress></s:Body></s:Envelope>'
    )

    REGEX='ExternalIPAddress>([0-9.]+)<'
    if [[ $result =~ $REGEX ]]
    then
       echo "${BASH_REMATCH[@]:1}"
    fi
}

function initial_query() {
   echo -e 'M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: "ssdp:discover"\r\nMX: 3\r\nST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n\r\n' |
       socat STDIO UDP4-DATAGRAM:239.255.255.250:1900
}

function main() {
   location=$( initial_query | grep LOCATION )
   location=${location%/*}
   location=${location##*/}
   ip=$( wan_ip_connection "$location" )
   echo $ip
}

main

関連情報