From b979274a6dc8135969c609b766eecbfd7a78ac88 Mon Sep 17 00:00:00 2001 From: Brandon Rozek Date: Sat, 19 Dec 2020 20:20:31 -0500 Subject: [PATCH] New Post --- content/blog/bashvalidateip.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 content/blog/bashvalidateip.md diff --git a/content/blog/bashvalidateip.md b/content/blog/bashvalidateip.md new file mode 100644 index 0000000..cd42c17 --- /dev/null +++ b/content/blog/bashvalidateip.md @@ -0,0 +1,27 @@ +--- +title: "Quick Bash: Validate IP Address" +date: 2020-12-19T20:15:24-05:00 +draft: false +tags: ["bash"] +--- + +`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. + +```bash +if ! command -v ipcalc > /dev/null ; then + echo "ipcalc not found. Exiting..." + exit 1 +fi +``` + +Now for this example, we'll validate an IP address stored in the variable `$IP`. + +```bash +if ! ipcalc -cs "$IP" ; then + echo "Invalid IP Address" + exit 1 +fi +``` +