Python Scripts for Automation

Below are some of the Linux Automation Scripts I wrote in the Python Coding Language

##############################################################
# Script Name: AutoUpdate.py
# 
# Description: AutoUpdates Linux system with error checking
#
# Usage: Automation script to update Linux system.
#
# Author: John Wilson
# Date: 27 April 2023
##############################################################


import subprocess

print('=============================================')
print('== Auto_Updating the system. PLEASE WAIT!! ==')
print('=============================================')

# Run the sudo update command using subprocess
update_command = "sudo apt update"
result=subprocess.run(update_command.split(), capture_output=True)

if result.returncode != 0:
    print('Error running the "sudo apt update" command', result.stderr)
    exit()
    

print('==============================================')
print('== Auto_Upgrading the system. PLEASE WAIT!! ==')
print('==============================================')

# Run the sudo upgrade command using subprocess, with progress updates
update_command = "sudo apt upgrade -y"
process=subprocess.Popen(update_command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
    output=process.stdout.readline()
    if output==b'' and process.poll() is not None:
        break
    if output:
        print (output.decode().strip())
            
if process.poll() != 0:
    print('Error running the "sudo apt upgrade -y" command', process.stderr.read().decode().strip())
    exit()

print('==================================================================')
print('== Auto_Removing un-needed files from the system. PLEASE WAIT!! ==')
print('==================================================================')

# Run the sudo autoremove command using subprocess
update_command = "sudo apt autoremove -y"
process=subprocess.Popen(update_command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
    output=process.stdout.readline()
    if output==b'' and process.poll() is not None:
        break
    if output:
        print (output.decode().strip())
            
if process.poll() != 0:
    print('Error running the "sudo apt upgrade -y" command', process.stderr.read().decode().strip())
    exit()

print('===============================================================')
print('== The Auto-Updating process has sucessfully been completed! ==')
print('========== The System has been updated sucessfully!! ==========')
print('===============================================================')
print('               +++++++++++++++++++++++++++++++')
print('               ++++++++++END OF LINE++++++++++')
print('               +++++++++++++++++++++++++++++++')