Skip to content

Instantly share code, notes, and snippets.

#!/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 / 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}*}
@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 / 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 ))"
}
#!/bin/bash
############################
## Methods
############################
prefix_to_bit_netmask() {
prefix=$1;
shift=$(( 32 - prefix ));
bitmask=""
for (( i=0; i < 32; i++ )); do
num=0
@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
@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=""
#!/bin/bash
############################
## Methods
############################
prefix_to_bit_netmask() {
prefix=$1;
shift=$(( 32 - prefix ));
bitmask=""
@aachyee
aachyee / gist:f42c765bcbeade506107457a11c685b3
Last active August 30, 2023 15:57 — forked from StephenGenusa/gist:90f0ecc094eb630975a97732489d4ba4
Build wget2 with all features on clean Ubuntu 20.04 installation
sudo apt-get -y install gcc g++ make cmake doxygen pandoc awk sed perl python3 python-is-python3 m4 bison flex lzip lcov git zlib1g-dev libssl-dev libnghttp2-dev autoconf autoconf-archive autogen automake autopoint libtool pkg-config texinfo nettle-dev libunistring-dev gettext make libbz2-dev libbrotli-dev libzstd-dev liblz-dev libpcre2-dev libmicrohttpd-dev libgpgme-dev liblzma-dev libgnutls28-dev libgcrypt20-dev git-merge-changelog libidn2-0 libpsl-dev # libpcre3-dev nettle-bin lzma brotli zstd lzip
cd ~/Downloads
mkdir wget-dev
cd wget-dev
git clone https://gitlab.com/rockdaboot/libhsts
cd libhsts
autoreconf -fi
./configure
make
@aachyee
aachyee / udp.rb
Last active September 6, 2021 23:41 — forked from akostadinov/udp.rb
Ruby UDP echo server and client
require 'socket'
# echo server
Socket.udp_server_loop(4444) do |data, src|
src.reply data
end
# client
addr = Socket.sockaddr_in(4444, "localhost")
socket = Socket.new(:INET, :DGRAM)
begin