IGD/UPNP get ExternalIPAddress of upper router
$ upnpc -s | grep ExternalIPAddress
ExternalIPAddress = 192.168.199.121
$ upnpc -s | awk '/^ExternalIPAddress/{print $3}'
192.168.199.121
ref: https://superuser.com/a/637116/449837
capture communication & shell implement without upnp client.
get_ext_ip(){
local host=$1
local hdr='SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"'
local dat='<?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>'
local pipe='sed -ne s/.*\?<NewExternalIPAddress>\([0-9.]\+\)<.*/\1/p'
[ "$2" == "-v" ] && pipe='cat'
curl -s "$host:5000/ctl/IPConn" -H "$hdr" -d "$dat" | $pipe
}
usage:
# 10.1.2.1 is gateway ip
$ get_ext_ip 10.1.2.1
192.168.199.121
$ get_ext_ip 10.1.2.1 -v
<?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>192.168.199.121</NewExternalIPAddress></u:GetExternalIPAddressResponse></s:Body></s:Envelope>
HTTP request:
POST /ctl/IPConn HTTP/1.1
Host: 10.1.2.1:5000
User-Agent: Debian/jessie/sid, UPnP/1.0, MiniUPnPc/1.6
Content-Length: 285
Content-Type: text/xml
SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"
Connection: Close
Cache-Control: no-cache
Pragma: no-cache
<?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 response:
HTTP/1.1 200 OK
Content-Type: text/xml; charset="utf-8"
Connection: close
Content-Length: 361
Server: HiWiFi/HiWiFi/T1.0 UPnP/1.1 MiniUPnPd/1.8
Ext:
<?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>192.168.199.121</NewExternalIPAddress></u:GetExternalIPAddressResponse></s:Body></s:Envelope>