Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.