diff --git a/IPv6-disable.sh b/IPv6-disable.sh new file mode 100755 index 0000000..b5a6f70 --- /dev/null +++ b/IPv6-disable.sh @@ -0,0 +1,243 @@ +#!/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" + + + +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 + blank_line + 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_warn() { + if $terminal ; then + echo -e "\033[75G[ \033[33mwarn\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[90m\033[1mskipped\033[m ]" + fi +} +echo_wait(){ + if $terminal ; then + echo -en "\033[75G[ \033[5m\033[1m...\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" + + +# ---------- +# - Some checks .. +# ---------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + + +# ---------- +# - 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" + echo "" +fi + + +echononl "Disable IPv6 on this machine.." + +output="$(ip -6 addr 2> $log_file)" +if [[ $? -ne 0 ]] ; then + echo_failed + error "Executing command 'ip -6 addr' failed!\n\nThe Error was:\n$(cat "$log_file")" +elif [[ -z "$output" ]] ; then + echo_skipped + warn "IPv6 is already disabled." +else + sysctl -w net.ipv6.conf.all.disable_ipv6=1 > /dev/null 2> $log_file + if [[ $? -ne 0 ]] ; then + echo_failed + error "Cannot set kernel parameter 'net.ipv6.conf.all.disable_ipv6'\n\nThe Error was:\n$(cat "$log_file")" + clean_up 1 + fi + + sysctl -w net.ipv6.conf.default.disable_ipv6=1 > /dev/null 2> $log_file + if [[ $? -ne 0 ]] ; then + echo_failed + error "Cannot set kernel parameter 'net.ipv6.conf.default.disable_ipv6'\n\nThe Error was:\n$(cat "$log_file")" + clean_up 1 + fi + + sysctl -w net.ipv6.conf.lo.disable_ipv6=1 > /dev/null 2> $log_file + if [[ $? -ne 0 ]] ; then + echo_failed + error "Cannot set kernel parameter 'net.ipv6.conf.default.disable_ipv6'\n\nThe Error was:\n$(cat "$log_file")" + clean_up 1 + fi + echo_ok +fi + +# ---------- +# One method to make this option persist is modifying /etc/sysctl.conf. +# +# net.ipv6.conf.all.disable_ipv6=0 +# net.ipv6.conf.default.disable_ipv6=0 +# net.ipv6.conf.lo.disable_ipv6=0 +# +# For the settings to take effect use: +# +# sudo sysctl -p +# +# If IPv6 is still enabled after rebooting, you must create (with root +# privileges) the file /etc/rc.local and fill it with: +# +# #!/bin/bash +# # /etc/rc.local +# +# /etc/sysctl.d +# /etc/init.d/procps restart +# +# exit 0 +# +# Now use chmod command to make the file executable: +# +# chmod 755 /etc/rc.local +# ---------- + +clean_up 0 diff --git a/IPv6-enable.sh b/IPv6-enable.sh new file mode 100755 index 0000000..71b0754 --- /dev/null +++ b/IPv6-enable.sh @@ -0,0 +1,243 @@ +#!/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" + + + +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 + blank_line + 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_warn() { + if $terminal ; then + echo -e "\033[75G[ \033[33mwarn\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[90m\033[1mskipped\033[m ]" + fi +} +echo_wait(){ + if $terminal ; then + echo -en "\033[75G[ \033[5m\033[1m...\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" + + +# ---------- +# - Some checks .. +# ---------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + + +# ---------- +# - 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" + echo "" +fi + + +echononl "Enable IPv6 on this machine.." + +output="$(ip -6 addr 2> $log_file)" +if [[ $? -ne 0 ]] ; then + echo_failed + error "Executing command 'ip -6 addr' failed!\n\nThe Error was:\n$(cat "$log_file")" +elif [[ -n "$output" ]] ; then + echo_skipped + warn "IPv6 is already enabled." +else + sysctl -w net.ipv6.conf.all.disable_ipv6=0 > /dev/null 2> $log_file + if [[ $? -ne 0 ]] ; then + echo_failed + error "Cannot set kernel parameter 'net.ipv6.conf.all.disable_ipv6'\n\nThe Error was:\n$(cat "$log_file")" + clean_up 1 + fi + + sysctl -w net.ipv6.conf.default.disable_ipv6=0 > /dev/null 2> $log_file + if [[ $? -ne 0 ]] ; then + echo_failed + error "Cannot set kernel parameter 'net.ipv6.conf.default.disable_ipv6'\n\nThe Error was:\n$(cat "$log_file")" + clean_up 1 + fi + + sysctl -w net.ipv6.conf.lo.disable_ipv6=0 > /dev/null 2> $log_file + if [[ $? -ne 0 ]] ; then + echo_failed + error "Cannot set kernel parameter 'net.ipv6.conf.default.disable_ipv6'\n\nThe Error was:\n$(cat "$log_file")" + clean_up 1 + fi + echo_ok +fi + +# ---------- +# One method to make this option persist is modifying /etc/sysctl.conf. +# +# net.ipv6.conf.all.disable_ipv6=0 +# net.ipv6.conf.default.disable_ipv6=0 +# net.ipv6.conf.lo.disable_ipv6=0 +# +# For the settings to take effect use: +# +# sudo sysctl -p +# +# If IPv6 is still enabled after rebooting, you must create (with root +# privileges) the file /etc/rc.local and fill it with: +# +# #!/bin/bash +# # /etc/rc.local +# +# /etc/sysctl.d +# /etc/init.d/procps restart +# +# exit 0 +# +# Now use chmod command to make the file executable: +# +# chmod 755 /etc/rc.local +# ---------- + +clean_up 0