Skip to content

Instantly share code, notes, and snippets.

@Airr
Created March 8, 2019 08:07
Show Gist options
  • Save Airr/e8436d1566822475dfe5c84718466634 to your computer and use it in GitHub Desktop.
Save Airr/e8436d1566822475dfe5c84718466634 to your computer and use it in GitHub Desktop.
PublicIP
// LINUX - MACOS ONLY
package main
import (
"fmt"
"log"
"os/exec"
"strings"
)
//PublicIP - Retrieves public ip address
func PublicIP() (string, error) {
cmdName := "dig"
cmdArgs := strings.Split("+short myip.opendns.com @resolver1.opendns.com", " ")
ipAddress, err := exec.Command(cmdName, cmdArgs...).Output()
return string(ipAddress), err
}
func main() {
ip, err := PublicIP()
if err != nil {
log.Fatal(err)
}
fmt.Print(ip)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment