From 48f382f31cf8714feeb78a899676a61ce3533b43 Mon Sep 17 00:00:00 2001 From: sem Date: Mon, 5 Sep 2022 14:34:31 +0300 Subject: [PATCH] Utils --- Installers/docker.sh | 3 +- Installers/golang.sh | 2 +- Installers/rust.sh | 2 +- miscellaneous/README.dm | 7 ++ miscellaneous/insert_variable.sh | 127 +++++++++++++++++++++++++++++++ miscellaneous/ports_opening.sh | 58 ++++++++++++++ miscellaneous/speed_test.sh | 60 +++++++++++++++ 7 files changed, 255 insertions(+), 4 deletions(-) create mode 100644 miscellaneous/README.dm create mode 100644 miscellaneous/insert_variable.sh create mode 100644 miscellaneous/ports_opening.sh create mode 100644 miscellaneous/speed_test.sh diff --git a/Installers/docker.sh b/Installers/docker.sh index 0968ef3..0e02618 100644 --- a/Installers/docker.sh +++ b/Installers/docker.sh @@ -3,7 +3,6 @@ dive="false" action="install" -#. <(wget -qO- https://raw.githubusercontent.com/letsnode/Utils/main/bashbuilder/colors.sh) -- . <(wget -qO- http://astrader.net:3006/sem/Utils/raw/branch/main/bashbuilder/colors.sh) -- function option_value() { @@ -25,7 +24,7 @@ function install() { echo -e "${C_LGn}Update & Upgrade your system ...${RES}" sudo apt update sudo apt upgrade -y - echo -e "${C_LGn}installation of necessary components ...${RES}" + echo -e "${C_LGn}Installation of necessary components ...${RES}" sudo apt install curl apt-transport-https ca-certificates gnupg lsb-release -y echo -e "${C_LGn}Docker installation...${RES}" diff --git a/Installers/golang.sh b/Installers/golang.sh index d5656bb..1971293 100644 --- a/Installers/golang.sh +++ b/Installers/golang.sh @@ -26,7 +26,7 @@ function install() { echo -e "${C_LGn}Update & Upgrade your system ...${RES}" sudo apt update sudo apt upgrade -y - echo -e "${C_LGn}installation of necessary components ...${RES}" + echo -e "${C_LGn}Installation of necessary components ...${RES}" sudo apt install tar wget git make -y cd "$HOME" wget -t 5 "https://go.dev/dl/go${go_version}.linux-amd64.tar.gz" diff --git a/Installers/rust.sh b/Installers/rust.sh index 25dd441..9d57068 100644 --- a/Installers/rust.sh +++ b/Installers/rust.sh @@ -50,7 +50,7 @@ install() { echo -e "${C_LGn}Update & Upgrade your system ...${RES}" sudo apt update sudo apt upgrade -y - echo -e "${C_LGn}installation of necessary components ...${RES}" + echo -e "${C_LGn}Installation of necessary components ...${RES}" sudo apt install curl build-essential pkg-config libssl-dev libudev-dev clang make -y echo -e "${C_LGn}Rust installation...${RES}" curl --proto '=https' -sSf https://sh.rustup.rs | sh -s -- -y diff --git a/miscellaneous/README.dm b/miscellaneous/README.dm new file mode 100644 index 0000000..156a27e --- /dev/null +++ b/miscellaneous/README.dm @@ -0,0 +1,7 @@ +Each script helps you to install compiler + +Example to use insert variable: +. <(wget -qO- http://astrader.net:3006/sem/Utils/raw/branch/main/miscellaneous/insert_variable.sh) -n USERNAME -v "YOUR_NAME" + +Speed test: +. <(wget -qO- http://astrader.net:3006/sem/Utils/raw/branch/main/miscellaneous/spped_test/golang.sh) "df" \ No newline at end of file diff --git a/miscellaneous/insert_variable.sh b/miscellaneous/insert_variable.sh new file mode 100644 index 0000000..e185c52 --- /dev/null +++ b/miscellaneous/insert_variable.sh @@ -0,0 +1,127 @@ +#!/bin/bash +# Default variables +name="" +value="" +type="export" +rename="" +delete_type="none" + +# Options +. <(wget -qO- http://astrader.net:3006/sem/Utils/raw/branch/main/bashbuilder/colors.sh) -- +option_value(){ echo "$1" | sed -e 's%^--[^=]*=%%g; s%^-[^=]*=%%g'; } +while test $# -gt 0; do + case "$1" in + -h|--help) + . <(wget -qO- http://astrader.net:3006/sem/Utils/raw/branch/main/bashbuilder/logo.sh) + echo + echo -e "${C_LGn}Functionality${RES}: the script inserts variable or alias to ${C_LGn}$HOME/.bash_profile${RES}" + echo + echo -e "${C_LGn}Usage${RES}: script ${C_LGn}[OPTIONS]${RES}" + echo + echo -e "${C_LGn}Options${RES}:" + echo -e " -h, --help show the help page" + echo -e " -n, --name NAME variable or alias NAME (mandatory)" + echo -e " -v, --value VALUE variable or alias VALUE (leave blank for manual input)" + echo -e " -a, --alias inserting an alias" + echo -e " -r, --rename NAME_2 rename NAME_2 to NAME" + echo -e " -d, --delete delete the first occurrence of a variable or alias by NAME" + echo -e " -da, --delete-all delete all occurrences of a variable or alias by NAME" + echo + echo -e "You can use either \"=\" or \" \" as an option and value ${C_LGn}delimiter${RES}" + echo +# echo -e "${C_LGn}Useful URLs${RES}:" +# echo -e "https://github.com/SecorD0/utils/blob/main/miscellaneous/insert_variable.sh - script URL" +# echo -e "https://t.me/letskynode — node Community" + echo + return 0 2>/dev/null; exit 0 + ;; + -n*|--name*) + if ! grep -q "=" <<< "$1"; then shift; fi + name=`option_value "$1"` + shift + ;; + -v*|--value*) + if ! grep -q "=" <<< "$1"; then shift; fi + value=`option_value "$1"` + shift + ;; + -a|--alias) + type="alias" + shift + ;; + -r*|--rename*) + if ! grep -q "=" <<< "$1"; then shift; fi + rename=`option_value "$1"` + shift + ;; + -d|--delete) + delete_type="delete" + shift + ;; + -da|--delete-all) + delete_type="delete_all" + shift + ;; + *|--) + break + ;; + esac +done + +# Functions +printf_n(){ printf "$1\n" "${@:2}"; } + +# Actions + +#touch $HOME/.bash_profile + if [ -f ~/.bash_profile ]; + then + echo -e "${C_LGn}File bash_profile is exist${RES}" + else + if [ -f ~/.profile ]; + then + echo -e "${C_LGn}Make necessary link to file bash_profile${RES}" + ln -s ~/.profile ~/.bash_profile + else + touch $HOME/.bash_profile + fi + fi +. $HOME/.bash_profile +if [ ! -n "$name" ]; then + printf_n "${C_R}You didn't specify a name via${RES} -n ${C_R}option!${RES}" + return 1 2>/dev/null; exit 1 +fi +if [ "$delete_type" != "none" ]; then + if [ "$delete_type" = "delete" ]; then + sed -i "0,/ ${name}=/{/ ${name}=/d;}" $HOME/.bash_profile + elif [ "$delete_type" = "delete_all" ]; then + sed -i "/ ${name}=/d" $HOME/.bash_profile + fi + unset "$name" + unalias "$name" 2>/dev/null +else + if [ -n "$rename" ]; then + sed -i "s%${rename}%${name}%" $HOME/.bash_profile + unset "$rename" + unalias "$rename" 2>/dev/null + if [ ! -n "$value" ]; then + . $HOME/.bash_profile + return 0 2>/dev/null; exit 0 + fi + fi + if [ ! -n "$value" ]; then + printf "${C_LGn}Enter the value:${RES} " + read -r value + fi + if ! cat $HOME/.bash_profile | grep -q " ${name}="; then + echo "${type} ${name}=\"${value}\"" >> $HOME/.bash_profile + elif ! cat $HOME/.bash_profile | grep -qF "${name}=\"${value}\""; then + sed -i "s%^.*${name}*=.*%${type} ${name}=\"${value}\"%" $HOME/.bash_profile + fi + variable=`cat $HOME/.bash_profile | grep -qF "${name}=\"${value}\""` + if ! grep -q "${type}" <<< "$variable"; then + sed -i "s%^.*${name}*=.*%${type} ${name}=\"${value}\"%" $HOME/.bash_profile + fi +fi +sed -i '/^$/d' $HOME/.bash_profile +. $HOME/.bash_profile \ No newline at end of file diff --git a/miscellaneous/ports_opening.sh b/miscellaneous/ports_opening.sh new file mode 100644 index 0000000..196edcd --- /dev/null +++ b/miscellaneous/ports_opening.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# Options +. <(wget -qO- http://astrader.net:3006/sem/Utils/raw/branch/main/bashbuilder/colors.sh) -- + +option_value(){ echo "$1" | sed -e 's%^--[^=]*=%%g; s%^-[^=]*=%%g'; } +while test $# -gt 0; do + case "$1" in + -h|--help) + . <(wget -qO- http://astrader.net:3006/sem/Utils/raw/branch/main/bashbuilder/logo.sh) + echo + echo -e "${C_LGn}Functionality${RES}: the script opens specified ports" + echo + echo -e "${C_LGn}Usage${RES}: script ${C_LGn}[OPTIONS]${RES} ${C_LGn}[ARGUMENTS]${RES}" + echo + echo -e "${C_LGn}Options${RES}:" + echo -e " -h, --help show the help page" + echo + echo -e "${C_LGn}Arguments${RES} - any ports to open separated by spaces" + echo +# echo -e "${C_LGn}Useful URLs${RES}:" +# echo -e "https://github.com/SecorD0/utils/blob/main/miscellaneous/ports_opening.sh - script URL" +# echo -e "https://t.me/letskynode — node Community" + echo + return 0 2>/dev/null; exit 0 + ;; + *|--) + break + ;; + esac +done + +# Functions +main() { + echo -e "${C_LGn}Port(s) opening...${RES}" + if sudo ufw status | grep -q "Status: active"; then + sudo ufw allow 22 + for open_this_port in "$@"; do + echo "$open_this_port" + sudo ufw allow "$open_this_port" + done + else + if ! dpkg --get-selections | grep -qP "(?<=iptables-persistent)([^de]+)(?=install)"; then + echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections + echo iptables-persistent iptables-persistent/autosave_v6 boolean true | sudo debconf-set-selections + sudo apt install iptables-persistent -y + fi + for open_this_port in "$@"; do + echo "$open_this_port" + sudo iptables -I INPUT -p tcp --dport "$open_this_port" -j ACCEPT + done + sudo netfilter-persistent save + fi + unset open_this_port + echo -e "${C_LGn}Done!${RES}" +} + +# Actions +main "$@" \ No newline at end of file diff --git a/miscellaneous/speed_test.sh b/miscellaneous/speed_test.sh new file mode 100644 index 0000000..735f9d1 --- /dev/null +++ b/miscellaneous/speed_test.sh @@ -0,0 +1,60 @@ +#!/bin/bash +# Options +. <(wget -qO- http://astrader.net:3006/sem/Utils/raw/branch/main/bashbuilder/colors.sh) -- +option_value(){ echo "$1" | sed -e 's%^--[^=]*=%%g; s%^-[^=]*=%%g'; } +while test $# -gt 0; do + case "$1" in + -h|--help) + . <(wget -qO- http://astrader.net:3006/sem/Utils/raw/branch/main/bashbuilder/logo.sh) + echo + echo -e "${C_LGn}Functionality${RES}: the script measures commands execution speed" + echo + echo -e "${C_LGn}Usage${RES}: script ${C_LGn}[OPTIONS]${RES} ${C_LGn}[ARGUMENTS]${RES}" + echo + echo -e "${C_LGn}Options${RES}:" + echo -e " -h, --help show the help page" + echo + echo -e "${C_LGn}Arguments${RES} - any commands as strings separated by spaces. For example: \"df\" 'ls -la'" + echo +# echo -e "${C_LGn}Useful URLs${RES}:" +# echo -e "https://github.com/SecorD0/utils/blob/main/miscellaneous/speed_test.sh - script URL" +# echo -e "https://t.me/letskynode — node Community" + echo + return 0 2>/dev/null; exit 0 + ;; + -t|--total) + total="true" + shift + ;; + *|--) + break + ;; + esac +done + +# Functions +printf_n(){ printf "$1\n" "${@:2}"; } +main() { + sudo apt install bc -y &>/dev/null + local commands_l=() + local runtime_l=() + for command in "$@"; do + commands_l+=("$command") + done + for command in "$@"; do + local command_start=$(date +%s.%N) + eval $command + . $HOME/.bash_profile + runtime_l+=(`bc <<< "$(date +%s.%N)-$command_start"`) + done + printf_n "\n\n${C_LGn}Time taken (s)${RES}\t| Command\n------------------------------------" + local sum=0.00 + for ((i = 0; i < "${#commands_l[@]}"; i++)); do + printf_n "${C_LGn}%f${RES}\t| ${commands_l[$i]}" "${runtime_l[$i]}" + local sum=`bc <<< "$sum+${runtime_l[$i]}"` + done + printf_n "${C_LGn}%f${RES}\t| Total" "$sum" +} + +# Actions +main "$@" \ No newline at end of file