Skip to content

Instantly share code, notes, and snippets.

@Avaray
Last active January 15, 2024 12:53
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 Avaray/b0bd37cf1b78b30f535476b3d0da62e1 to your computer and use it in GitHub Desktop.
Save Avaray/b0bd37cf1b78b30f535476b3d0da62e1 to your computer and use it in GitHub Desktop.
[BASH] Get public IP address
#!/bin/bash
S1='whatismyip.akamai.com'
S2='icanhazip.com'
S3='ifconfig.me'
IP=$(curl -sk $S1)
IP_regex='^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
if [[ ! $IP =~ $IP_regex ]]; then
IP=$(curl -sk $S2)
if [[ ! $IP =~ $IP_regex ]]; then
IP=$(curl -sk $S3)
fi
fi
if [[ $IP =~ $IP_regex ]]; then
echo $IP | tr -d '[:space:]'
fi
@Avaray
Copy link
Author

Avaray commented Dec 20, 2022

You can also use my GIP, a CLI tool for NodeJS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment