Difference between revisions of "Pro cidr2mask and mask2cidr scripts"
From thelinuxwiki
(Pushed from thelinuxwiki.com.) |
|||
Line 44: | Line 44: | ||
} | } | ||
− | function netmask2cidr() | + | function netmask2cidr() |
− | { | + | { |
# Phase 1: Count how many 255 prefixes | # Phase 1: Count how many 255 prefixes | ||
local mask="${1//255./xrandomx}" sfx=${1//255.} | local mask="${1//255./xrandomx}" sfx=${1//255.} | ||
Line 55: | Line 55: | ||
mask="${mask//$2*}" | mask="${mask//$2*}" | ||
echo $(( ${1} + ${#mask}/4 )) | echo $(( ${1} + ${#mask}/4 )) | ||
− | } | + | } |
− | + | # Parse and validate arguments | |
− | # Parse and validate arguments | + | [ -z "$1" ] && exit 0 |
− | [ -z "$1" ] && exit 0 | + | [ "${1%/*}" == "$1" ] && mask="$2" || mask="${1#*/}" |
− | [ "${1%/*}" == "$1" ] && mask="$2" || mask="${1#*/}" | + | [ -z "$mask" ] && echo "No netmask." && exit 0 |
− | [ -z "$mask" ] && echo "No netmask." && exit 0 | + | # Check and convert mask |
− | + | ip=${1%/*} | |
− | # Check and convert mask | + | # Convert CIDR to netmask and vice versa |
− | ip=${1%/*} | + | echo -n "IP: ${ip}/${mask} --> ${ip}/" |
− | + | is_number $mask && cidr2netmask $mask || netmask2cidr $mask | |
− | # Convert CIDR to netmask and vice versa | + | |
− | echo -n "IP: ${ip}/${mask} --> ${ip}/" | + | |
− | is_number $mask && cidr2netmask $mask || netmask2cidr $mask | + | |
[[category:bash]] | [[category:bash]] |
Revision as of 13:47, 28 August 2013
taken from http://forums.gentoo.org/viewtopic-p-6770850.html
function cidr2netmask() {
local maskpat="255 255 255 255" local maskdgt="254 252 248 240 224 192 128" set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3} echo ${1-0}.${2-0}.${3-0}.${4-0}
}
function netmask2cidr() {
# Phase 1: Count how many 255 prefixes local mask="${1//255./xrandomx}" sfx=${1//255.} mask="${mask%%digit:*}" set -- ${#mask} ${sfx//./ }
# Phase 2: Convert the first non-255 number, rest is supposed to be zeroes mask="0--.128.192.224.240.248.252.254." mask="${mask//$2*}" echo $(( ${1} + ${#mask}/4 ))
}
Here's the complete script to test these two functions and examples: cidrnetmask.sh:
- !/bin/sh
- Script: cidrnetmask.sh
- By Vince C <v_cadet AT yahoo.fr>.
- Released under the WTFPL :D .
function is_number() {
[ -n "${1}" ] && [ -z "${1//digit:}" ]
}
function cidr2netmask() {
local maskpat="255 255 255 255" local maskdgt="254 252 248 240 224 192 128" set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3} echo ${1-0}.${2-0}.${3-0}.${4-0}
}
function netmask2cidr() { # Phase 1: Count how many 255 prefixes local mask="${1//255./xrandomx}" sfx=${1//255.} mask="${mask%%digit:*}" set -- ${#mask} ${sfx//./ }
# Phase 2: Convert the first non-255 number, rest is supposed to be zeroes mask="0--.128.192.224.240.248.252.254." mask="${mask//$2*}" echo $(( ${1} + ${#mask}/4 )) } # Parse and validate arguments [ -z "$1" ] && exit 0 [ "${1%/*}" == "$1" ] && mask="$2" || mask="${1#*/}" [ -z "$mask" ] && echo "No netmask." && exit 0 # Check and convert mask ip=${1%/*} # Convert CIDR to netmask and vice versa echo -n "IP: ${ip}/${mask} --> ${ip}/" is_number $mask && cidr2netmask $mask || netmask2cidr $mask