Last active
March 26, 2026 14:23
-
-
Save bmatthewshea/ec18b2198c49e2b0422717de5ab1c9c8 to your computer and use it in GitHub Desktop.
Show your default route, private IP and public IP from BASH using only fresh install/default commands. (DNSUTILS not needed.)
This file contains hidden or 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
| #!/bin/bash | |
| # Brady Shea - APR 2024 | |
| # IP details without any extra packages installed. | |
| # No 'dnsutils' or 'net-tools' (dig, nslookup or route) package needed for this command. | |
| # | |
| # Make sure these are in your path. | |
| # They usually reside in /usr/sbin or /usr/bin if you need to set manually: | |
| ip=$(command -v ip) | |
| host=$(command -v host) | |
| printf "Gateway:\n"; $ip -br route | grep "^default" | cut -d " " -f 3 | |
| printf "Private:\n"; $ip -br -4 -o ad | grep --invert-match "^lo" | cut -d " " -f 25 | |
| printf "Public:\n"; $host -4 -t a myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has"| cut -d " " -f 4 | |
| # Note: These^ may need fine tuning - based on 1 Ethernet interface! | |
| # If you have 'dig' you could also use this for public: | |
| # > printf "Public:\n"; dig +short myip.opendns.com @resolver1.opendns.com | |
| # (or) Quoted public: | |
| # dig TXT +short o-o.myaddr.l.google.com @ns1.google.com |
This file contains hidden or 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
| Gateway: | |
| 192.168.1.1 | |
| Private: | |
| 192.168.1.101/24 | |
| Public: | |
| 199.199.199.199 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions:
Copy the script to somewhere in your path and make it executable.
Then simply run it by typing 'myip' on the command line from any directory.