commit f39b0fbcd8c8f4a86e98e17a6048463c29689aed Author: Christoph Date: Mon Apr 30 02:53:44 2018 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60deed0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.log +*.swp +*conf/*.conf diff --git a/snippets/base_script.sh b/snippets/base_script.sh new file mode 100755 index 0000000..ed286ca --- /dev/null +++ b/snippets/base_script.sh @@ -0,0 +1,285 @@ +#!/usr/bin/env bash + +script_name="$(basename $(realpath $0))" +working_dir="$(dirname $(realpath $0))" + +conf_file="${working_dir}/conf/${script_name%%.*}.conf" + +LOCK_DIR="/tmp/$(basename $0).$$.LOCK" +log_file="${LOCK_DIR}/${script_name%%.*}.log" + +backup_date="$(date +%Y-%m-%d-%H%M)" + +# ---------- +# Base Function(s) +# ---------- + +usage() { + + + [[ -n "$1" ]] && error "$1" + + + [[ $terminal ]] && echo -e " +\033[1mUsage:\033[m + + $(basename $0) [OPTION [OPTION .. + +\033[1mDescription\033[m + + + +\033[1mOptions\033[m + + + +\033[1mExample:\033[m + + + + $(basename $0) .. + + + + $(basename $0) .. + +" + + clean_up 1 + +} + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + exit $1 +} + + +echononl(){ + if $terminal ; then + echo X\\c > /tmp/shprompt$$ + if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then + echo -e -n " $*\\c" 1>&2 + else + echo -e -n " $*" 1>&2 + fi + rm /tmp/shprompt$$ + fi +} + + +fatal(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ] $*" + else + echo -e " [ Fatal ] $*" + fi + echo "" + if $terminal ; then + echo -e " \033[1mScript terminated\033[m.." + else + echo -e " Script terminated.." + fi + echo "" + rm -rf $LOCK_DIR + exit 1 +} + +error (){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ] $*" + else + echo " [ Error ] $*" + fi + echo "" +} + +warn (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarn\033[m ] $*" + else + echo " [ Warn ] $*" + fi + echo "" + fi +} + +info (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mInfo\033[m ] $*" + else + echo " [ Info ] $*" + fi + echo "" + fi +} + +ok (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mOk\033[m ] $*" + else + echo " [ Ok ] $*" + fi + echo "" + fi +} + +echo_done() { + if $terminal ; then + echo -e "\033[75G[ \033[32mdone\033[m ]" + fi +} +echo_ok() { + if $terminal ; then + echo -e "\033[75G[ \033[32mok\033[m ]" + fi +} +echo_failed(){ + if $terminal ; then + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal ; then + echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + + + +# ---------- +# - Jobhandling +# ---------- + +# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM +# - +trap clean_up SIGHUP SIGINT SIGTERM + +# - Create lock directory '$LOCK_DIR" +# +mkdir "$LOCK_DIR" + + +# ---------- +# - Headline +# ---------- + +if $terminal ; then + echo "" + echo -e "\033[1m----------\033[m" + echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" + echo -e "\033[1m----------\033[m" +fi + + +# ---------- +# - Some checks .. +# ---------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + +# - Print help? +# - +if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then + usage +fi + +if [[ -z "$(which basename)" ]]; then + fatal 'It seems "basename" is not installed, but needed!' +fi + +if [[ -z "$(which realpath)" ]]; then + fatal 'It seems "realpath" is not installed, but needed!' +fi + + +# ========== +# - Begin Main Script +# ========== + +# ---------- +# Read Configurations from $conf_file +# ---------- + + +# - Give your default values here +# - +LOGGING=false + +if [[ -f "$conf_file" ]]; then + source "$conf_file" +else + warn "No configuration file '$conf_file' present.\n + Loading default values.." +fi + + +# ---------- +# - Some pre-script tasks .. +# ---------- + +if $terminal ; then + echo "" + echo "" + echo -e " \033[1mDoing some pre-script tasks ..\033[m" + echo "" +fi + +echononl "All is fine" +echo_ok + + +# ---------- +# - Main part of script +# ---------- + +if $terminal ; then + echo "" + echo "" + echo -e " \033[1mMain part of script ..\033[m" + echo "" +fi + + +# ---------- +# - Some post-script tasks .. +# ---------- + +if $terminal ; then + echo "" + echo "" + echo -e " \033[1mDoing some post-script tasks ..\033[m" + echo "" +fi + +clean_up 0 diff --git a/snippets/cidr2mask.sh b/snippets/cidr2mask.sh new file mode 100755 index 0000000..3d7eb5a --- /dev/null +++ b/snippets/cidr2mask.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +# - Convert CIDR to netmask +# - +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 +} + +cidr=$1 +mask=$(cidr2mask $cidr) + +echo "" +echo "cidr: $cidr - mask: $mask" +echo "" +exit 0 + + diff --git a/snippets/conf/base_script.conf.sample b/snippets/conf/base_script.conf.sample new file mode 100644 index 0000000..6294e6d --- /dev/null +++ b/snippets/conf/base_script.conf.sample @@ -0,0 +1,17 @@ +# ========== +# - Configuration file for script base_script.sh +# ========== + +# - LOGGING +# - +# - Enables/Disables script output. Setting this value to 'true' is +# - only useful if NOT running in a terminal (i.e. as cronjob). +# - +# - If script is running in a terminal, script output is enabled and +# - cannot be disabled. +# - +# - Running this script in a +# - +# - Default value: false +# - +#LOGGING=false diff --git a/snippets/declare_i.sh b/snippets/declare_i.sh new file mode 100755 index 0000000..9d513bc --- /dev/null +++ b/snippets/declare_i.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +declare -i num=1 + +while [ $num -lt 128 ] ; do + ping -c 5 195.135.133.$num > /dev/null 2>&1; + if [ "$?" == 0 ]; then + echo 195.135.133.$num; + fi; + let num=$num+1 +done diff --git a/snippets/detect_os.sh b/snippets/detect_os.sh new file mode 100755 index 0000000..a8c4fa2 --- /dev/null +++ b/snippets/detect_os.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash + +detect_os_1 () { + + if $(which lsb_release > /dev/null 2>&1) ; then + + os_dist="$(lsb_release -i | awk '{print tolower($3)}')" + os_version="$(lsb_release -r | awk '{print tolower($2)}')" + os_codename="$(lsb_release -c | awk '{print tolower($2)}')" + + if [[ "$os_dist" = "debian" ]]; then + if $(echo "$os_version" | grep -q '\.') ; then + os_version=$(echo "$os_version" | cut --delimiter='.' -f1) + fi + fi + + elif [[ -e "/etc/os-release" ]]; then + + . /etc/os-release + + os_dist=$ID + os_version=${VERSION_ID} + + fi + + # remove whitespace from os_dist and os_version + os_dist="${os_dist// /}" + os_version="${os_version// /}" + +} + +detect_os_2 () +{ + if [[ ( -z "${os}" ) && ( -z "${os_dist}" ) ]]; then + # some systems dont have lsb-release yet have the lsb_release binary and + # vice-versa + if [ -e /etc/lsb-release ]; then + . /etc/lsb-release + + if [ "${ID}" = "raspbian" ]; then + os=${ID} + os_dist=`cut --delimiter='.' -f1 /etc/debian_version` + else + os=${DISTRIB_ID} + os_dist=${DISTRIB_CODENAME} + + if [ -z "$os_dist" ]; then + os_dist=${DISTRIB_RELEASE} + fi + fi + + elif [ `which lsb_release 2>/dev/null` ]; then + os_dist=`lsb_release -c | cut -f2` + os=`lsb_release -i | cut -f2 | awk '{ print tolower($1) }'` + + elif [ -e /etc/debian_version ]; then + # some Debians have jessie/sid in their /etc/debian_version + # while others have '6.0.7' + os=`cat /etc/issue | head -1 | awk '{ print tolower($1) }'` + if grep -q '/' /etc/debian_version; then + os_dist=`cut --delimiter='/' -f1 /etc/debian_version` + else + os_dist=`cut --delimiter='.' -f1 /etc/debian_version` + fi + + else + unknown_os + fi + fi + + if [ -z "$os_dist" ]; then + unknown_os + fi + + # remove whitespace from OS and os_dist name + os="${os// /}" + os_dist="${os_dist// /}" + + echo "Detected operating system as $os/$os_dist." +} + +detect_os_1 + +echo -e "\nOutput from function 'detect_os_1'" +echo "==================================" + +echo "Distribution: ${os_dist}" +echo "Version: ${os_version}" +if [[ -n "$os_codename" ]]; then + echo "Codename: ${os_codename}" +fi + +os_dist="" +os_version="" +os_codename="" + +echo -e "\nOutput from function 'detect_os_2'" +echo "==================================" +detect_os_2 + +echo "OS: $os" +echo "Dist: $os_dist" + + +echo +exit diff --git a/snippets/files/ban_ipv4.list b/snippets/files/ban_ipv4.list new file mode 100644 index 0000000..10b7da3 --- /dev/null +++ b/snippets/files/ban_ipv4.list @@ -0,0 +1,22 @@ +# - IPv4 addresses listet here will be completly banned by the firewall +# - +# - - Line beginning with '#' will be ignored. +# - - Blank lines will be ignored +# - - Only the first entry (until space sign or end of line) of each line will be considered. +# - +# - Valid values are: +# - complete IPv4 adresses like 1.2.3.4 (will be converted to 1.2.3.0/32) +# - partial IPv4 addresses like 1.2.3 (will be converted to 1.2.3.0/24) +# - network/nn CIDR notation like 1.2.3.0/27 +# - network/netmask notaions like 1.2.3.0/255.255.255.0 +# - network/partial_netmask like 1.2.3.4/255 +# - +# - Note: +# - - wrong addresses like 1.2.3.256 or 1.2.3.4/33 will be ignored +# - +# - Example: +# - 79.171.81.0/24 +# - 79.171.81.0/255.255.255.0 +# - 79.171.81.0/255.255.255 +# - 79.171.81 + diff --git a/snippets/files/password.list b/snippets/files/password.list new file mode 100644 index 0000000..a073a03 --- /dev/null +++ b/snippets/files/password.list @@ -0,0 +1,463 @@ +############################################################ +# absent-friends.org # +############################################################ + + +############################################################ +# mail36.net # +############################################################ +trotzig@mail36.net:wendland1 +armin@mail36.net:Me8tallica +silke@mail36.net:24011979 +sindikat@mail36.net:passwd314 +michel@mail36.net:cikedece + +############################################################ +# access.so36.net # +############################################################ + + +############################################################ +# agora-info.de # +############################################################ + + +############################################################ +# alte-socken.de # +############################################################ + + +############################################################ +# ansoko.de # +############################################################ + + +############################################################ +# antifa.de # +############################################################ + + +############################################################ +# antifa-kok.de # +############################################################ + + +############################################################ +# antifa-versand.de # +############################################################ + + +############################################################ +# anti-hartz-buendnis-nrw.de # +############################################################ + + +############################################################ +# antimilitarismustag.de # +############################################################ + + +############################################################ +# antimilitaristisch-unterwegs.so36.net # +############################################################ + + +############################################################ +# anyplacebeforenow.net # +############################################################ + + +############################################################ +# archiv-kiel.de # +############################################################ + + +############################################################ +# autoorganisation.org # +############################################################ + + +############################################################ +# az-wuppertal.de # +############################################################ +uschi-anschlag@az-wuppertal.de:sommer13 + +############################################################ +# bamm.de # +############################################################ +gerit@bamm.de:icke01 + +############################################################ +# bdsberlin.org # +############################################################ + + +############################################################ +# bds-kampagne.de # +############################################################ + + +############################################################ +# beatleprint.de # +############################################################ + + +############################################################ +# bildungskritik.de # +############################################################ + + +############################################################ +# cilip.de # +############################################################ + + +############################################################ +# cinetrain.indyvideo.net # +############################################################ + + +############################################################ +# codecoop.org # +############################################################ + + +############################################################ +# datarecollective.net # +############################################################ + + +############################################################ +# desertoere.de # +############################################################ + + +############################################################ +# die-linkspartei.de # +############################################################ + + +############################################################ +# dosto.de # +############################################################ +ahi@dosto.de:muhkuh2k + +############################################################ +# einstellung.so36.net # +############################################################ + + +############################################################ +# freedom-for-thomas.de # +############################################################ + + +############################################################ +# fsi-geschichte.so36.net # +############################################################ + + +############################################################ +# funk-the-system.net # +############################################################ + + +############################################################ +# g20-doku.org # +############################################################ + + +############################################################ +# g8andwar.de # +############################################################ + + +############################################################ +# gaypunk.de # +############################################################ + + +############################################################ +# geloebnix.de # +############################################################ + + +############################################################ +# gemeinsam-gegen-nazis.de # +############################################################ + + +############################################################ +# gipfelsoli.org # +############################################################ + + +############################################################ +# hamburgerwetter.de # +############################################################ + + +############################################################ +# hamburg.geloebnix.de # +############################################################ + + +############################################################ +# hh.geloebnix.de # +############################################################ + + +############################################################ +# hotmehl.com # +############################################################ +oasch@hotmehl.com:oasch + +############################################################ +# iaadh.de # +############################################################ + + +############################################################ +# kamalatta.de # +############################################################ + + +############################################################ +# kill-hup.de # +############################################################ + + +############################################################ +# koelner-sozialforum.de # +############################################################ + + +############################################################ +# kpd-rz.de # +############################################################ + + +############################################################ +# krank-feiern.org # +############################################################ + + +############################################################ +# kreta-film.net # +############################################################ + + +############################################################ +# libertad.so36.net # +############################################################ + + +############################################################ +# netzwerk-asyl.de # +############################################################ + + +############################################################ +# nolager.de # +############################################################ + + +############################################################ +# norden-gegen-g8.info # +############################################################ + + +############################################################ +# nsc.so36.net # +############################################################ + + +############################################################ +# oh21.de # +############################################################ + + +############################################################ +# ohne-uns.de # +############################################################ + + +############################################################ +# opferperspektive.de # +############################################################ + + +############################################################ +# opfer-rechter-gewalt.de # +############################################################ + + +############################################################ +# ostpack.de # +############################################################ + + +############################################################ +# palaestina-solidaritaet.de # +############################################################ + + +############################################################ +# publicsolidarity.de # +############################################################ +redaktion@publicsolidarity.de:prospekt17 + +############################################################ +# redecontraviolencia.org # +############################################################ + + +############################################################ +# redfrentetransgenicos.net # +############################################################ + + +############################################################ +# rndm.biz # +############################################################ + + +############################################################ +# rojava-solidaritaet.net # +############################################################ + + +############################################################ +# schoenerfriedrichshain.de # +############################################################ + + +############################################################ +# schokoholic.de # +############################################################ + + +############################################################ +# s-e-d.org # +############################################################ + + +############################################################ +# so36.com # +############################################################ + + +############################################################ +# so36.de # +############################################################ +fatma@so36.de:gayhanebleibt +crise@so36.de:kiez666 +criselend@so36.de:roller36 +monique@so36.de:Jule123 +monique@so36.de:valentin1 +stern89@so36.de:zk24.8r +post@so36.de:wurst42 +chris@so36.de:akiraxs + +############################################################ +# so36.net # +############################################################ +ckubu@so36.net:EadGl15E.% +leia@so36.net:f1n4lly +tobi@so36.net:blamblam67 +tom@so36.net:THomas10 +tom@so36.net:adelskrone +tom@so36.net:tompunk +acab@so36.net:irhab.6 +acab@so36.net:zickezacke +schandmaul@so36.net:napalm +svenja@so36.net:fruhansen +svenja@so36.net:kaefer48 +peace@so36.net:romuzuxe +peace@so36.net:scheisse +susi@so36.net:R2D2-f +ilker@so36.net:test123 +idfx@so36.net:dlgesmsl.01 +vitaminepille@so36.net:lecker6 +defa-blendr@so36.net:azocomyw +christopher@so36.net:daredevil +altdel@so36.net:Knutson +altdel@so36.net:Knutson-01 +altdel@so36.net:spitzwitz +daniel@so36.net:roFl0815 +anke@so36.net:0119289011 +andi@so36.net:malou +mio@so36.net:blonde redhead +mir@so36.net:somos +sanne@so36.net:xobolude +maria.schulz@so36.net:U120108681 +mario@so36.net:holsten_ +markus@so36.net:engagierthoch3 +joe@so36.net:music +jonas@so36.net:hinzkunz +janmaat@so36.net:bumerang + +############################################################ +# so36net.de # +############################################################ + + +############################################################ +# so36.org # +############################################################ + + +############################################################ +# socialforum-berlin.org # +############################################################ + + +############################################################ +# sozialforum-berlin.de # +############################################################ + + +############################################################ +# speakerinnen.org # +############################################################ + + +############################################################ +# stop-torture.de # +############################################################ + + +############################################################ +# talk36.net # +############################################################ + + +############################################################ +# uffmucken-schoeneweide.de # +############################################################ + + +############################################################ +# una-pt.org # +############################################################ + + +############################################################ +# verdammtlangquer.org # +############################################################ + + +############################################################ +# web.so36.net # +############################################################ + + +############################################################ +# womensrightsproject.de # +############################################################ + + +############################################################ +# wuppertal-dichtmachen.de # +############################################################ + diff --git a/snippets/get_next_uid_gid_pair.sh b/snippets/get_next_uid_gid_pair.sh new file mode 100755 index 0000000..78269ec --- /dev/null +++ b/snippets/get_next_uid_gid_pair.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +min_uid=1000 + +declare -i _id=$(expr $min_uid - 1) + +while true ; do + ((_id++)) + $(id $_id > /dev/null 2>&1) && continue + $(cat /etc/group | cut -d ':' -f3 | grep -q $_id 2> /dev/null) && continue + break +done + +echo +echo " Next free uid/gid pair:" +echo +echo -e " uid: \033[1m$_id\033[m" +echo -e " gid: \033[1m$_id\033[m" +echo + +exit 0 + diff --git a/snippets/get_top_level_parent_pid.sh b/snippets/get_top_level_parent_pid.sh new file mode 100755 index 0000000..bcb4580 --- /dev/null +++ b/snippets/get_top_level_parent_pid.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +function top_level_parent_pid { + # Look up the parent of the given PID. + pid=${1:-$$} + stat=($( /dev/null | grep -w $day > /dev/null +if [[ $? -eq 0 ]] ; then + echo "" + echo "Valid Date: $DATE" + echo "" +else + echo "" + echo "Invalid Date: $DATE" + echo "" + echo "Usage: $(basename $0) YYYYMMDD" + echo "" +fi + +exit 0 diff --git a/snippets/is_chrooted.sh b/snippets/is_chrooted.sh new file mode 100755 index 0000000..f44bb94 --- /dev/null +++ b/snippets/is_chrooted.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +# - Test if script is running in a chrooted environment +# - +# - !! Script MUST run with root privileges!! +# - + +if [ "$(id -u)" != "0" ]; then + echo -e "\n\tThis script must be run as root.\n" 1>&2 + exit 1 +fi + +if [[ "$(stat -c %d:%i /)" = "$(stat -c %d:%i /proc/1/root/.)" ]]; then + echo -e "\n\tRunning NOT in a chrooted environment.\n" +else + echo -e "\n\tRunning in a chrooted environment.\n" + +fi + diff --git a/snippets/is_number.sh b/snippets/is_number.sh new file mode 100755 index 0000000..0acb43b --- /dev/null +++ b/snippets/is_number.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +is_number() { + + return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1); + + # - also possible + # - + #[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1 + #return $([[ ! -z "${1##*[!0-9]*}" ]]) +} + +is_int() { + return $(test "$@" -eq "$@" > /dev/null 2>&1); +} + +_int=-5 +_number=5 +_no_number=5a + +echo -e "\nTest of valid number (positiv integer)" +if is_number $_number ; then + echo -e "\t$_number is a number" +else + echo -e "\t$_number is NOT a number" +fi +if is_number $_int ; then + echo -e "\t$_int is a number" +else + echo -e "\t$_int is NOT a number" +fi +if is_number $_no_number ; then + echo -e "\t$_no_number is a number" +else + echo -e "\t$_no_number is NOT a number" +fi + + +echo -e "\nTest of valid integer" +if is_int $_number ; then + echo -e "\t$_number is a valid integer" +else + echo -e "\t$_number is NOT a valid integer" +fi +if is_int $_int ; then + echo -e "\t$_int is a valid integer" +else + echo -e "\t$_int is NOT a valid integer" +fi +if is_int $_no_number ; then + echo -e "\t$_no_number is a valid integer" +else + echo -e "\t$_no_number is NOT a valid integer" +fi + +echo "" +exit 0 diff --git a/snippets/is_valid_ipv4.sh b/snippets/is_valid_ipv4.sh new file mode 100755 index 0000000..84d1e18 --- /dev/null +++ b/snippets/is_valid_ipv4.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +## - Test of valid IPv4 Address +## - +## - Returns 0 if valid, > 0 otherwise +## - +is_valid_ipv4() { + local -a octets=( ${1//\./ } ) + local RETURNVALUE=0 + + # return an error if the IP doesn't have exactly 4 octets + [[ ${#octets[@]} -ne 4 ]] && return 1 + + for octet in ${octets[@]} + do + if [[ ${octet} =~ ^[0-9]{1,3}$ ]] + then # shift number by 8 bits, anything larger than 255 will be > 0 + ((RETURNVALUE += octet>>8 )) + else # octet wasn't numeric, return error + return 1 + fi + done + return ${RETURNVALUE} +} + +if [ "X$@" = "X" ]; then + echo -e "\n\t\033[33m\033[1mNo argumnet given!\033[m\n" + exit 1 +fi + +if is_valid_ipv4 $@ ; then + echo -e "\n\t\033[32m\033[1m$@ is a valid IPv4 Address\033[m\n" +else + echo -e "\n\t\033[31m\033[1m$@ is NOT a valid IPv4 Address\033[m\n" +fi + +exit diff --git a/snippets/mask2cidr.sh b/snippets/mask2cidr.sh new file mode 100755 index 0000000..6bde0f8 --- /dev/null +++ b/snippets/mask2cidr.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# Function calculates number of bit in a netmask +# +mask2cidr() { + nbits=0 + IFS=. + for dec in $1 ; do + case $dec in + 255) let nbits+=8;; + 254) let nbits+=7;; + 252) let nbits+=6;; + 248) let nbits+=5;; + 240) let nbits+=4;; + 224) let nbits+=3;; + 192) let nbits+=2;; + 128) let nbits+=1;; + 0);; + *) echo "Error: $dec is not recognised"; exit 1 + esac + done + echo "$nbits" +} + + +mask=$1 +cidr=$(mask2cidr $mask) + +echo "" +echo "cidr: $cidr - mask: $mask" +echo "" +exit 0 + + diff --git a/snippets/netmask2cidr.sh b/snippets/netmask2cidr.sh new file mode 100755 index 0000000..e6224e5 --- /dev/null +++ b/snippets/netmask2cidr.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +netmask2cidr() { + case $1 in + 0x*) + local hex=${1#0x*} quad= + while [ -n "${hex}" ]; do + local lastbut2=${hex#??*} + quad=${quad}${quad:+.}0x${hex%${lastbut2}*} + hex=${lastbut2} + done + set -- ${quad} + ;; + esac + + local i= len= + local IFS=. + for i in $1; do + while [ ${i} != "0" ]; do + len=$((${len} + ${i} % 2)) + i=$((${i} >> 1)) + done + done + + echo "${len}" +} + +netmask2cidr $1 diff --git a/snippets/qsort.sh b/snippets/qsort.sh new file mode 100755 index 0000000..9db7f7f --- /dev/null +++ b/snippets/qsort.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# quicksorts positional arguments +# return is in array qsort_ret +qsort() { + local pivot i smaller=() larger=() + qsort_ret=() + (($#==0)) && return 0 + pivot=$1 + shift + for i; do + if [[ $i < $pivot ]]; then + smaller+=( "$i" ) + else + larger+=( "$i" ) + fi + done + qsort "${smaller[@]}" + smaller=( "${qsort_ret[@]}" ) + qsort "${larger[@]}" + larger=( "${qsort_ret[@]}" ) + qsort_ret=( "${smaller[@]}" "$pivot" "${larger[@]}" ) +} + +array=(a c b "f f" 3 5) + +qsort "${array[@]}" + +declare -p qsort_ret + +exit 0 diff --git a/snippets/read_first_char_of_file.sh b/snippets/read_first_char_of_file.sh new file mode 100755 index 0000000..f352027 --- /dev/null +++ b/snippets/read_first_char_of_file.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +# - Read first charactor of a file: +# - +# - head -c1 + +deb_major_version="" + +is_number() { + + return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1); + + # - also possible + # - + #[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1 + #return $([[ ! -z "${1##*[!0-9]*}" ]]) +} + +if [[ -f "/etc/debian_version" ]]; then + + deb_major_version=$(head -c1 /etc/debian_version) + if is_number $deb_major_version ; then + echo -e "\n\tDebian Major Version: $deb_major_version\n" + else + echo -e "\n\tNo numeric Version given. Debian Version: $(head -1 /etc/debian_version)\n" + fi +else + echo -e "\nFile '/etc/debian_version' not found" +fi + +exit 0 diff --git a/snippets/read_ipv4_from_file.sh b/snippets/read_ipv4_from_file.sh new file mode 100755 index 0000000..ad1200a --- /dev/null +++ b/snippets/read_ipv4_from_file.sh @@ -0,0 +1,158 @@ +#!/usr/bin/env bash + +ipv4_list_file="files/ban_ipv4.list" + +if [[ ! -f "$ipv4_list_file" ]] ; then + echo "" + echo " File '$ipv4_list_file' not found'" + echo "" + exit 0 +fi + +declare -a octets +declare -i index + +while IFS='' read -r _line || [[ -n $_line ]] ; do + + is_valid_ipv4=true + is_valid_mask=true + ipv4="" + mask="" + + # Ignore comment lines + # + [[ $_line =~ ^[[:space:]]{0,}# ]] && continue + + # Ignore blank lines + # + [[ $_line =~ ^[[:space:]]*$ ]] && continue + + # Remove leading whitespace characters + # + _line="${_line#"${_line%%[![:space:]]*}"}" + + + # Catch IPv4 Address + # + given_ipv4="$(echo $_line | cut -d ' ' -f1)" + + + # Splitt Ipv4 address from possible given CIDR number + # + IFS='/' read -ra _addr <<< "$given_ipv4" + _ipv4="${_addr[0]}" + + if [[ -n "${_addr[1]}" ]] ; then + _mask="${_addr[1]}" + test_netmask=false + + # Is 'mask' a valid CIDR number? If not, test agains a valid netmask + # + if $(test -z "${_mask##*[!0-9]*}" > /dev/null 2>&1) ; then + + # Its not a vaild mask number, but naybe a valit netmask. + # + test_netmask=true + else + if [[ $_mask -gt 32 ]]; then + + # Its not a vaild cidr number, but naybe a valit netmask. + # + test_netmask=true + else + + # OK, we have a vaild cidr number between '0' and '32' + # + mask=$_mask + fi + fi + + # Test if given '_mask' is a valid netmask. + # + if $test_netmask ; then + octets=( ${_mask//\./ } ) + + # Complete netmask if necessary + # + while [[ ${#octets[@]} -lt 4 ]]; do + octets+=(0) + done + + [[ ${#octets[@]} -gt 4 ]] && is_valid_mask=false + + index=0 + for octet in ${octets[@]} ; do + if [[ ${octet} =~ ^[0-9]{1,3}$ ]] ; then + if [[ $octet -gt 255 ]] ; then + is_valid_mask=false + fi + if [[ $index -gt 0 ]] ; then + mask="${mask}.${octet}" + else + mask="${octet}" + fi + + else + is_valid_mask=false + fi + + ((index++)) + done + fi + + adjust_mask=false + else + mask=32 + adjust_mask=true + fi + + # Splitt given address into their octets + # + octets=( ${_ipv4//\./ } ) + + # Complete IPv4 address if necessary + # + while [[ ${#octets[@]} -lt 4 ]]; do + octets+=(0) + + # Only adjust CIDR number if not given + # + if $adjust_mask ; then + mask="$(expr $mask - 8)" + fi + done + + # Pre-check if given IPv4 Address seems to be a valid address + # + [[ ${#octets[@]} -gt 4 ]] && is_valid_ipv4=false + + # Check if given IPv4 Address is a valid address + # + if $is_valid_ipv4 ; then + index=0 + for octet in ${octets[@]} ; do + if [[ ${octet} =~ ^[0-9]{1,3}$ ]] ; then + if [[ $octet -gt 255 ]] ; then + is_valid_ipv4=false + fi + if [[ $index -gt 0 ]] ; then + ipv4="${ipv4}.${octet}" + else + ipv4="${octet}" + fi + + else + is_valid_ipv4=false + fi + + ((index++)) + done + fi + + if $is_valid_ipv4 && $is_valid_mask; then + echo " ${ipv4}/${mask}" + else + echo " '$given_ipv4' isn't a valid IPv4 address" + fi + +done < ban_ipv4.list diff --git a/snippets/read_yaml.sh b/snippets/read_yaml.sh new file mode 100755 index 0000000..b6bd03e --- /dev/null +++ b/snippets/read_yaml.sh @@ -0,0 +1,97 @@ +#!/usr/bin/env bash + +# - See: https://gist.github.com/epiloque/8cf512c6d64641bde388 +# - +# - Changes from the original (see above url: +# - if the yaml file contains carriage returns, the processing will fail. +# - In this case, I made a fix on this line (currently line 14) +# - +# - fs="$(echo @|tr @ '\034')" +# - +# - so it became +# - fs="$(echo @|tr @ '\034'|tr -d '\015')" +# - +# - NOTE! +# - The indent of the yaml configuration file is "2" +# - +# - If you want your indentation to be 4 spaces instead of 2 change this line: +# - indent = length($1)/2; +# - to be this: +# - indent = length($1)/4; +# - + +working_dir="$(dirname $(realpath $0))" + +# - This file will be created. (see below..) +# - +example_yaml_config_file="${working_dir}/files/example_yaml_config.yml" + +parse_yaml() { + local prefix=$2 + local s + local w + local fs + s='[[:space:]]*' + w='[a-zA-Z0-9_]*' + #fs="$(echo @|tr @ '\034')" + fs="$(echo @|tr @ '\034'|tr -d '\015')" + sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \ + -e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" | + awk -F"$fs" '{ + indent = length($1)/2; + vname[indent] = $2; + for (i in vname) {if (i > indent) {delete vname[i]}} + if (length($3) > 0) { + vn=""; for (i=0; i "$example_yaml_config_file" +development: + adapter: mysql2 + encoding: utf8 + database: my_database + username: root + password: + apt: + - somepackage + - anotherpackage +EOF + +eval $(parse_yaml "$example_yaml_config_file" "optional_prefix_") +#parse_yaml $example_yaml_config_file "optional_prefix" + +echo "" +echo "-------------------" +echo "- Read yaml configuration file" +echo "-------------------" + +echo "" +echo "$example_yaml_config_file:" +cat $example_yaml_config_file + + +echo -e "\nUsing with optional prefix 'optional_prefix_'" +echo "=============================================" +echo -e "\t\033[32meval \$(parse_yaml \"$example_yaml_config_file\" "optional_prefix_")\033[m" +echo "" +echo -e "\t\${optional_prefix_development_adapter}: ${optional_prefix_development_adapter}" +echo -e "\t\${optional_prefix_development_apt[@]}: ${optional_prefix_development_apt[@]}" + + +eval $(parse_yaml "$example_yaml_config_file" ) +#parse_yaml $example_yaml_config_file "optional_prefix" + +echo "" +echo -e "\nUsing without optional prefix " +echo "==============================" +echo -e "\t\033[32meval \$(parse_yaml \"$example_yaml_config_file\" )\033[m" +echo "" +echo -e "\t\${development_adapter}: ${development_adapter}" +echo -e "\t\${development_apt[@]}: ${development_apt[@]}" + +echo "" +rm "$example_yaml_config_file" +exit 0 diff --git a/snippets/sort_array.sh b/snippets/sort_array.sh new file mode 100755 index 0000000..8b10de4 --- /dev/null +++ b/snippets/sort_array.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +array=("a c" b f "Change" "3 5") +IFS=$'\n' sorted=($(sort <<<"${array[*]}")) + +echo "" + +echo "Print out array elemnts (for loop)" +echo "==================================" +for val in ${sorted[@]} ; do + echo -e "\t$val" +done + +echo "" + +echo "Print out array elemnts (printf)" +echo "================================" +printf "\t%s\n" "${sorted[@]}" + +echo "" +exit diff --git a/snippets/test_mail_paswd.sh b/snippets/test_mail_paswd.sh new file mode 100755 index 0000000..104c840 --- /dev/null +++ b/snippets/test_mail_paswd.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +script_dir="$(dirname $(realpath $0))" +passwd_file="${script_dir}/files/password.list" + +if [[ ! -f "$passwd_file" ]] ; then + echo "" + echo -e " [ \033[31m\033[1mFatal\033[m ] File '$passwd_file' not found!" + echo "" + exit 1 +fi + +#imap_server="imap.so36.net" +imap_server="localhost" + +if [[ $imap_server =~ ^127\.0\.0\.1 ]] || [[ $imap_server =~ ^localhost ]]; then + imap_protocol="imap" +else + imap_protocol="imaps" +fi + +echo "" +set +H +while IFS=':' read -r email passwd || [[ -n $email ]] ; do + + [[ $email =~ ^\s*$ ]] && continue + [[ $email =~ ^\s*# ]] && continue + + echo -en " Test given password for email \033[1m$email\\033[m .." + curl --url "${imap_protocol}://${imap_server}" --user "${email}:${passwd}" > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + echo -e "\033[1G [ \033[31m\033[1mWarning\033[m ]: Password for \033[1m$email\033[m is unsafe!" + else + echo -e "\033[1G [ \033[32mOk\033[m ]: Password for '$email' does not match password list." + fi + +done < "$passwd_file" +set -H +echo "" + +exit 0 diff --git a/snippets/tolower.sh b/snippets/tolower.sh new file mode 100755 index 0000000..56eff0c --- /dev/null +++ b/snippets/tolower.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +string="Hallo Welt!" + +echo "" +echo "string: $string" +echo "" + + +# - Bash 4 +# - +echo "______" +echo "Bash 4" +echo " \${string,,}" +echo "" +echo "string: ${string,,}" +echo "" + + +# - tr +# - +echo "______" +echo "tr" +echo " echo \"\$string\" | tr '[:upper:]' '[:lower:]'" +echo "" +echo "string: $(echo "$string" | tr '[:upper:]' '[:lower:]')" +echo "" + + +# - awk +# - +echo "______" +echo "awk" +echo " echo \"\$string\" | awk '{print tolower(\$0)}'" +echo "" +echo "string: $(echo "$string" | awk '{print tolower($0)}')" +echo "" + + +# - sed +# - +echo "______" +echo "sed" +echo " echo \"\$string\" | sed -e 's/\(.*\)/\L\1/'" +echo "" +echo " sed -e 's/\(.*\)/\L\1/' <<< \"\$string\"" +echo "" +echo "string: $(echo "$string" | sed -e 's/\(.*\)/\L\1/')" +echo "string: $(sed -e 's/\(.*\)/\L\1/' <<< "$string")" +echo "" + + +# - Perl +# - +echo "______" +echo "Perl" +echo " echo \"\$string\" | perl -ne 'print lc'" +echo "" +echo "string: $(echo "$string" | perl -ne 'print lc')" +echo "" diff --git a/snippets/toupper.sh b/snippets/toupper.sh new file mode 100755 index 0000000..240a426 --- /dev/null +++ b/snippets/toupper.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +string="Hallo Welt!" + +echo "" +echo "string: $string" +echo "" + + +# - Bash 4 +# - +echo "______" +echo "Bash 4" +echo " \${string^^}" +echo "" +echo "string: ${string^^}" +echo "" + + +# - tr +# - +echo "______" +echo "tr" +echo " echo \"\$string\" | tr '[:lower:]' '[:upper:]'" +echo "" +echo "string: $(echo "$string" | tr '[:lower:]' '[:upper:]')" +echo "" + + +# - awk +# - +echo "______" +echo "awk" +echo " echo \"\$string\" | awk '{print toupper(\$0)}'" +echo "" +echo "string: $(echo "$string" | awk '{print toupper($0)}')" +echo "" + + +# - sed +# - +echo "______" +echo "sed" +echo " echo \"\$string\" | sed -e 's/\(.*\)/\U\1/'" +echo "" +echo " sed -e 's/\(.*\)/\U\1/' <<< \"\$string\"" +echo "" +echo "string: $(echo "$string" | sed -e 's/\(.*\)/\U\1/')" +echo "string: $(sed -e 's/\(.*\)/\U\1/' <<< "$string")" +echo "" + + +# - Perl +# - +echo "______" +echo "Perl" +echo " echo \"\$string\" | perl -ne 'print uc'" +echo "" +echo "string: $(echo "$string" | perl -ne 'print uc')" +echo "" diff --git a/snippets/trim.sh b/snippets/trim.sh new file mode 100755 index 0000000..2d6c55a --- /dev/null +++ b/snippets/trim.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +var=" - Hallo Welt - " + +echo "" +echo " |${var}|" + +var="$(trim $var)" +echo " |${var}|" + +echo "" +exit 0