diff --git a/.change_network_gateway.sh.swp b/.change_network_gateway.sh.swp new file mode 100644 index 0000000..576e96c Binary files /dev/null and b/.change_network_gateway.sh.swp differ diff --git a/change_network_gateway.sh b/change_network_gateway.sh index 8cd43b8..56faa44 100755 --- a/change_network_gateway.sh +++ b/change_network_gateway.sh @@ -1,11 +1,32 @@ #!/usr/bin/env bash +# ============= +# --- Some Definitions +# ============= base_dir=/root/Office_Networks chown -R root:root $base_dir logfile=$(mktemp) + +# --- +# --- DON'T MAKE CHANGES AFTER THIS LINE +# --- + + +# ============= +# --- Some functions +# ============= + +# - Is this script running on terminal ? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + function usage() { if [ -n "$1" ];then echo -e "\nError: $1" @@ -44,7 +65,7 @@ Usage: `basename $0` Notice: - !! To change the network, you have to chroot into /ro !! + !! If you are on a readonly system, chroot into /ro !! remountrw rebind on @@ -95,15 +116,27 @@ info (){ echo_OK() { - echo -en "\\033[45G[ \\033[1;32mOK\\033[0;39m ]\n" + if $terminal ; then + echo -en "\\033[45G[ \\033[1;32mOK\\033[0;39m ]\n" + else + echo " .. [ OK ]" + fi } echo_FAILED() { - echo -en "\\033[45G[ \\033[1;31mFAIL\\033[0;39m ]\n" + if $terminal ; then + echo -en "\\033[45G[ \\033[1;31mFAIL\\033[0;39m ]\n" + else + echo " .. [ FAIL ]" + fi } echo_SKIPPED() { - echo -en "\\033[45G[ \\033[37mSkipped\\033[0;39m ]\n" + if $terminal ; then + echo -en "\033[45G[ \033[37mSKIP\033[m ]\n" + else + echo " .. [ SKIP ]" + fi } [ $# -ne "1" ] && usage "wrong number of arguments" @@ -377,20 +410,50 @@ else fi -# - /usr/local/src directory +# - /root/bin directory # - +src_dir=${base_dir}/${network}/bin +dst_dir=$(realpath /root/bin) echo -en "\t/root/bin directory" -if [[ -d "${base_dir}/${network}/bin" ]]; then - if [[ ! -d "/root/bin" ]]; then - mkdir /root/bin > $logfile 2>&1 +omitted=false +msg="" +if [[ -d "$src_dir" ]]; then + if [[ ! -d "$dst_dir" ]]; then + mkdir $dst_dir > $logfile 2>&1 fi - cp -a ${base_dir}/${network}/bin/* /root/bin/ > $logfile 2>&1 + + # - Delete all existing files/directories of $dst_dir, but + # - ommit directory, where this script is running.. + # - + while IFS='' read -r -d '' file ; do + if [[ "$(basename $file)" = "$(basename $(realpath $(dirname $0)))" ]]; then + omitted=true + msg="Existing Directory '$(basename $file)' was not deleted, because this script is running there!" + continue + fi + rm -r $file + done < <(find "$dst_dir" -mindepth 1 -maxdepth 1 -print0) + while IFS='' read -r -d '' file ; do + if [[ "$(realpath $(dirname $0))" = "${dst_dir}/$(basename $file)" ]] ; then + omitted=true + if [[ -n "$msg" ]];then + msg="$msg\n\t New Directory '$(basename $file)' was also not copied." + continue + else + msg="Directory '$(basename $file)' was omitted, because this script is running there!" + fi + fi + cp -a ${file} ${dst_dir}/ > $logfile 2>&1 + done < <(find "$src_dir" -mindepth 1 -maxdepth 1 -print0) if ! $_rval ; then echo_FAILED error $(cat $logfile) else echo_OK fi + if $omitted ; then + info "$msg" + fi else echo_SKIPPED fi @@ -683,7 +746,6 @@ echo -en "\tSetting up rc.local.." if [ "$network" = "NONE-CKUBU" -o "$network" = "NONE-WF" -o "$network" = "GA-Ersatz" ];then cp -a ${base_dir}/rc.local.NONE /etc/rc.local > $logfile 2>&1 else - #cp -a ${base_dir}/rc.local /etc/rc.local cp -a ${base_dir}/${network}/rc.local.${network} /etc/rc.local > $logfile 2>&1 fi if [ "$?" != "0" ]; then @@ -693,28 +755,24 @@ else echo_OK fi -if $_add_support_if ; then - echo -e " After restarting, you can reach that device" - echo -e " at ip-address: \\033[1;33m172.16.1.1\\033[0;39m" -fi - +_error=false if [ "$(stat -c %d:%i /)" = "$(stat -c %d:%i /proc/1/root/.)" ]; then - warn "This is NOT a chrooted environment. Maybe thats ok. \n\n\t\033[1;37mIf this is a readonly system, take care, you are chrooted into /ro.\n\tOtherwise changes are not persistent.\033[m" + if [[ -d "/ro" ]] ; then + error "It seems, this is a readonly system and you are not chrooted.\n\n\t \033[1;37mChanges made by this script are not persistent!!\033[m\n\n\t Change root to directory '/ro' (\033[1;37mchroot /ro /bin/bash\033[m) and\n\t run this script again.\033[m" + #info "Change root to directory '/ro' (\033[1;37mchroot /ro /bin/bash\033[m) and run this script again.\033[m" + fi + + _error=true - #echo "" - #echo "" - #echo -e " \\033[1;31m Take care, you are chrooted into /ro. Otherwise" - #echo -e " changes are not persistent \\033[0;39m !!" - #echo "" - #echo "" else + info "Programm was running in a chrooted environment.\n\n\t\033[1;37mExit from chroot environment and restart to make changes active..\033[m" - #echo "" - #echo "" - #echo -e " \\033[1;33mExit from chroot environment and restart to make changes active..\\033[0;39m" - #echo "" +fi + +if $_add_support_if i&& ! $_error ; then + info "After restarting, you can reach this machine\n\t at ip-address: \033[1;33m172.16.1.1\033[m" fi exit 0