Skip to content

Instantly share code, notes, and snippets.

@yurenchen000
Last active April 17, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurenchen000/01270fa66fd45b6746271422e064aee1 to your computer and use it in GitHub Desktop.
Save yurenchen000/01270fa66fd45b6746271422e064aee1 to your computer and use it in GitHub Desktop.
IGD/UPNP client Get ExternalIPAddress

upnp get ext ip

IGD/UPNP get ExternalIPAddress of upper router

A. upnpc cmd

$ 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

B. shell implement

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>

C. the capture

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment