From e2f211c67890477fe8a10d88d94392a4058425e1 Mon Sep 17 00:00:00 2001 From: hwdsl2 Date: Wed, 10 Aug 2022 22:41:55 -0500 Subject: [PATCH] Improve OS detection - Improve OS detection and clean up --- extras/ikev2changeaddr.sh | 48 ++++++++----------------------------- extras/ikev2setup.sh | 38 ++++++++++++++++------------- extras/quickstart.sh | 38 ++++++++++++++++------------- extras/vpnuninstall.sh | 20 +++++++++------- extras/vpnupgrade.sh | 38 ++++++++++++++++------------- extras/vpnupgrade_centos.sh | 40 +++++++++++++++++-------------- vpnsetup.sh | 38 ++++++++++++++++------------- vpnsetup_centos.sh | 38 ++++++++++++++++------------- 8 files changed, 148 insertions(+), 150 deletions(-) diff --git a/extras/ikev2changeaddr.sh b/extras/ikev2changeaddr.sh index f90d428..bc27af2 100755 --- a/extras/ikev2changeaddr.sh +++ b/extras/ikev2changeaddr.sh @@ -36,44 +36,16 @@ check_root() { } check_os() { - os_type=centos - rh_file="/etc/redhat-release" - if grep -qs "Red Hat" "$rh_file"; then - os_type=rhel - fi - [ -f /etc/oracle-release ] && os_type=ol - if grep -qs "release 7" "$rh_file" || grep -qs "release 8" "$rh_file" \ - || grep -qs "release 9" "$rh_file"; then - grep -qs -i rocky "$rh_file" && os_type=rocky - grep -qs -i alma "$rh_file" && os_type=alma - elif grep -qs "Amazon Linux release 2" /etc/system-release; then - os_type=amzn - else - os_type=$(lsb_release -si 2>/dev/null) - [ -z "$os_type" ] && [ -f /etc/os-release ] && os_type=$(. /etc/os-release && printf '%s' "$ID") - case $os_type in - [Uu]buntu) - os_type=ubuntu - ;; - [Dd]ebian) - os_type=debian - ;; - [Rr]aspbian) - os_type=raspbian - ;; - [Aa]lpine) - os_type=alpine - ;; - *) -cat 1>&2 <<'EOF' -Error: This script only supports one of the following OS: - Ubuntu, Debian, CentOS/RHEL, Rocky Linux, AlmaLinux, - Oracle Linux, Amazon Linux 2 or Alpine Linux -EOF - exit 1 - ;; - esac - fi + os_type=$(lsb_release -si 2>/dev/null) + [ -z "$os_type" ] && [ -f /etc/os-release ] && os_type=$(. /etc/os-release && printf '%s' "$ID") + case $os_type in + [Aa]lpine) + os_type=alpine + ;; + *) + os_type=other + ;; + esac } check_libreswan() { diff --git a/extras/ikev2setup.sh b/extras/ikev2setup.sh index ee17ba1..c90772d 100755 --- a/extras/ikev2setup.sh +++ b/extras/ikev2setup.sh @@ -46,23 +46,26 @@ check_container() { } check_os() { - os_type=centos - os_arch=$(uname -m | tr -dc 'A-Za-z0-9_-') rh_file="/etc/redhat-release" - if grep -qs "Red Hat" "$rh_file"; then - os_type=rhel - fi - [ -f /etc/oracle-release ] && os_type=ol - grep -qs -i rocky "$rh_file" && os_type=rocky - grep -qs -i alma "$rh_file" && os_type=alma - if grep -qs "release 7" "$rh_file"; then - os_ver=7 - elif grep -qs "release 8" "$rh_file"; then - os_ver=8 - grep -qi stream "$rh_file" && os_ver=8s - elif grep -qs "release 9" "$rh_file"; then - os_ver=9 - grep -qi stream "$rh_file" && os_ver=9s + if [ -f "$rh_file" ]; then + os_type=centos + if grep -q "Red Hat" "$rh_file"; then + os_type=rhel + fi + [ -f /etc/oracle-release ] && os_type=ol + grep -qi rocky "$rh_file" && os_type=rocky + grep -qi alma "$rh_file" && os_type=alma + if grep -q "release 7" "$rh_file"; then + os_ver=7 + elif grep -q "release 8" "$rh_file"; then + os_ver=8 + grep -qi stream "$rh_file" && os_ver=8s + elif grep -q "release 9" "$rh_file"; then + os_ver=9 + grep -qi stream "$rh_file" && os_ver=9s + else + exiterr "This script only supports CentOS/RHEL 7-9." + fi elif grep -qs "Amazon Linux release 2" /etc/system-release; then os_type=amzn os_ver=2 @@ -154,7 +157,7 @@ confirm_or_abort() { show_header() { cat <<'EOF' -IKEv2 Script Copyright (c) 2020-2022 Lin Song 9 Aug 2022 +IKEv2 Script Copyright (c) 2020-2022 Lin Song 10 Aug 2022 EOF } @@ -1151,6 +1154,7 @@ EOF } apply_ubuntu1804_nss_fix() { + os_arch=$(uname -m | tr -dc 'A-Za-z0-9_-') if [ "$os_type" = "ubuntu" ] && [ "$os_ver" = "bustersid" ] && [ "$os_arch" = "x86_64" ]; then nss_url1="https://mirrors.kernel.org/ubuntu/pool/main/n/nss" nss_url2="https://mirrors.kernel.org/ubuntu/pool/universe/n/nss" diff --git a/extras/quickstart.sh b/extras/quickstart.sh index 4957f85..93c91c4 100755 --- a/extras/quickstart.sh +++ b/extras/quickstart.sh @@ -69,25 +69,29 @@ EOF } check_os() { - os_type=centos rh_file="/etc/redhat-release" - if grep -qs "Red Hat" "$rh_file"; then - os_type=rhel - fi - [ -f /etc/oracle-release ] && os_type=ol - grep -qs -i rocky "$rh_file" && os_type=rocky - grep -qs -i alma "$rh_file" && os_type=alma - if grep -qs "release 7" "$rh_file"; then - os_ver=7 - elif grep -qs "release 8" "$rh_file"; then - os_ver=8 - grep -qi stream "$rh_file" && os_ver=8s - if [ "$os_type" = "centos" ] && [ "$os_ver" = "8" ]; then - exiterr "CentOS Linux 8 is EOL and not supported." + if [ -f "$rh_file" ]; then + os_type=centos + if grep -q "Red Hat" "$rh_file"; then + os_type=rhel + fi + [ -f /etc/oracle-release ] && os_type=ol + grep -qi rocky "$rh_file" && os_type=rocky + grep -qi alma "$rh_file" && os_type=alma + if grep -q "release 7" "$rh_file"; then + os_ver=7 + elif grep -q "release 8" "$rh_file"; then + os_ver=8 + grep -qi stream "$rh_file" && os_ver=8s + if [ "$os_type$os_ver" = "centos8" ]; then + exiterr "CentOS Linux 8 is EOL and not supported." + fi + elif grep -q "release 9" "$rh_file"; then + os_ver=9 + grep -qi stream "$rh_file" && os_ver=9s + else + exiterr "This script only supports CentOS/RHEL 7-9." fi - elif grep -qs "release 9" "$rh_file"; then - os_ver=9 - grep -qi stream "$rh_file" && os_ver=9s elif grep -qs "Amazon Linux release 2" /etc/system-release; then os_type=amzn os_ver=2 diff --git a/extras/vpnuninstall.sh b/extras/vpnuninstall.sh index e09bc20..356accd 100755 --- a/extras/vpnuninstall.sh +++ b/extras/vpnuninstall.sh @@ -34,16 +34,18 @@ check_root() { } check_os() { - os_type=centos rh_file="/etc/redhat-release" - if grep -qs "Red Hat" "$rh_file"; then - os_type=rhel - fi - [ -f /etc/oracle-release ] && os_type=ol - if grep -qs "release 7" "$rh_file" || grep -qs "release 8" "$rh_file" \ - || grep -qs "release 9" "$rh_file"; then - grep -qs -i rocky "$rh_file" && os_type=rocky - grep -qs -i alma "$rh_file" && os_type=alma + if [ -f "$rh_file" ]; then + os_type=centos + if grep -q "Red Hat" "$rh_file"; then + os_type=rhel + fi + [ -f /etc/oracle-release ] && os_type=ol + grep -qi rocky "$rh_file" && os_type=rocky + grep -qi alma "$rh_file" && os_type=alma + if ! grep -q -E "release (7|8|9)" "$rh_file"; then + exiterr "This script only supports CentOS/RHEL 7-9." + fi elif grep -qs "Amazon Linux release 2" /etc/system-release; then os_type=amzn else diff --git a/extras/vpnupgrade.sh b/extras/vpnupgrade.sh index 4ffca6d..3fff58b 100755 --- a/extras/vpnupgrade.sh +++ b/extras/vpnupgrade.sh @@ -37,25 +37,29 @@ check_vz() { } check_os() { - os_type=centos rh_file="/etc/redhat-release" - if grep -qs "Red Hat" "$rh_file"; then - os_type=rhel - fi - [ -f /etc/oracle-release ] && os_type=ol - grep -qs -i rocky "$rh_file" && os_type=rocky - grep -qs -i alma "$rh_file" && os_type=alma - if grep -qs "release 7" "$rh_file"; then - os_ver=7 - elif grep -qs "release 8" "$rh_file"; then - os_ver=8 - grep -qi stream "$rh_file" && os_ver=8s - if [ "$os_type" = "centos" ] && [ "$os_ver" = "8" ]; then - exiterr "CentOS Linux 8 is EOL and not supported." + if [ -f "$rh_file" ]; then + os_type=centos + if grep -q "Red Hat" "$rh_file"; then + os_type=rhel + fi + [ -f /etc/oracle-release ] && os_type=ol + grep -qi rocky "$rh_file" && os_type=rocky + grep -qi alma "$rh_file" && os_type=alma + if grep -q "release 7" "$rh_file"; then + os_ver=7 + elif grep -q "release 8" "$rh_file"; then + os_ver=8 + grep -qi stream "$rh_file" && os_ver=8s + if [ "$os_type$os_ver" = "centos8" ]; then + exiterr "CentOS Linux 8 is EOL and not supported." + fi + elif grep -q "release 9" "$rh_file"; then + os_ver=9 + grep -qi stream "$rh_file" && os_ver=9s + else + exiterr "This script only supports CentOS/RHEL 7-9." fi - elif grep -qs "release 9" "$rh_file"; then - os_ver=9 - grep -qi stream "$rh_file" && os_ver=9s elif grep -qs "Amazon Linux release 2" /etc/system-release; then os_type=amzn os_ver=2 diff --git a/extras/vpnupgrade_centos.sh b/extras/vpnupgrade_centos.sh index 2b95a59..8c5c636 100755 --- a/extras/vpnupgrade_centos.sh +++ b/extras/vpnupgrade_centos.sh @@ -40,25 +40,29 @@ check_vz() { } check_os() { - os_type=centos rh_file="/etc/redhat-release" - if grep -qs "Red Hat" "$rh_file"; then - os_type=rhel - fi - [ -f /etc/oracle-release ] && os_type=ol - grep -qs -i rocky "$rh_file" && os_type=rocky - grep -qs -i alma "$rh_file" && os_type=alma - if grep -qs "release 7" "$rh_file"; then - os_ver=7 - elif grep -qs "release 8" "$rh_file"; then - os_ver=8 - grep -qi stream "$rh_file" && os_ver=8s - if [ "$os_type" = "centos" ] && [ "$os_ver" = "8" ]; then - exiterr "CentOS Linux 8 is EOL and not supported." + if [ -f "$rh_file" ]; then + os_type=centos + if grep -q "Red Hat" "$rh_file"; then + os_type=rhel + fi + [ -f /etc/oracle-release ] && os_type=ol + grep -qi rocky "$rh_file" && os_type=rocky + grep -qi alma "$rh_file" && os_type=alma + if grep -q "release 7" "$rh_file"; then + os_ver=7 + elif grep -q "release 8" "$rh_file"; then + os_ver=8 + grep -qi stream "$rh_file" && os_ver=8s + if [ "$os_type$os_ver" = "centos8" ]; then + exiterr "CentOS Linux 8 is EOL and not supported." + fi + elif grep -q "release 9" "$rh_file"; then + os_ver=9 + grep -qi stream "$rh_file" && os_ver=9s + else + exiterr "This script only supports CentOS/RHEL 7-9." fi - elif grep -qs "release 9" "$rh_file"; then - os_ver=9 - grep -qi stream "$rh_file" && os_ver=9s else cat 1>&2 <<'EOF' Error: This script only supports one of the following OS: @@ -166,7 +170,7 @@ install_pkgs_2() { erp="--enablerepo" rp1="$erp=*server-*optional*" rp2="$erp=*releases-optional*" - if [ "$os_type" = "ol" ] && [ "$os_ver" = "7" ]; then + if [ "$os_type$os_ver" = "ol7" ]; then rp2="$erp=ol7_optional_latest" fi if [ "$os_ver" = "7" ]; then diff --git a/vpnsetup.sh b/vpnsetup.sh index 4957f85..93c91c4 100755 --- a/vpnsetup.sh +++ b/vpnsetup.sh @@ -69,25 +69,29 @@ EOF } check_os() { - os_type=centos rh_file="/etc/redhat-release" - if grep -qs "Red Hat" "$rh_file"; then - os_type=rhel - fi - [ -f /etc/oracle-release ] && os_type=ol - grep -qs -i rocky "$rh_file" && os_type=rocky - grep -qs -i alma "$rh_file" && os_type=alma - if grep -qs "release 7" "$rh_file"; then - os_ver=7 - elif grep -qs "release 8" "$rh_file"; then - os_ver=8 - grep -qi stream "$rh_file" && os_ver=8s - if [ "$os_type" = "centos" ] && [ "$os_ver" = "8" ]; then - exiterr "CentOS Linux 8 is EOL and not supported." + if [ -f "$rh_file" ]; then + os_type=centos + if grep -q "Red Hat" "$rh_file"; then + os_type=rhel + fi + [ -f /etc/oracle-release ] && os_type=ol + grep -qi rocky "$rh_file" && os_type=rocky + grep -qi alma "$rh_file" && os_type=alma + if grep -q "release 7" "$rh_file"; then + os_ver=7 + elif grep -q "release 8" "$rh_file"; then + os_ver=8 + grep -qi stream "$rh_file" && os_ver=8s + if [ "$os_type$os_ver" = "centos8" ]; then + exiterr "CentOS Linux 8 is EOL and not supported." + fi + elif grep -q "release 9" "$rh_file"; then + os_ver=9 + grep -qi stream "$rh_file" && os_ver=9s + else + exiterr "This script only supports CentOS/RHEL 7-9." fi - elif grep -qs "release 9" "$rh_file"; then - os_ver=9 - grep -qi stream "$rh_file" && os_ver=9s elif grep -qs "Amazon Linux release 2" /etc/system-release; then os_type=amzn os_ver=2 diff --git a/vpnsetup_centos.sh b/vpnsetup_centos.sh index 51c777e..b32f5c9 100755 --- a/vpnsetup_centos.sh +++ b/vpnsetup_centos.sh @@ -74,25 +74,29 @@ EOF } check_os() { - os_type=centos rh_file="/etc/redhat-release" - if grep -qs "Red Hat" "$rh_file"; then - os_type=rhel - fi - [ -f /etc/oracle-release ] && os_type=ol - grep -qs -i rocky "$rh_file" && os_type=rocky - grep -qs -i alma "$rh_file" && os_type=alma - if grep -qs "release 7" "$rh_file"; then - os_ver=7 - elif grep -qs "release 8" "$rh_file"; then - os_ver=8 - grep -qi stream "$rh_file" && os_ver=8s - if [ "$os_type" = "centos" ] && [ "$os_ver" = "8" ]; then - exiterr "CentOS Linux 8 is EOL and not supported." + if [ -f "$rh_file" ]; then + os_type=centos + if grep -q "Red Hat" "$rh_file"; then + os_type=rhel + fi + [ -f /etc/oracle-release ] && os_type=ol + grep -qi rocky "$rh_file" && os_type=rocky + grep -qi alma "$rh_file" && os_type=alma + if grep -q "release 7" "$rh_file"; then + os_ver=7 + elif grep -q "release 8" "$rh_file"; then + os_ver=8 + grep -qi stream "$rh_file" && os_ver=8s + if [ "$os_type$os_ver" = "centos8" ]; then + exiterr "CentOS Linux 8 is EOL and not supported." + fi + elif grep -q "release 9" "$rh_file"; then + os_ver=9 + grep -qi stream "$rh_file" && os_ver=9s + else + exiterr "This script only supports CentOS/RHEL 7-9." fi - elif grep -qs "release 9" "$rh_file"; then - os_ver=9 - grep -qi stream "$rh_file" && os_ver=9s else cat 1>&2 <<'EOF' Error: This script only supports one of the following OS: