Skip to content

Instantly share code, notes, and snippets.

@hmmbug
Created July 15, 2014 03:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmmbug/e386274e299ab4d00a9f to your computer and use it in GitHub Desktop.
Save hmmbug/e386274e299ab4d00a9f to your computer and use it in GitHub Desktop.
bash - calculate network address from IP address & netmask
#!/bin/bash
IP_ADDR=$(hostname -i)
IFS=. read -r io1 io2 io3 io4 <<< "$IP_ADDR"
IFS=. read -r mo1 mo2 mo3 mo4 < <(ifconfig -a | sed -n "/inet addr:$IP_ADDR /{ s/.*Mask://;p; }")
NET_ADDR="$((io1 & mo1)).$(($io2 & mo2)).$((io3 & mo3)).$((io4 & mo4))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment