
10 Python Scripting Techniques for System Automation
As system administrators, we are constantly looking for ways to automate repetitive tasks and improve the efficiency of our infrastructure. Python is an ideal language for scripting due to its simplicity, flexibility, and extensive libraries. In this article, we will explore 10 essential Python scripting techniques that can be used for system automation.
1. File Management
One of the most common tasks in system administration is file management. You can use Python to automate tasks such as:
- Copying files from one location to another
- Renaming files based on a pattern or date
- Deleting old log files
Here’s an example script that uses the shutil
library to copy files:
“`python
import shutil
src = ‘/path/to/source’
dst = ‘/path/to/destination’
shutil.copy(src, dst)
“`
2. User Account Management
Python can be used to automate tasks related to user account management such as:
- Creating new users with specific settings (e.g., shell, home directory)
- Modifying existing user accounts
- Deleting inactive or unnecessary accounts
Here’s an example script that uses the getpass
and pwd
libraries:
“`python
import getpass
import pwd
username = ‘newuser’
password = ‘password’
try:
# Create a new user with specific settings
user_id = pwd.getpwnam(username)
if user_id == 0:
print(“User already exists.”)
else:
# Create the user and set the password
getpass.setpass(password, salt=None)
except KeyError:
# Create the user directly
getpass.getuser()
“`
3. Service Management
Python can be used to automate tasks related to service management such as:
- Starting or stopping services on a system
- Enabling or disabling services
- Checking if a service is running
Here’s an example script that uses the subprocess
library:
“`python
import subprocess
Start a service
service_name = ‘httpd’
try:
# Check if the service is already running
output = subprocess.check_output([‘systemctl’, ‘status’, service_name])
if ‘active (running)’ in str(output):
print(f”The {service_name} service is already running.”)
else:
# Start the service
subprocess.run([‘systemctl’, ‘start’, service_name], check=True)
except FileNotFoundError:
# The systemctl command is not available on this system.
pass
“`
4. Process Management
Python can be used to automate tasks related to process management such as:
- Killing or restarting processes based on specific criteria (e.g., CPU usage, memory usage)
Here’s an example script that uses the psutil
library:
“`python
import psutil
Get a list of running processes
processes = [p.info for p in psutil.process_iter()]
Filter the list to only include processes with high CPU usage
high_cpu_processes = [p for p in processes if p[‘cpu_percent’] > 50]
for process in high_cpu_processes:
# Kill or restart the process based on specific criteria
try:
process.kill()
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
“`
5. Network Configuration
Python can be used to automate tasks related to network configuration such as:
- Setting up IP addresses and subnet masks for networks
- Configuring DNS servers and domain names
Here’s an example script that uses the netifaces
library:
“`python
import netifaces
Get a list of network interfaces on the system
interfaces = netifaces.interfaces()
Set up IP address and subnet mask for a specific interface
interface_name = ‘eth0’
ip_address = ‘192.168.1.100’
subnet_mask = ‘255.255.255.0’
try:
# Get the existing configuration
ifconfig_output = netifaces.ifaddresses(interface_name)
existing_config = ifconfig_output[‘inet’]
# Update the IP address and subnet mask
new_config = {
'addr': ip_address,
'netmask': subnet_mask,
'broadcast': f"{ip_address}.{subnet_mask}"
}
# Save the updated configuration to the system's network settings
netifaces.ifaddresses(interface_name) = new_config
except ValueError:
# The interface does not exist or has no IP address.
pass
“`
6. Backup and Restore
Python can be used to automate tasks related to backup and restore such as:
- Creating backups of important system files (e.g., configuration files, log files)
- Restoring backed-up data to the original location
Here’s an example script that uses the tarfile
library:
“`python
import tarfile
Create a backup of all important system files
backup_dir = ‘/path/to/backup’
files_to_backup = [‘config.txt’, ‘log.log’]
try:
# Open the backup file for writing
with tarfile.TarFile(backup_dir + ‘/system_backup.tar.gz’, mode=’w’) as tar_file:
# Add each file to the archive
for filename in files_to_backup:
file_path = f'{backup_dir}/{filename}’
tar_file.add(file_path, arcname=filename)
print("Backup created successfully.")
except IOError:
# The backup file already exists.
pass
Restore data from a backed-up location
restore_dir = ‘/path/to/restore’
data_to_restore = [‘config.txt’, ‘log.log’]
try:
# Open the backup file for reading
with tarfile.TarFile(restore_dir + ‘/system_backup.tar.gz’, mode=’r’) as tar_file:
# Extract each file from the archive
for filename in data_to_restore:
file_path = f'{restore_dir}/{filename}’
tar_file.extract(filename, path=backup_dir)
print("Data restored successfully.")
except IOError:
# The backup file does not exist or has been corrupted.
pass
“`
7. System Updates
Python can be used to automate tasks related to system updates such as:
- Checking for available software updates
- Downloading and installing updates
Here’s an example script that uses the apt
library:
“`python
import subprocess
Check for available software updates
try:
# Run the apt update command
subprocess.run([‘sudo’, ‘apt-get’, ‘update’], check=True)
except FileNotFoundError:
# The apt-get command is not available on this system.
pass
Install updates
try:
# Run the apt install command
subprocess.run([‘sudo’, ‘apt-get’, ‘upgrade’, ‘-y’], check=True)
except (FileNotFoundError, subprocess.CalledProcessError):
# No updates are available or installation failed.
pass
“`
8. Disk Space Monitoring
Python can be used to automate tasks related to disk space monitoring such as:
- Checking for low disk space on specific partitions
- Sending alerts when disk space is running low
Here’s an example script that uses the psutil
library:
“`python
import psutil
Get a list of disk partitions on the system
partitions = [p.info for p in psutil.disk_partitions()]
Filter the list to only include partitions with low free space
low_disk_partitions = [p for p in partitions if p[‘free’] < 10 * (1024 ** 3)]
for partition in low_disk_partitions:
# Send an alert or notify the system administrator when disk space is running low
print(f”Low disk space detected on {partition[‘mountpoint’]}”)
try:
# Free up some disk space by deleting unnecessary files
subprocess.run([‘sudo’, ‘rm’, ‘-rf’, ‘/path/to/unwanted/files’], check=True)
except (FileNotFoundError, subprocess.CalledProcessError):
# The rm command is not available or failed to free up disk space.
pass
“`
9. Memory Monitoring
Python can be used to automate tasks related to memory monitoring such as:
- Checking for high memory usage by specific processes
- Sending alerts when memory is running low
Here’s an example script that uses the psutil
library:
“`python
import psutil
Get a list of running processes on the system
processes = [p.info for p in psutil.process_iter()]
Filter the list to only include processes with high memory usage
high_memory_processes = [p for p in processes if p[‘memory_percent’] > 50]
for process in high_memory_processes:
# Send an alert or notify the system administrator when memory is running low
print(f”High memory usage detected by {process[‘name’]}”)
try:
# Kill or restart the process to free up memory
subprocess.run([‘sudo’, ‘killall’, ‘-9’, process[‘name’]], check=True)
except (FileNotFoundError, subprocess.CalledProcessError):
# The kill command is not available or failed to free up memory.
pass
“`
10. System Logging
Python can be used to automate tasks related to system logging such as:
- Collecting and analyzing logs from specific systems or services
- Sending alerts when log messages indicate potential security breaches
Here’s an example script that uses the logging
library:
“`python
import logging
Set up a logger with a specific level (e.g., INFO, WARNING, ERROR)
logger = logging.getLogger(‘system_logger’)
logger.setLevel(logging.INFO)
try:
# Collect and analyze logs from specific systems or services
log_files = [‘log1.log’, ‘log2.log’]
for file in log_files:
with open(file, ‘r’) as log_file:
log_data = log_file.read()
logger.info(log_data)
print("Logs collected successfully.")
except IOError:
# The log files do not exist or have been corrupted.
pass
Send alerts when log messages indicate potential security breaches
try:
# Check for specific log messages that indicate security breaches
if ‘security_breach’ in logger.info:
print(“Security breach detected!”)
except NameError:
# The log message is not available.
pass
“`
These scripts demonstrate how Python can be used to automate various system administration tasks, such as backing up and restoring data, monitoring disk space and memory usage, and collecting and analyzing logs. By leveraging these capabilities, IT professionals can streamline their work, reduce errors, and improve overall system reliability and performance.