UsiScript
check the web connection
Code
############################### # 2 Functions # check_dns_resolution = check if your system can resolved the hostname (dns check) # check_web_connection = check if your system can connection on the Web/Internet #function use to check if your system can resolved the hostname (dns check) function check_dns_resolution { NOK=0 if [ "$1" != "" ]; then varHOSTTOTEST=$1 else varHOSTTOTEST="" fi varDNSFILE="/etc/resolv.conf" varNSSWITCH="/etc/nsswitch.conf" varMYDNSSRV="" varDNSHOST="" varRESULTDNS="" varERROR=0 if [ "$varHOSTTOTEST" = "" ]; then echo "No hostname to test" >&2 varERROR=$(expr "$varERROR" + "1") else if [ -f $varNSSWITCH ]; then grep "hosts:" $varNSSWITCH|grep "dns">/dev/null if [ $? -gt 0 ]; then echo "Your OS doesn't use dns server to resolved hostname. (info seen on $varNSSWITCH)" >&2 grep "hosts:" $varNSSWITCH|grep "file">/dev/null if [ $? -eq 0 ]; then echo "Your OS use files to resolved hostname." >&2 fi fi fi if [ -f $varDNSFILE ]; then varMYDNSSRV=$(grep "nameserver " $varDNSFILE|grep -v '^#'|head -n 1|cut -d' ' -f2) fi if [ "$varMYDNSSRV" != "" ]; then # We check if the command ping exist on the machine CMDOK=1 command -v ping >/dev/null || { CMDOK=0; } if [ $CMDOK -eq 1 ]; then # the command ping exist ping -q -c 4 $varMYDNSSRV >/dev/null if [ $? -gt 0 ]; then echo "Error when connect to the DNS server $varMYDNSSRV (using ping command)" >&2 varERROR=$(expr "$varERROR" + "20") fi else echo "The command ping doesn't exist, so we won't try to connect to the dns server $varMYDNSSRV" >&2 fi fi # We check if the command host exist on the machine. If yes we check the dns resolution CMDOK=1 command -v host >/dev/null || { CMDOK=0; } if [ $CMDOK -eq 0 ]; then # the command host doesn't exist echo "the command host doesn't exist" >&2 # If the host command doesn't exists, we try with the command getent CMDOK=1 command -v getent >/dev/null || { CMDOK=0; } if [ $CMDOK -eq 0 ]; then # the command getent doesn't exist echo "the command getent doesn't exist" >&2 varERROR=$(expr "$varERROR" + "30") else varDNSHOST=$(getent hosts $varHOSTTOTEST) varCHECKDNS=$(echo $varDNSHOST|grep -v "not found"| cut -d' ' -f1) fi else varDNSHOST=$(host $varHOSTTOTEST) varCHECKDNS=$(echo $varDNSHOST|grep -v "not found"| cut -d' ' -f1) fi # We try to ping the host target if [ $varERROR -lt 30 ]; then if [ "$varCHECKDNS" != "" ]; then # We check if the command ping exist on the machine CMDOK=1 command -v ping >/dev/null || { CMDOK=0; } if [ $CMDOK -eq 1 ]; then # the command ping exist ping -q -c 4 $varHOSTTOTEST >/dev/null if [ $? -gt 0 ]; then echo "Error when connect to $varHOSTTOTEST (using ping command)" >&2 varERROR=$(expr "$varERROR" + "100") fi else echo "The command ping doesn't exist, so we won't try to connect to the host $varHOSTTOTEST" >&2 fi varRESULTDNS=$varCHECKDNS else echo "Can't resolve the DNS name for $varHOSTTOTEST" >&2 varERROR=$(expr "$varERROR" + "200") varRESULTDNS="" fi fi fi return $varRESULTDNS } #function use to check if your system can connection on the Web/Internet function check_web_connection { NOK=0 if [ "$1" != "" ]; then varHOSTTOTEST=$1 else varHOSTTOTEST="" fi varHTTPPROTO="http" varHTTPPORT=80 varRESULTCON=1 varERROR=0 if [ "$varHOSTTOTEST" = "" ]; then echo "No hostname to test" >&2 varERROR=$(expr "$varERROR" + "1") else # We check if the command wget exist on the machine CMDOK=1 command -v wget >/dev/null || { CMDOK=0; } if [ $CMDOK -eq 1 ]; then # the command wget exist wget -q --timeout=20 -O - $varHTTPPROTO://$varHOSTTOTEST:$varHTTPPORT > /dev/null # If proxy needed # wget -q --timeout=20 -e use_proxy=yes -e http_proxy=proxy_server:proxy_port -O - $varHTTPPROTO://$varHOSTTOTEST:$varHTTPPORT > /dev/null if [[ $? -eq 0 ]]; then #echo "Web Online" varRESULTCON=0 else echo "Web Offline" >&2 varRESULTCON=$varERROR fi else echo "The command wget doesn't exist" >&2 # We check if the command curl exist on the machine CMDOK=1 command -v curl >/dev/null || { CMDOK=0; } if [ $CMDOK -eq 1 ]; then # the command curl exist curl -sL -w "%{http_code}\\n" $varHTTPPROTO://$varHOSTTOTEST:$varHTTPPORT -o /dev/null # If proxy needed #curl -x http://proxy_server:proxy_port --proxy-user username:password -sL -w "%{http_code}\\n" $varHTTPPROTO://$varHOSTTOTEST:$varHTTPPORT -o /dev/null if [[ $? -eq 0 ]]; then #echo "Web Online" varRESULTCON=0 else echo "Web Offline" >&2 varRESULTCON=$varERROR fi else echo "The command curl doesn't exist" >&2 fi fi fi return $varRESULTCON } ######## END FUNCTIONS ####### ###### MAIN ACTIONS ###### varMYHOST="www.usiscript.com" varRESULTCHKDNS=$(check_dns_resolution $varMYHOST) if [ "$varRESULTCHKDNS" != "" ]; then echo "!! Errors have appeared when check the dns resolution to $varMYHOST" else echo "Dns name = $varRESULTCHKDNS" fi varRESULTCHKCON=$(check_web_connection $varMYHOST) if [ $varRESULTCHKCON -gt 0 ]; then echo "!! Errors have appeared when check the web connection to $varMYHOST" else echo "You are connected to the Web!" fi fi
download a file
Code
function download_sources () { URLSRC=$1 SRCFILE=$(basename $URLSRC) DESTSRC=$2 PACKAGENAME=$(basename $SRCFILE) RESULTSRC=0 varGETHTTPCODE=404 if [ "$DESTSRC" = "" ]; then echo "NO destination specified" >&2 DESTSRC="./$PACKAGENAME" #exit 1 fi if [ -d $DESTSRC ]; then DESTSRCDIR=$DESTSRC DESTSRC=$DESTSRCDIR/$PACKAGENAME else DESTSRCDIR=$(dirname $DESTSRC) fi if [ ! -d $DESTSRCDIR ]; then #echo "The folder $DESTSRCDIR doesn't exist. We create it" mkdir -p $DESTSRCDIR if [ $? -ne 0 ]; then echo "Error when create the destination folder ($DESTSRCDIR)" >&2 RESULTSRC=13 fi fi DESTFULLPATH=$(echo ${DESTSRC}|sed 's#//#/#g') #DESTFULLPATH=$(echo ${DESTSRC}/${PACKAGENAME}|sed 's#//#/#g') #echo "Download/Get sources $PACKAGENAME ( to $DESTFULLPATH)" if [ -d $DESTSRCDIR ]; then if [[ ! -f $SRCFILE ]] || [[ ! -f $DESTFULLPATH ]]; then if [[ $URLSRC == http* ]] || [[ $URLSRC == ftp* ]]; then if [ ! -f ${DESTFULLPATH} ]; then # Check the http response varGETHTTPCODE=$(wget -S --no-check-certificate --spider -O /dev/null --tries=2 --timeout=15 $URLSRC 2>&1 |grep "HTTP/\|^220 "| sed "s/220/220 200/g" |tail -n1| awk '{print $2}') if [ -z $varGETHTTPCODE ]; then varGETHTTPCODE=404 ; fi #echo "$SRCFILE = $varGETHTTPCODE" if [ $varGETHTTPCODE -lt 400 ]; then #Download the sources using wget command #echo "Download the sources ($SRCFILE - $URLSRC) ..." if [[ $varGETHTTPCODE -gt 300 ]] && [[ $varGETHTTPCODE -lt 400 ]]; then #echo "curl -s -k -L -o ${DESTFULLPATH} $URLSRC" curl -s -k -L -o ${DESTFULLPATH} $URLSRC else #echo "wget -q --no-check-certificate -O ${DESTFULLPATH} $URLSRC" wget -q --no-check-certificate -O ${DESTFULLPATH} $URLSRC fi if [ $? -ne 0 ]; then echo "Error when download the sources ($SRCFILE - $URLSRC)" >&2 RESULTSRC=11 rm -f ${DESTFULLPATH} #echo "rm -f ${DESTFULLPATH}" else RESULTS_SIZE=$(stat -c %s ${DESTFULLPATH}) if [ $RESULTS_SIZE -lt 2 ]; then echo "Error : The filesize of the file (${DESTFULLPATH}) is null" >&2 RESULTSRC=11 fi fi if [ -f ${PACKAGENAME} ]; then if [ ! -f ${DESTFULLPATH} ]; then #echo "Copy the file ${PACKAGENAME} to ${DESTFULLPATH}" cp ${PACKAGENAME} ${DESTFULLPATH} fi else if [ ! -f ${DESTFULLPATH} ]; then echo "File ${DESTFULLPATH} not found" >&2 RESULTSRC=12 fi fi #if [ -f ${DESTFULLPATH} ]; then # echo "File ${DESTFULLPATH} well downloaded" #fi else echo "Url $SRCFILE return a bad HTTP answer : $varGETHTTPCODE " >&2 fi else echo "The file ${DESTFULLPATH} already exist" >&2 fi else if [ -f ${PACKAGENAME} ]; then if [ ! -f ${DESTFULLPATH} ]; then #echo "Copy the file ${PACKAGENAME} to ${DESTFULLPATH}" #echo "cp ${PACKAGENAME} ${DESTFULLPATH}" cp ${PACKAGENAME} ${DESTFULLPATH} fi else echo "File $SRCFILE not found" >&2 RESULTSRC=12 fi fi else if [ ! -f ${DESTFULLPATH} ]; then #echo "Copy the file $SRCFILE to ${DESTFULLPATH}" cp $SRCFILE ${DESTFULLPATH} else echo "The file ${DESTFULLPATH} already exist" >&2 fi fi else echo "Destination folder $DESTSRC doesn't exist" >&2 RESULTSRC=13 fi echo $RESULTSRC return $RESULTSRC } ######## END FUNCTIONS ####### ###### MAIN ACTIONS ###### varHTTPURLISOSOURCE="http://cdimage.kali.org/kali-2022.2/kali-linux-2022.2-live-amd64.iso" varISOIMAGE="/media/usbkey/kali-linux-2022_live_amd64.iso" # Download the ISO IMAGE varRESULTDOWNLOAD=$(download_sources $varHTTPURLISOSOURCE $varISOIMAGE) if [ $varRESULTDOWNLOAD -gt 0 ]; then echo "!! Errors have appeared when download the iso image from $varHTTPURLISOSOURCE" exit 1 else if [ -f $varISOIMAGE ]; then echo "Iso image downloaded from $varHTTPURLISOSOURCE" else echo "Errors when download the iso image from $varHTTPURLISOSOURCE" exit 1 fi fi
manage firewall rules
Code
############################### # 1 Functions # set_firewall_rule : configure a new rule on your firewall #Check if you are running this script as ROOT user. If yes; no need to a Sudo if [ "$USER" == "root" ]; then INSUDO="" else INSUDO="sudo " fi ######## FUNCTIONS ####### # set_firewall_rule : configure a new rule on your firewall set_firewall_rule() { NETPORT=0 TODAYDATE=$(date +%d%m%Y) # Check if there is an argument given to the function if [[ ${!1} ]]; then NETPORT=$1 fi #IPTABLES CMDIPTABLESOK=1 $INSUDO command -v iptables >/dev/null || { echo "iptables command not found."; CMDIPTABLESOK=0; } if [ $CMDIPTABLESOK -gt 0 ]; then #We backup the new iptables rules echo "We backup the new iptables rules" $INSUDO $IPTABLESBIN-save > iptables-rules_${TODAYDATE} if [ $NETPORT -gt 0 ]; then # OPEN PORT / INSERT NEW RULE $INSUDO $IPTABLESBIN $IPFILTERWEB -A OUTPUT -o ${PUB_IF} -p tcp --dport $NETPORT -j ACCEPT $INSUDO $IPTABLESBIN $IPFILTERWEB -A INPUT -i ${PUB_IF} -p tcp --dport $NETPORT -j ACCEPT fi # Allow http / https (open port 80 / 443) $INSUDO $IPT -A INPUT -i ${PUB_IF} -p tcp --destination-port 80 -j ACCEPT $INSUDO $IPT -A OUTPUT -o ${PUB_IF} -p tcp --destination-port 443 -j ACCEPT # Allow full outgoing connection but no incomming stuff $INSUDO $IPT -A INPUT -i ${PUB_IF} -m state --state ESTABLISHED,RELATED -j ACCEPT $INSUDO $IPT -A OUTPUT -o ${PUB_IF} -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT if [ $SSHPORT -gt 0 ]; then $INSUDO $IPT -A INPUT -m state --state NEW -m tcp -p tcp --destination-port $SSHPORT -j ACCEPT fi # Block DDos attacks $INSUDO $IPT -A INPUT -p tcp --dport 80 -m limit --limit 20/minute --limit-burst 100 -j ACCEPT # Do not log smb/windows sharing packets - too much logging $INSUDO $IPT -A INPUT -p tcp -i ${PUB_IF} --dport 137:139 -j REJECT $INSUDO $IPT -A INPUT -p udp -i ${PUB_IF} --dport 137:139 -j REJECT fi # FIREWALLD CMDFIREWALLDOK=1 varGETSRVFIREWD="" #We backup the new iptables rules echo "We backup the new firwalld rules" if [ -d "$varFIREWALLDDIR" ]; then $INSUDO tar -czf firewall_rules_firewalld_${TODAYDATE}.tar.gz /etc/firewalld/ fi $INSUDO command -v firewall-cmd >/dev/null || { echo "firewall-cmd command not found."; CMDFIREWALLDOK=0; } if [ $CMDFIREWALLDOK -gt 0 ]; then if [ $NETPORT -gt 0 ]; then if [ -d "$varFIREWALLDDIR/services" ]; then varGETSRVFIREWD1=$(grep -l $NETPORT $varFIREWALLDDIR/services/*) if [ "$varGETSRVFIREWD1" != "" ]; then if [ -f $varGETSRVFIREWD1 ]; then varGETSRVFIREWD=$(grep short $varGETSRVFIREWD1|sed 's/
\(.*\)
/\1/') fi fi fi if [ "$varGETSRVFIREWD" != "" ]; then $INSUDO firewall-cmd --add-service=$varGETSRVFIREWD --permanent fi $INSUDO firewall-cmd --add-port=$NETPORT/tcp --permanent $INSUDO firewall-cmd --add-port=$NETPORT/udp --permanent #Port forwarding #sudo firewall-cmd --zone="public" --add-forward-port=port=$NETPORT:proto=tcp:toport=12345 #sudo firewall-cmd --zone="public" --add-forward-port=port=$NETPORT:proto=tcp:toport=8080:toaddr=123.456.78.9 fi # To add the rules use ‘–add-rules‘ as show below. # firewall-cmd --direct --add-rule ipv4 filter IN_public_allow 0 -m tcp -p tcp --dport 25 -j ACCEPT # To remove the rules just replace ‘–add-rule‘ with ‘–remove-rule‘. # firewall-cmd --direct --remove-rule ipv4 filter IN_public_allow 0 -m tcp -p tcp --dport 25 -j ACCEPT # To add specific IP address (192.168.0.254) to trusted public zone, use the following command. # firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.0.254" accept' # After adding above rule, don’t forget to list all the trusted public zone rules. # firewall-cmd --zone=public --list-all # To remove any added rule, just replace the ‘–add-rich-rule‘ with remove ‘–remove-rich-rule‘ as show in below command. # firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="192.168.0.254" accept' # To reject or drop a IP address from the trusted zones, just replace ‘accept‘ with ‘reject‘ as shown in the below command. # firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.0.250" reject' ######### # List all rules/zone and service # sudo firewall-cmd --list-all # firewall-cmd --get-zones # firewall-cmd --get-active-zones # firewall-cmd --get-services # sudo firewall-cmd --list-services # sudo firewall-cmd --zone=public --list-ports # Reload rules and restart Firewalld service $INSUDO firewall-cmd --reload $INSUDO systemctl restart firewall fi echo "Restart the Firewall service" CMDSYSTEMCTLOK=1 $INSUDO command -v systemctl >/dev/null || { echo "systemctl command not found."; CMDSYSTEMCTLOK=0; } if [ $CMDSYSTEMCTLOK -gt 0 ]; then # new methode echo "Restart the iptables service" $INSUDO systemctl restart iptables $INSUDO systemctl enable iptables else CMDSERVOK=1 $INSUDO command -v service >/dev/null || { echo "service command not found."; CMDSERVOK=0; } if [ $CMDSERVOK -gt 0 ]; then # old methode echo "Restart the iptables service" $INSUDO service iptables restart fi fi } ######## END FUNCTIONS ####### ###### MAIN ACTIONS ###### PUB_IF="eth0" SSHPORT=22 varFIREWALLDDIR="/etc/firewalld" varPKGTOINSTALL="firewalld" varMYPKGMNG=$(get_package_manager) echo "My package manager = $varMYPKGMNG" result_package_exist=$(check_package_exist $varPKGTOINSTALL) if [ $result_package_exist ]; then set_firewall_rule else echo "The package $varPKGTOINSTALL is NOT installed" varRESULTINST=$(install_package $varPKGTOINSTALL) if [ $varRESULTINST -eq 1 ]; then echo -e "!! Errors have appeared when install the package(s)" exit 1 else set_firewall_rule fi fi
get the type of the OS
Code
############################### # 2 Functions # get_os = to get the os on your machine # get_linux_distrib = to get the type and name of your os version/distribution # get the os on your machine get_os() { #Check which os you are running varPLATFORM='unknown' unamestr=$(uname) if [[ "$unamestr" == 'Linux' ]]; then varPLATFORM='linux' elif [[ "$unamestr" == 'FreeBSD' ]]; then varPLATFORM='freebsd' fi if [[ "$OSTYPE" == "linux-gnu" ]]; then varPLATFORM='linux' elif [[ "$OSTYPE" == "darwin"* ]]; then varPLATFORM='macos' elif [[ "$OSTYPE" == "cygwin" ]]; then varPLATFORM='cygwin' elif [[ "$OSTYPE" == "msys" ]]; then varPLATFORM='linux' elif [[ "$OSTYPE" == "win32" ]]; then # I'm not sure this can happen. varPLATFORM='windows' elif [[ "$OSTYPE" == "freebsd"* ]]; then varPLATFORM='freebsd' else varPLATFORM='Unknown' fi echo $varPLATFORM } # get the type and name of your os version/distribution get_linux_distrib() { varDISTRO='unknown' #Check that you're on Centos/Redhat plateform or an ubuntu/debian # Determine OS platform UNAME=$(uname | tr "[:upper:]" "[:lower:]") # If Linux, try to determine specific distribution if [ "$UNAME" == "linux" ]; then # If available, use LSB to identify distribution if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then varDISTRO=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//) # Otherwise, use release info file else if [ -f /etc/redhat-release ]; then varDISTRO="redhat" else if [ -f /etc/debian_version ]; then varDISTRO="debian" else if [ -f /etc/issue ]; then varDISTRO=$( grep -Eoi 'Debian|SUSE|Ubuntu' /etc/issue ) else varDISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1|grep -v "os\|system") fi fi fi fi varDISTRO=$(echo $varDISTRO|tr "[:upper:]" "[:lower:]"|sed "s/ //g") fi echo $varDISTRO } strYOUROS=$(get_os) strYOURLINUX=$(get_linux_distrib)
check if exsit/install a package
Code
############################### # 3 Functions # get_package_manager = to get the package manager install on your OS # check_package_exist = check if a package already exist # install_package = to install the package you want ######## FUNCTIONS ####### # get the package manager install on your OS get_package_manager() { LISTPKGMNG="ipkg yum rpm urpmi apt-get apt dpkg emerge" varPKGMNG='unknown' for i in $LISTPKGMNG do command -v $i >/dev/null && packmanager=$i && break || { echo "$i command not found."; NOK=1; } #if the command COMMAND doesn't exist #which $i >/dev/null && packmanager=$i && continue || { echo "$i command not found."; NOK=1; } done case "$packmanager" in "apt") export DEBIAN_FRONTEND=noninteractive # If you have a proxy : sudo http_proxy='http://user:pass@proxy.example.com:8080/' apt-get install package-name cmdinstall="apt -qy install " cmduninstall="apt remove purge " #cmdlistpackage="apt-cache pkgnames" cmdlistpackage="dpkg --get-selections" varPKGMNG='apt' ;; "apt-get") export DEBIAN_FRONTEND=noninteractive # If you have a proxy : sudo http_proxy='http://user:pass@proxy.example.com:8080/' apt-get install package-name cmdinstall="apt-get -qy install " cmduninstall="apt-get remove purge " #cmdlistpackage="apt-cache pkgnames" cmdlistpackage="dpkg --get-selections" varPKGMNG='apt-get' ;; "dpkg") cmdinstall="dpkg -i " cmduninstall="dpkg -r -P " cmdlistpackage="dpkg -l" varPKGMNG='dpkg' ;; "ipkg") cmdinstall="ipkg install " cmduninstall="ipkg remove " cmdlistpackage="ipkg status" varPKGMNG='ipkg' ;; "yum") cmdinstall="yum -q -y install " # If you have a proxy : export http_proxy="http://yum-user:qwerty@mycache.mydomain.com:3128"; yum -q -y install package-name cmduninstall="yum remove " cmdlistpackage="yum list" varPKGMNG='yum' ;; "rpm") cmdinstall="rpm -ivh " # If you have a proxy : rpm -ivh --httpproxy my.proxy.com --httpport 8080 package-name cmduninstall="rpm -e " cmdlistpackage="rpm -qa" varPKGMNG='rpm' ;; "urpmi") cmdinstall="urpmi --auto " cmduninstall="urpme " cmdlistpackage="urpmq" varPKGMNG='urpm' ;; "emerge") cmdinstall="emerge -uD " cmduninstall="emerge -c " cmdlistpackage="equery list '*' " varPKGMNG='emerge' ;; "pacman") cmdinstall="pacman -S " cmduninstall="pacman -Qqe " cmdlistpackage="pacman -Qs " varPKGMNG='pacman' ;; *) cmdinstall="unknown" cmduninstall="unknown" cmdlistpackage="unknown" varPKGMNG='unknown' ;; esac echo $varPKGMNG } #function use to check if package(s) that we will install already exist in the system. #If yes we put an error flag. function check_package_exist { NOK=0 packagesexist="" if [ "$1" != "" ]; then varPKGTOCHECK=$1 else varPKGTOCHECK="" fi #Check if you are running this script as ROOT user. If yes; no need to a Sudo if [ "$USER" == "root" ]; then INSUDO="" else INSUDO="sudo " fi if [ "$varPKGTOCHECK" != "" ]; then for varLISTPKG in $varPKGTOCHECK do if [[ $cmdlistpackage = 'unknown' ]]; then echo "no package manager" else echo "Check if package $varLISTPKG already exists in the system..." RES=$($INSUDO $cmdlistpackage |grep -ie "^$varLISTPKG\$"|wc -l) if [ $RES -gt 0 ]; then NOK=1 packagesexist=$packagesexist" "$varLISTPKG echo "The package $varLISTPKG already exist in the system" fi fi done fi if [ $NOK -eq 1 ]; then return 1 else return 0 fi } # install the package you want install_package() { FORCEINSTALL=1 varRESULT=0 if [ "$1" != "" ]; then varPKGTOINST=$1 else varPKGTOINST="" fi #Check if you are running this script as ROOT user. If yes; no need to a Sudo if [ "$USER" == "root" ]; then INSUDO="" else INSUDO="sudo " fi if [ "$varPKGTOINST" != "" ]; then for varLISTPKG in $varPKGTOINST do if [ $FORCEINSTALL -lt 1 ]; then echo "Are you sure to install $varLISTPKG ? [y/n]" read reponse else reponse='y' fi if [[ $reponse = 'y' ]]; then echo "Install package $varLISTPKG..." echo "$cmdinstall $varLISTPKG" if [[ $cmdlistpackage = 'unknown' ]]; then echo "no package manager" else RES=$($INSUDO $cmdinstall $varLISTPKG) if [ $? -eq 1 ]; then echo -e "Error when install $varLISTPKG" varRESULT=1 fi fi fi done fi return $varRESULT } ######## END FUNCTIONS ####### ###### MAIN ACTIONS ###### varPKGTOINSTALL="mypackage" varMYPKGMNG=$(get_package_manager) echo "My package manager = $varMYPKGMNG" result_package_exist=$(check_package_exist $varPKGTOINSTALL) if [ ! $result_package_exist ]; then varRESULTINST=$(install_package $varPKGTOINSTALL) if [ $varRESULTINST -eq 1 ]; then echo -e "!! Errors have appeared when install the package(s)" exit 1 fi else echo "The package $varPKGTOINSTALL already installed" fi
start/stop a service
Code
############################### # 2 Functions # start_stop_service : start - stop - restart service # check_service_exist = check if a service exist on the server #Check if you are running this script as ROOT user. If yes; no need to a Sudo if [ "$USER" == "root" ]; then INSUDO="" else INSUDO="sudo " fi ######## FUNCTIONS ####### # start_stop_service : start - stop - restart service function start_stop_service() { echo "Restart the Firewall service" CMDSYSTEMCTLOK=1 $INSUDO command -v systemctl >/dev/null || { echo "systemctl command not found."; CMDSYSTEMCTLOK=0; } if [ $CMDSYSTEMCTLOK -gt 0 ]; then # new methode echo "Restart the iptables service" $INSUDO systemctl restart iptables $INSUDO systemctl enable iptables else CMDSERVOK=1 $INSUDO command -v service >/dev/null || { echo "service command not found."; CMDSERVOK=0; } if [ $CMDSERVOK -gt 0 ]; then # old methode echo "Restart the iptables service" $INSUDO service iptables restart fi fi } # check_service_exist = check if a service exist on the server function check_service_exist() { } ######## END FUNCTIONS ####### ###### MAIN ACTIONS ###### varSERVICENAME="networking" varCHECKSRV=$(check_service_exist($varSERVICENAME)) if [ $varCHECKSRV -ne 1 ]; then echo -e "!! the service $varSERVICENAME doesn't exist" exit 1 else varMNGSRV=$(start_stop_service($varSERVICENAME)) fi