This file contains hidden or 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 | |
| ############################ | |
| ## Methods | |
| ############################ | |
| prefix_to_bit_netmask() { | |
| prefix=$1; | |
| shift=$(( 32 - prefix )); | |
| bitmask="" |
This file contains hidden or 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 | |
| # Calculates network and broadcast based on supplied ip address and netmask | |
| # Usage: broadcast_calc.sh 192.168.0.1 255.255.255.0 | |
| # Usage: broadcast_calc.sh 192.168.0.1/24 | |
| tonum() { | |
| if [[ $1 =~ ([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+) ]]; then |
This file contains hidden or 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 | |
| ############################ | |
| ## Methods | |
| ############################ | |
| prefix_to_bit_netmask() { | |
| prefix=$1; | |
| shift=$(( 32 - prefix )); | |
| bitmask="" | |
| for (( i=0; i < 32; i++ )); do | |
| num=0 |
This file contains hidden or 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
| # Return netmask for a given network and CIDR. | |
| cidr_to_netmask() { | |
| value=$(( 0xffffffff ^ ((1 << (32 - $2)) - 1) )) | |
| echo "$(( (value >> 24) & 0xff )).$(( (value >> 16) & 0xff )).$(( (value >> 8) & 0xff )).$(( value & 0xff ))" | |
| } |
This file contains hidden or 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
| # $1 is a decimal number between 0-255 | |
| dec2bin () { | |
| [ -z "$1" ] && return | |
| D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}) | |
| echo ${D2B[$1]} | |
| } | |
| # $1 is ip address (dotted quad) | |
| # $2 is netmask (dotted quad) | |
| calc_network () { |
This file contains hidden or 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 | |
| function ::netmask2cidr() | |
| { | |
| case $1 in | |
| 0x*) | |
| local hex=${1#0x*} quad= | |
| while [ -n "${hex}" ]; do | |
| local lastbut2=${hex#??*} | |
| quad=${quad}${quad:+.}0x${hex%${lastbut2}*} |
This file contains hidden or 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/sh | |
| # Copyright (C) 2016 The Linux Containers Project | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software |
This file contains hidden or 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/sh | |
| exit_error() | |
| { | |
| echo "$1" >&2 && exit 1 | |
| } | |
| valid_ip() | |
| { |
This file contains hidden or 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
| #!/usr/bin/env bash | |
| # Print usage | |
| function usage { | |
| echo -n "$(basename $0) CIDR... | |
| $(basename $0) [OPTION] CIDR... | |
| This script prints the ip range or full list of ip addresses for one or more CIDR. | |
| Options: |
This file contains hidden or 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 | |
| # Function to convert cidr to a mask | |
| cidr2mask () { | |
| # Number of args to shift, 255..255, first non-255 byte, zeroes | |
| set -- $(( 5 - (${1} / 8) )) 255 255 255 255 $(( (255 << (8 - (${1} % 8))) & 255 )) 0 0 0 | |
| [ ${1} -gt 1 ] && shift ${1} || shift | |
| echo ${1-0}.${2-0}.${3-0}.${4-0} | |
| } |