System Automation

Basic Windows and Linux Infrastructure automation
Author: Jason Graham

more Advanced Topics here

Windows and Linux operating systems can be highly automated. The closest automation to the processor and hardware can be done from programming the computer in it’s native hardware API’s and having it startup as a background service but that is too advanced and hard!

When I was entering college, I took some courses in computer hardware like x8086 microarchitecture. It taught us to develop Assembler code .ASM that can control the BIOS, CPU and memory before the OS even loads. You could write a small boot up program that would initialize and run in a very small amount of RAM from a bootable diskette. But that is overly complex. Other higher level languages like Python, C/C++ and Java can make this easier and you can write programs that do a lot! But now a days, computers can be automated with languages like Python and shell script very quickly.



Modern Windows Automation

Microsoft Windows comes with a number of free scripting languages like Visual Studio (a IDE based compiler), powershell script (.ps1), visual basic (.vbs) and C# (.cs) script.
But for most portable, feature rich and the smallest code, and simplicity, you can’t beat Python.

Learn Python script

Scripting windows with Python

Python is an interpreted language that does not build binary files like C/C++ or Java. Download the interpreter and install it to your PC

  1. install Python by downloading it from: Here
    (For now do not change any install settings, just accept all defaults. When this finishes, continue)
  2. create a code directory on your PC. Typically in your user folder and back it up to at least one other place frequently
  3. test your python shell
  4. write your first script program

Beginner first Python program

(note, if you are more advanced, you may skip to automation below)
– Open a new notepad file in your new code folder called hello.py, and save it with this code:

# This program prints Hello, world!

print('Hello, world!')

-Run the program:

Save the file. Click start, type the directory and open it. right click it in file explorer and select run with (select python.exe). If you don’t see this, you need to add python.exe to the program type list in explorer for the .py files. (note, this should already been done after installing python, but not always. i.e., if you’ve stopped the install in the middle and restarted it later.)

When the program runs, you will see:

Output:

Hello, world!


Windows Python Automation example 1 – automate shared directory

Let’s say you want to search a shared folder someone has that contains their text report file. You need something that checks this everyday and tells you (1 – it exists) and (2 – it’s weekly content is done).
Python can connect you and find the file and look at it’s contents and tell you if it’s been updated.

  • Open your new code folder and create a folder called C:\reportfolder and share it in explorer.
  • Create a new file called report.py and open it in notepad
import subprocess

# Disconnect anything on M
subprocess.call(r'net use M: /del', shell=True)
subprocess.call(r'net use M: \\YOURPCNAME\reportfolder /user:yourusername', shell=True)

Save the text above to the file with read permission only to you, to report.py and run it by following the same directions above. After running this, you should see a M: drive in explorer. You now have a remote directory that others if they have that password can access.

Note: (If the python script still gives you a code 86 or similar network password error, make sure you check under control panel that your network is “Private” under Network & Internet settings. Once this is set, close control panel and the command window or explorer folder and try again.)

Also, you need to give everyone else access to this in the explorer on your PC, (as administrator user) and the network should be able to reach this from everyone else’s PC.

Check file exists and was modified from last time. If so, email the team.

(Note – the version of windows is important here. If whoever is running this has windows Home edition, your folder may not show up on others computers unless it is defined as a shared media computer in windows media explorer in windows home networks.

Linux Python Automation

Similar automation can be found in Linux. The Linux OS and Unix were built to automate tedious procedures in computing and as a operation system framework of best practices to encapsulate hardware and processes. Linux is the workhorse behind many large and small corporations and their enterprise computing endeavors.

Advanced Linux Automation

Python can help to automate the software build – (compilation of sourcecode, online service and binaries) and produce continual automated releases of a project (CI). It can also be used to monitor and report (Dashboarding) and share files and projects via web or sharing services that can be added so a team can quickly access files without need of a sharepoint, office or FTP host. For now, let’s have python do some tasks that are normally manual process a system admin needs to accomplish. Namely, share a cloud web report with other people

To start a simple report, make a directory:

% mkdir ~/reportfolder

If there is no report, save one using a linux editor to ~/reportfolder/report.txt

For this example, we use the windows report.py and make the following changes:

import subprocess
import time
from time import gmtime, strftime
import os

file = "/home/jason/reportfolder/report.txt"

# test if file is in the directory
fileexist = os.path.exists(file)

if fileexist:

    NOW = strftime("%d-%m-%Y")

    MOD = time.strftime("%d-%m-%Y", time.localtime(os.path.getmtime(file)))

    if (NOW == MOD):
       print("File last modified today!\n")

else:
    print("File does not exist")
     
   

cron – A builtin job scheduler for linux can run your python

At a linux terminal, type crontab -e [enter]
If you are on debian or a basic shell, it will ask you to setup crontab editor. Do that and save the results as specified.

edit a cron job:

% crontab -e

Next, type the bottom line at the bottom of the cron file:

# Edit this file to introduce tasks to be run by cron.
# ..
# For more information see the manual pages of crontab(5) and cron(8)
# m  h   dom   mon   dow   command

30  9 * * 1  python ~/reportfolder/report.py &>> /dev/null

Next, save the file.

This will run the report every day at 9:30am local time and send the terminal output to dev/null or the linux virtual shredder can.

[todo] Next, we will update the script to display the results to a dashboard in the cloud.

[ ToDO add linux python code here ]

Last update: Jason(admin) – 7/19/2023 from Orem, UT

Leave a comment

Your email address will not be published. Required fields are marked *