Last active
December 20, 2017 14:13
-
-
Save darren/9a0f25082d4a880a3090b73488bb1f93 to your computer and use it in GitHub Desktop.
Get external IP address (nat-pmp)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/jackpal/gateway" | |
natpmp "github.com/jackpal/go-nat-pmp" | |
) | |
func main() { | |
gatewayIP, err := gateway.DiscoverGateway() | |
if err != nil { | |
log.Fatal(err) | |
} | |
client := natpmp.NewClient(gatewayIP) | |
response, err := client.GetExternalAddress() | |
if err != nil { | |
log.Fatal(err) | |
} | |
ip := response.ExternalIPAddress | |
fmt.Printf("%d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment