
我正在嘗試找到一種方法讓我的路由器使用 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/InternetGatewayDevice.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