From e35fb02bb063f5a604ab329b830b4e326ad835e5 Mon Sep 17 00:00:00 2001 From: lework Date: Mon, 2 Sep 2019 18:20:10 +0800 Subject: [PATCH] feat: add file --- shell/ip.sh | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ shell/log.sh | 18 +++++++++ 2 files changed, 119 insertions(+) create mode 100644 shell/ip.sh create mode 100644 shell/log.sh diff --git a/shell/ip.sh b/shell/ip.sh new file mode 100644 index 0000000..20f8581 --- /dev/null +++ b/shell/ip.sh @@ -0,0 +1,101 @@ +#!/bin/bash + + +# converts IPv4 as "A.B.C.D" to integer +ip4_to_int() { + IFS=. read -r i j k l <> 24) % 256 )).$(( ($1 >> 16) % 256 )).$(( ($1 >> 8) % 256 )).$(( $1 % 256 ))" +} + +# returns the ip part of an CIDR +cidr_ip() { + IFS=/ read -r ip _ < 3232235521 + +int_to_ip4 3232235521 +# => 192.168.0.1 + + +# network address +ip=$(ip4_to_int 172.16.10.20) +netmask=$(ip4_to_int 255.255.252.0) +int_to_ip4 $((ip & netmask)) +# => 172.16.8.0 + + +# broadcast address +ip=$(ip4_to_int 172.16.10.20) +netmask=$(ip4_to_int 255.255.252.0) +int_to_ip4 $(((ip & netmask) + 1)) +# => 172.16.8.1 + + +cidr_ip "172.16.0.10/22" +# => 172.16.0.10 + +cidr_prefix "172.16.0.10/22" +# => 22 + +netmask_of_prefix 8 +# => 4278190080 + + +cidr_default_gw 192.168.10.1/24 +# => 192.168.10.1 +cidr_default_gw 192.168.10.1/16 +# => 192.168.0.1 +cidr_default_gw 172.17.18.19/20 +# => 172.17.16.1 + + +cidr_default_gw_2 192.168.10.1/24 +# => 192.168.10.254 +cidr_default_gw_2 192.168.10.1/16 +# => 192.168.255.254 +cidr_default_gw_2 172.17.18.19/20 +# => 172.17.31.254 \ No newline at end of file diff --git a/shell/log.sh b/shell/log.sh new file mode 100644 index 0000000..4ab2184 --- /dev/null +++ b/shell/log.sh @@ -0,0 +1,18 @@ +#!/bin/bash + + +LOGFILE=log.log +RETAIN_NUM_LINES=10 + +function logsetup { + TMP=$(tail -n $RETAIN_NUM_LINES $LOGFILE 2>/dev/null) && echo "${TMP}" > $LOGFILE + exec > >(tee -a $LOGFILE) + exec 2>&1 +} + +function log { + echo "[$(date --rfc-3339=seconds)]: $*" +} + +logsetup +log hello this is a log \ No newline at end of file