From 78ee3595ec29c73038fdc73bfa69ff72371e3f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 28 Jan 2024 20:47:53 +0100 Subject: [PATCH] Added function into a files not in zshrc --- .local/bin/cidr2mask | 21 +++++++++++++++++++++ .local/bin/mask2cidr | 20 ++++++++++++++++++++ .zshrc | 37 ------------------------------------- 3 files changed, 41 insertions(+), 37 deletions(-) create mode 100755 .local/bin/cidr2mask create mode 100755 .local/bin/mask2cidr diff --git a/.local/bin/cidr2mask b/.local/bin/cidr2mask new file mode 100755 index 0000000..6999297 --- /dev/null +++ b/.local/bin/cidr2mask @@ -0,0 +1,21 @@ +#!/bin/bash +cidr2mask() { + local i mask="" + local full_octets=$(($1 / 8)) + local partial_octet=$(($1 % 8)) + + for ((i = 0; i < 4; i += 1)); do + if [ $i -lt $full_octets ]; then + mask+=255 + elif [ $i -eq $full_octets ]; then + mask+=$((256 - 2 ** (8 - partial_octet))) + else + mask+=0 + fi + test $i -lt 3 && mask+=. + done + + echo $mask +} + +cidr2mask "$@" diff --git a/.local/bin/mask2cidr b/.local/bin/mask2cidr new file mode 100755 index 0000000..0bb1406 --- /dev/null +++ b/.local/bin/mask2cidr @@ -0,0 +1,20 @@ +#!/bin/bash +mask2cidr() { + local mask=$1 + + # In RFC 4632 netmasks there's no "255." after a non-255 byte in the mask + local left_stripped_mask=${mask##*255.} + local len_mask=${#mask} + local len_left_stripped_mask=${#left_stripped_mask} + + local conversion_table=0^^^128^192^224^240^248^252^254^ + local number_of_bits_stripped=$(((len_mask - len_left_stripped_mask) * 2)) + local signifacant_octet=${left_stripped_mask%%.*} + + local right_stripped_conversion_table=${conversion_table%%$signifacant_octet*} + local len_right_stripped_conversion_table=${#right_stripped_conversion_table} + local number_of_bits_from_conversion_table=$((len_right_stripped_conversion_table / 4)) + echo $((number_of_bits_stripped + number_of_bits_from_conversion_table)) +} + +mask2cidr "$@" diff --git a/.zshrc b/.zshrc index 0348f4b..0bfb820 100644 --- a/.zshrc +++ b/.zshrc @@ -16,43 +16,6 @@ gbranch() { echo -e "$(git branch "$@")" } -mask2cidr() { - local mask=$1 - - # In RFC 4632 netmasks there's no "255." after a non-255 byte in the mask - local left_stripped_mask=${mask##*255.} - local len_mask=${#mask} - local len_left_stripped_mask=${#left_stripped_mask} - - local conversion_table=0^^^128^192^224^240^248^252^254^ - local number_of_bits_stripped=$((($len_mask - $len_left_stripped_mask) * 2)) - local signifacant_octet=${left_stripped_mask%%.*} - - local right_stripped_conversion_table=${conversion_table%%$signifacant_octet*} - local len_right_stripped_conversion_table=${#right_stripped_conversion_table} - local number_of_bits_from_conversion_table=$((len_right_stripped_conversion_table / 4)) - echo $(($number_of_bits_stripped + $number_of_bits_from_conversion_table)) -} - -cidr2mask() { - local i mask="" - local full_octets=$(($1 / 8)) - local partial_octet=$(($1 % 8)) - - for ((i = 0; i < 4; i += 1)); do - if [ $i -lt $full_octets ]; then - mask+=255 - elif [ $i -eq $full_octets ]; then - mask+=$((256 - 2 ** (8 - $partial_octet))) - else - mask+=0 - fi - test $i -lt 3 && mask+=. - done - - echo $mask -} - [ -f /etc/gentoo-release ] && export ZSH="/usr/share/zsh/site-contrib/oh-my-zsh" [ -f /etc/centos-release ] && export ZSH="$HOME/.oh-my-zsh" [ -f /etc/debian_version ] && export ZSH="$HOME/.oh-my-zsh"