#!/bin/bash

FBIP=192.168.178.1

NETCAT=`which netcat`
[ -z "$NETCAT" ] && NETCAT=`which nc`
[ -z "$NETCAT" ] && exit 1

INTERFACE_NS="urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1"
WANIP_NS="urn:schemas-upnp-org:service:WANIPConnection:1"

NS="$WANIP_NS"
REQUEST="GetExternalIPAddress"
SED='/^<NewExternalIP/ s,</\?NewExternalIPAddress>,,gp'

BODY="<?xml version=\"1.0\" encoding=\"utf-8\"?>
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
        <s:Body><u:$REQUEST xmlns:u=$NS /></s:Body>
</s:Envelope>
"

LENGTH=`echo -n "$BODY" | wc -c`

( $NETCAT $FBIP 49000 | sed -ne "$SED" ) <<EOF
POST /upnp/control/WANCommonIFC1 HTTP/1.1
Content-Type: text/xml; charset="utf-8"
Connection: close
HOST: $FBIP:49000
Content-Length: $LENGTH
SOAPACTION: "$NS#$REQUEST"

$BODY
EOF

