DEV Community

Brandon Rozek
Brandon Rozek

Posted on • Originally published at brandonrozek.com on

Quick Bash: Validate IP Address

ipcalc is a terminal tool that lets you validate an IP address. This proves useful to me as I have scripts that automate certain remote tasks given an IP address. Instead of trusting that an argument passed is a valid IP, why not check it?

First the script would need to check if ipcalc exists.

if ! command -v ipcalc > /dev/null ; then
    echo "ipcalc not found. Exiting..."
    exit 1
fi

Enter fullscreen mode Exit fullscreen mode

Now for this example, we’ll validate an IP address stored in the variable $IP.

if ! ipcalc -cs "$IP" ; then
    echo "Invalid IP Address"
    exit 1
fi

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay