SecPod Community Forums SanerScript Patch repository creation for ubuntu 18,20,22 LTS

Tagged: 

  • This topic has 1 reply, 1 voice, and was last updated by Shreya.
Viewing 1 reply thread
  • Author
    Posts
    • #6258
      Shreya
      Participant

        Ubuntu ClientSide

        #!/bin/sh

        log_file=”/var/log/create_sources_list.log”
        ip_address=”192.168.2.50″ # Replace with your desired IP address
        protocol=”https” # Replace with your desired protocol (http/https)

        # Create a backup of the existing sources.list
        mv /etc/apt/sources.list /etc/apt/sources.list.bak

        # Create an empty sources.list
        touch /etc/apt/sources.list

        # Get the OS version
        os_version=$(lsb_release -d | cut -d’.’ -f1 | awk ‘{print $NF}’)
        echo “OS version is $os_version” >> “$log_file”

        # Add repository entries based on the OS version
        if [ “$os_version” = “20” ]; then
        echo “Adding repository entries for Ubuntu 20.04” >> “$log_file”
        echo “deb $protocol://${ip_address}/ubuntu/ focal main restricted” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ focal-updates main restricted” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ focal universe” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ focal-updates universe” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ focal multiverse” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ focal-updates multiverse” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ focal-backports main restricted universe multiverse” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu focal-security main restricted” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu focal-security universe” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu focal-security multiverse” >> /etc/apt/sources.list

        elif [ “$os_version” = “22” ]; then
        echo “Adding repository entries for Ubuntu 22.04” >> “$log_file”
        echo “deb $protocol://${ip_address}/ubuntu/ jammy main restricted” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ jammy-updates main restricted” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ jammy universe” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ jammy-updates universe” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ jammy multiverse” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ jammy-updates multiverse” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ jammy-backports main restricted universe multiverse” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu jammy-security main restricted” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu jammy-security universe” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu jammy-security multiverse” >> /etc/apt/sources.list

        elif [ “$os_version” = “18” ]; then
        echo “Adding repository entries for Ubuntu 18.04” >> “$log_file”
        echo “deb $protocol://${ip_address}/ubuntu/ bionic main restricted” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ bionic-updates main restricted” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ bionic universe” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ bionic-updates universe” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ bionic-updates multiverse” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu/ bionic-backports main restricted universe multiverse” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu bionic-security main restricted” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu bionic-security universe” >> /etc/apt/sources.list
        echo “deb $protocol://${ip_address}/ubuntu bionic-security multiverse” >> /etc/apt/sources.list

        else
        echo “Expected OS not found” >> “$log_file”
        fi

        # Check the apt-get update
        update_output=$(sudo apt-get update 2>&1)

        if [ $? -eq 0 ]; then
        echo “apt-get update successful” >> “$log_file”
        else
        echo “apt-get update failed” >> “$log_file”
        echo “$update_output” >> “$log_file”
        echo “Removing /etc/apt/sources.list due to apt-get update failure” >> “$log_file”

        # Remove the sources.list file if the apt-get update fails
        rm -f /etc/apt/sources.list

        # Restore to the previous state
        mv /etc/apt/sources.list.bak /etc/apt/sources.list
        fi

      • #6259
        Shreya
        Participant

          Ubuntu Repo Final

          echo “#################################################################################################################”
          echo -e “\033[1;31mChecking the reachability of primary URLs… Please be patient\033[0m”
          echo “#################################################################################################################”
          echo “”

          # Check if lsb_release command is available
          if command -v lsb_release > /dev/null; then
          ubuntu_version=$(lsb_release -rs)
          echo “Ubuntu version: $ubuntu_version”
          else
          echo “lsb_release command not found. Unable to determine Ubuntu version.”
          fi

          hosts=(
          “archive.ubuntu.com”
          “security.ubuntu.com”
          )

          for host in “${hosts[@]}”; do
          if ping -c 1 “$host” >/dev/null 2>&1; then
          echo -e “\033[1;32m$host is reachable.\033[0m”
          else
          echo -e “\033[1;31m$host is not reachable.\033[0m”
          fi
          done

          echo “”

          if sudo apt-get -y install curl > /dev/null 2>&1; then
          echo “curl has been successfully installed.”
          else
          echo “Failed to install curl.”
          fi

          echo “”
          echo “”

          echo “Checking reachability of primary URLs…”

          URLS=(
          http://archive.ubuntu.com”
          http://security.ubuntu.com”
          )

          reachable_count=0
          attempted_count=${#URLS[@]}
          reachable=true

          for url in “${URLS[@]}”; do
          echo “Checking $url…”

          # Check port 80 (HTTP)
          curl -sI “$url” > /dev/null
          http_status=$?

          # Check port 443 (HTTPS)
          curl -sI “$url” –insecure > /dev/null
          https_status=$?

          if [ $http_status -eq 0 ] || [ $https_status -eq 0 ]; then
          echo “Reachable: $url”
          ((reachable_count++))
          else
          echo “Not Reachable: $url”
          reachable=false
          fi

          echo “”
          done

          echo “Attempted: $attempted_count”
          echo “Reachable: $reachable_count”

          echo “”

          if [ “$reachable” = true ]; then
          echo -e “\033[1;32mPre-requisite Check Completed successfully. Script Execution will continue \033[0m”
          echo “”
          else
          echo -e “\033[1;31mPre-requisite failure: do you still want to Proceed. Press Enter to proceed.\033[0m”
          echo “”
          read -r
          fi

          ############################################################################## PRE RE-Requiste tools#############################################################################################

          echo -e “\033[1;31mStarted the Ubuntu Repo Creation… Please be Patient\033[0m\n”
          echo “#################################################################################################################”
          echo “”
          echo -e “\x1b[30;44m Downloading the Pre-Requistite tools and Applications \x1b[m”

          # Function to check the success of an application installation
          check_installation() {
          app_name=$1
          if [ $? -eq 0 ]; then
          echo “$app_name installed successfully.”
          else
          echo “$app_name installation failed.”
          read -p “Do you want to continue? (y/n): ” choice
          if [ “$choice” != “y” ]; then
          echo “Exiting…”
          exit 1
          else
          echo “Continuing…”
          fi
          fi
          }

          echo “Updating package list…”
          sudo apt-get update
          check_installation “Package Update”

          echo “Installing wget…”
          sudo apt-get -y install wget
          check_installation “wget”

          echo “Installing curl…”
          sudo apt-get -y install curl
          check_installation “curl”

          echo “Installing net-tools…”
          sudo apt-get -y install net-tools
          check_installation “net-tools”

          echo “Installing vim…”
          sudo apt-get -y install vim
          check_installation “vim”

          echo “Installing cron…”
          sudo apt install cron
          check_installation “cron”

          echo “Enabling cron service…”
          sudo systemctl enable cron
          check_installation “Enable cron service”

          echo “Starting cron service…”
          sudo systemctl start cron.service
          check_installation “Start cron service”

          echo “”
          echo -e “\x1b[30;44m Installing Apache Application \x1b[m”
          echo “”
          sudo apt install apache2 -y
          check_installation “Apache Application”
          echo -e “\033[1;31mInstallation Completed \033[0m\n”

          echo -e “\x1b[30;44m Enabling Apache Application \x1b[m”
          sudo systemctl enable apache2
          check_installation “Enable Apache”
          echo -e “\033[1;31mEnabling the Apache Completed\033[0m\n”

          echo -e “\x1b[30;44m Checking the Apache service is in running state \x1b[m”
          STATUS=”$(systemctl is-active apache2)”
          if [ “${STATUS}” = “active” ]; then
          echo “Apache is running…..”
          else
          echo ” Apache is not running…. so exiting ”
          exit 1
          fi

          ################################################################### Validations ########################################################################################################

          # Function to check the success of a command execution
          check_command() {
          command_description=$1
          if [ $? -eq 0 ]; then
          echo “$command_description completed successfully.”
          else
          echo “$command_description failed.”
          read -p “Do you want to continue? (y/n): ” choice
          if [ “$choice” != “y” ]; then
          echo “Exiting…”
          exit 1
          else
          echo “Continuing…”
          fi
          fi
          }

          echo -e “\x1b[30;44mCreating the Repo Root Directory and owning the permissions. PLease enter the partition where teh suficient storage space is there to download the updates\x1b[m”
          sudo mkdir -p /opt/apt-mirror
          sudo chown www-data:www-data /opt/apt-mirror
          check_command “Create Repo Root Directory and Set Permissions”

          echo -e “\x1b[30;44mInstalling APT Mirror application \x1b[m”
          sudo apt install apt-mirror -y
          sudo apt update
          check_command “Install APT Mirror Application”

          echo -e “\x1b[30;44mBacking up /etc/apt/mirror.list \x1b[m”
          sudo cp /etc/apt/mirror.list /etc/apt/mirror.list.bak
          check_command “Backup /etc/apt/mirror.list”

          echo -e “\x1b[30;44mMaking var folder \x1b[m”
          sudo mkdir -p /opt/apt-mirror/ubuntu/var
          check_command “Create var folder”

          echo -e “\x1b[30;44mCopying post script into /opt/apt-mirror/ubuntu/var/ and Configuring the /etc/apt/mirror.list… Please be Patient…..\x1b[m”
          sudo cp /var/spool/apt-mirror/var/postmirror.sh /opt/apt-mirror/ubuntu/var/
          sudo mv /etc/apt/mirror.list /etc/apt/mirror.list.bak
          rm -rf /etc/apt/mirror.list
          check_command “Copy post script and Configure /etc/apt/mirror.list”

          echo -e “\033[1;31mFile Copied and Backup is done \033[0m\n”

          ############################################################# HANDLING APT MIRROR LIST WITH INPUTS #####################################################################################

          echo -e “\x1b[30;44mUpdating the /etc/apt/mirror.list file \x1b[m”

          # Prompt the user for the OS versions
          echo “Now enter the Ubuntu Verion numbers”
          echo “For APT Mirror List Configuration, please specify the desired OS versions. Choose only the necessary versions to minimize storage space utilization? (e.g., 18,20,22)”
          read os_versions

           

          # Split the input into an array
          IFS=’,’ read -ra os_versions_array <<< “$os_versions”

          # Validate the user input
          for os_version in “${os_versions_array[@]}”; do
          if [ “$os_version” != “18” ] && [ “$os_version” != “20” ] && [ “$os_version” != “22” ]; then
          echo “Invalid OS version selected: $os_version. Exiting…”
          exit 1
          fi
          done

          # Append configuration based on the selected OS versions
          echo “set base_path /opt/apt-mirror” >> /etc/apt/mirror.list
          echo “set nthreads 20” >> /etc/apt/mirror.list
          echo “set _tilde 0” >> /etc/apt/mirror.list

          for os_version in “${os_versions_array[@]}”; do
          if [ “$os_version” == “22” ]; then
          echo “### Ubuntu Jammy Jellyfish 22.04” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu jammy main restricted universe multiverse” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu jammy-security main restricted universe multiverse” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu jammy-updates main restricted universe multiverse” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu jammy-backports main restricted universe multiverse” >> /etc/apt/mirror.list
          elif [ “$os_version” == “20” ]; then
          echo “### Ubuntu Focal 20.04” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu focal main restricted universe multiverse” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu focal-security main restricted universe multiverse” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu focal-updates main restricted universe multiverse” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse” >> /etc/apt/mirror.list
          elif [ “$os_version” == “18” ]; then
          echo “### Ubuntu Bionic 18.04” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu bionic main restricted universe multiverse” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu bionic-security main restricted universe multiverse” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu bionic-updates main restricted universe multiverse” >> /etc/apt/mirror.list
          echo “deb http://archive.ubuntu.com/ubuntu bionic-backports main restricted universe multiverse” >> /etc/apt/mirror.list
          fi
          done

          echo “clean http://archive.ubuntu.com/ubuntu&#8221; >> /etc/apt/mirror.list

          echo -e “\033[1;31mUpdated the Configuration\033[0m\n”

          echo -e “\x1b[30;44mConfigure Apache … Please be Patient….. \x1b[m”
          cd /var/www/html/
          cd /var/www/html/
          cd /var/www/html/
          echo “$PWD”
          ln -s /opt/apt-mirror/mirror/archive.ubuntu.com/ubuntu/ ./ubuntu
          ls -ltra
          echo -e “\033[1;31mConfiguration Done\033[0m\n”
          ##################################################################################################################################################################

           

          # Prompt the user for the Ubuntu releases
          echo “Specify the required Ubuntu releases for Daily Sync in /root/ubuntucnf.sh (e.g., 18, 20, 22).”
          read user_releases

          # Split the input into an array
          IFS=’,’ read -ra user_releases_array <<< “$user_releases”

          # Validate the user input
          for user_release in “${user_releases_array[@]}”; do
          if [ “$user_release” != “18” ] && [ “$user_release” != “20” ] && [ “$user_release” != “22” ]; then
          echo “Invalid Ubuntu release selected: $user_release. Exiting…”
          exit 1
          fi
          done

          # Generate the content for ubuntucnf.sh based on the selected releases
          cat > “/root/ubuntucnf.sh” <<EOF

          EOF

          # Generate configurations and append to ubuntucnf.sh
          generate_configs() {
          for user_release in “${user_releases_array[@]}”; do
          case “$user_release” in
          18)
          release=”bionic”
          ;;
          20)
          release=”focal”
          ;;
          22)
          release=”jammy”
          ;;
          *)
          # Handle invalid input
          echo “Invalid Ubuntu release selected: $user_release. Exiting…”
          exit 1
          ;;
          esac

          # Append configurations for the selected release
          cat >> “/root/ubuntucnf.sh” <<EOF
          # Specify the Ubuntu release
          release=”${release}”

          # Function to download and process CNF files
          download_cnf() {
          for p in “\${release}”{,-{security,updates,backports}}/{main,restricted,universe,multiverse}; do
          >&2 echo “\${p}”
          wget -q -c -r -np -R “index.html*” “http://archive.ubuntu.com/ubuntu/dists/\${p}/cnf/Commands-amd64.xz”
          done
          }

          # Function to download and process DEP11 icon files
          download_dep11_icons() {
          for p in “\${release}”{,-{security,updates,backports}}/{main,restricted,universe,multiverse}; do
          >&2 echo “\${p}”
          wget -q -c -r -np -R “index.html*” “http://archive.ubuntu.com/ubuntu/dists/\${p}/dep11/icons-64×[email protected]
          wget -q -c -r -np -R “index.html*” “http://archive.ubuntu.com/ubuntu/dists/\${p}/dep11/icons-64×64.tar.gz”
          wget -q -c -r -np -R “index.html*” “http://archive.ubuntu.com/ubuntu/dists/\${p}/dep11/icons-48×48.tar.gz”
          done
          }

          # Function to download and process binary packages for i386 architecture
          download_binary_packages() {
          for p in “\${release}”{,-{security,updates,backports}}/{main,restricted,universe,multiverse}; do
          >&2 echo “\${p}”
          wget -q -c -r -np -R “index.html*” “http://archive.ubuntu.com/ubuntu/dists/\${p}/binary-i386/Packages.gz”
          gunzip archive.ubuntu.com/ubuntu/dists/\${p}/binary-i386/Packages.gz
          done
          }

          # Execute the functions for the selected release
          download_cnf
          download_dep11_icons
          download_binary_packages

          # Copy downloaded files to the mirror directory
          #cp -r /root/archive.ubuntu.com/ubuntu/dists/ /opt/apt-mirror/mirror/archive.ubuntu.com/ubuntu/

          # Remove temporary files
          #rm -rf /root/archive.ubuntu.com

          EOF
          done
          }

          # Generate configurations and append to ubuntucnf.sh
          generate_configs

          # Make ubuntucnf.sh executable
          chmod +x “/root/ubuntucnf.sh”

          cat > /root/dailysync.sh << ‘EOF’
          #!/bin/bash

          log_file=”/root/apt_update.log”

          log() {
          echo “$(date ‘+%Y-%m-%d %H:%M:%S’) – $*” >> “$log_file”
          }

          check_apt_update() {
          log “Running apt-get update…”
          update_output=$(sudo apt-get update)

          if [ $? -eq 0 ]; then
          log “APT update successful.”
          return 0
          else
          log “APT update failed.”
          log “$update_output”
          return 1
          fi
          }

          log “Script ran on ($(date ‘+%Y-%m-%d %H:%M:%S’))”

          check_apt_update

          max_retries=5

          for i in $(seq 1 $max_retries); do
          log “Running apt-mirror…”
          /usr/bin/apt-mirror >> “$log_file” 2>&1
          log “Running ubuntucnf.sh…”
          /root/ubuntucnf.sh >> “$log_file”

          if [ $? -eq 0 ]; then
          log “Copying downloaded files to the mirror directory…”
          cp -r /root/archive.ubuntu.com/ubuntu/dists/ /opt/apt-mirror/mirror/archive.ubuntu.com/ubuntu/
          else
          log “ubuntucnf.sh failed. Skipping copy operation.”
          fi

          log “Removing temporary files…”
          rm -rf /root/archive.ubuntu.com

          sleep 50

          check_apt_update
          if [ $? -eq 0 ]; then
          log “Scripts completed successfully. Exiting.”
          exit 0
          else
          log “Retry $i of $max_retries…”
          fi

          sleep 50

          done

          log “Maximum retries reached. Scripts and APT-GET update failed.”
          exit 1
          EOF

          chmod +x /root/dailysync.sh

          # Prompt the user for further actions
          echo “Configurations for selected Ubuntu releases have been appended to /root/ubuntucnf.sh.”
          echo “The script has been made executable. You can manually run the script or add it to your cron jobs.”

          ################################################################################ HANDLING APT-MIRROR AND CRON JOB ##############################################################################################
          sudo chmod +x /root/ubuntucnf.sh
          ls -ltra /root/ubuntucnf.sh

          echo -e “\x1b[30;44mCreating the configuration for the daily sync using Contab … This will run on everday at 1 AM and 1 PM hours….. \x1b[m”
          rm /tmp/cron.bak
          echo -e “0 5,17 * * * bash /root/dailysync.sh > /root/dailysync_lastrun.log 2>&1” >> /tmp/cron.bak
          crontab /tmp/cron.bak
          crontab -l
          echo -e “\033[1;31mCron Job Created\033[0m\n”

          echo -e “\x1b[30;44mStarting the apt-mirror. This will replicate the packages from Public Ubuntu Repo to the Local Ubuntu Repo….. This will take time, please be patient\x1b[m”
          sudo apt-mirror
          sudo cp /var/spool/apt-mirror/var/postmirror.sh /opt/apt-mirror/var/
          sudo apt-mirror
          echo -e “\x1b[30;44mUbuntu Repo Sync Completed…… Please point the client devices to the Repo.\x1b[m”

           

      Viewing 1 reply thread
      • You must be logged in to reply to this topic.