Skip to content

Instantly share code, notes, and snippets.

@aachyee
aachyee / msys2_shell.cmd
Created March 13, 2021 20:23
msys2/msys2_shell.cmd
@echo off
setlocal EnableDelayedExpansion
set "WD=%__CD__%"
if NOT EXIST "%WD%msys-2.0.dll" set "WD=%~dp0usr\bin\"
set "LOGINSHELL=bash"
set /a msys2_shiftCounter=0
rem To activate windows native symlinks uncomment next line
rem set MSYS=winsymlinks:nativestrict
#!/bin/bash
############################
## Methods
############################
prefix_to_bit_netmask() {
prefix=$1;
shift=$(( 32 - prefix ));
bitmask=""
@aachyee
aachyee / ipresolv.sh
Created September 24, 2020 16:45 — forked from alessiomangoni13/ipresolv.sh
Script per la generazione di IP a partire da notazione CIDR
#!/bin/bash
############################
## Methods
############################
prefix_to_bit_netmask() {
prefix=$1;
shift=$(( 32 - prefix ));
bitmask=""
@aachyee
aachyee / broadcast_calc.sh
Created September 24, 2020 16:37 — forked from cskeeters/broadcast_calc.sh
Bash script for calculating network and broadcast addresses from ip and netmask or CIDR Notation
#!/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
#!/bin/bash
############################
## Methods
############################
prefix_to_bit_netmask() {
prefix=$1;
shift=$(( 32 - prefix ));
bitmask=""
for (( i=0; i < 32; i++ )); do
num=0
@aachyee
aachyee / cidr-to-netmask.sh
Created September 24, 2020 16:30 — forked from kwilczynski/cidr-to-netmask.sh
CIDR to netmask in bash.
# 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 ))"
}
@aachyee
aachyee / ip_and_netmask2cidr.sh
Created September 24, 2020 10:19 — forked from robinsmidsrod/ip_and_netmask2cidr.sh
Calculate IPv4 network address and CIDR mask from IP address (dotted quad) and network mask (dotted quad)
# $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 () {
@aachyee
aachyee / netutils.sh
Created September 24, 2020 10:19 — forked from tiveone/netutils.sh
Convert netmask to cidr.
#!/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}*}
#!/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
@aachyee
aachyee / addr.sh
Created September 24, 2020 10:15 — forked from ar45/addr.sh
Pure shell IPv4 subnet calculations
#!/bin/sh
exit_error()
{
echo "$1" >&2 && exit 1
}
valid_ip()
{