Last active
August 21, 2019 11:01
-
-
Save thesp0nge/72c91cd96e4916914548f329fd787a0c to your computer and use it in GitHub Desktop.
A useless script to calculate the network address with the CIDR /xx notation in pure bash.
This file contains 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 | |
# | |
# A useless script to calculate the network address with the CIDR /xx notation | |
# in pure bash. | |
# | |
# (c) 2019 - thesp0nge - paolo@armoredcode.com | |
# | |
# I started from this gist I found on Github: | |
# https://gist.github.com/hmmbug/e386274e299ab4d00a9f. The hostname -i command | |
# doesn't work for a machine resolvng as localhost, i.e. a virtualbox machine. | |
# We must consider also hosts with more than a network card... so the interface | |
# must be a variable parameter. | |
# | |
# Then I used this IPprefix_by_netmask implementation in bash I found on | |
# StackOverflow: | |
# https://stackoverflow.com/questions/50413579/bash-convert-netmask-in-cidr-notation | |
IPprefix_by_netmask () { | |
c=0 x=0$( printf '%o' ${1//./ } ) | |
while [ $x -gt 0 ]; do | |
let c+=$((x%2)) 'x>>=1' | |
done | |
echo /$c ; } | |
IPconfig_to_netaddr () { | |
line=`ifconfig -a $net_i | grep netmask | tr -s " "` | |
ip=`echo $line | cut -f 2 -d " "` | |
mask=`echo $line | cut -f 4 -d " "` | |
IFS=. read -r io1 io2 io3 io4 <<< $ip | |
IFS=. read -r mo1 mo2 mo3 mo4 <<< $mask | |
NET_ADDR="$((io1 & mo1)).$(($io2 & mo2)).$((io3 & mo3)).$((io4 & mo4))" | |
echo $NET_ADDR`IPprefix_by_netmask $mask` ; } | |
if [ "$#" -ne 1 ]; then | |
echo "usage: $0 network interface" | |
exit 2 | |
fi | |
net_i=$1 | |
found=`ifconfig -a $net_i 2> /dev/null` | |
if [ $? -eq 1 ]; then | |
echo $0: $net_i interface not found | |
exit 1 | |
fi | |
IPconfig_to_netaddr $net_i | |
This file contains 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
Copyright (c) 2019 - Paolo Perego - paolo@armoredcode.com | |
Permission is hereby granted, free of charge, to any person | |
obtaining a copy of this software and associated documentation | |
files (the "Software"), to deal in the Software without | |
restriction, including without limitation the rights to use, | |
copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the | |
Software is furnished to do so, subject to the following | |
conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment