-ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q')
-gateway_ip=$(route -n | sed -rn 's/^0\.0\.0\.0\s+(\S+).*/\1/p')
-my_ip=$(host faiserver $gateway_ip | sed -rn 's/^\S+ has address //p;T;q')
+gateway_if=$(ip route | sed -rn 's/^default via \S+ dev (\S+) .*/\1/p')
+if [[ ! $gateway_if ]]; then
+ echo "$0: failed to find gateway interface"
+ exit 1
+fi
+# assuming ipv4, or else we might need to deal with multiple addresses
+# in an ipv4 + ipv6 network.
+network=$(ip -4 -o a show dev $gateway_if | sed -rn '/scope.*global/s/^(\S+\s+){3}(\S+)\s.*/\2/p')
+if [[ ! $network ]]; then
+ echo "$0: failed to find network"
+ exit 1
+fi
+my_ip=${network%/*}
+if [[ $host == default ]]; then
+ ip=$network
+else
+ ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q')/32
+fi
+
+
+# alternate way of getting my ip
+#gateway_ip=$(ip route | sed -rn 's/^default via (\S+) .*/\1/p')
+#my_ip=$(host faiserver $gateway_ip | sed -rn 's/^\S+ has address //p;T;q')