Table of contents
Introduction
We all know that ChatGPT is useful in many ways , in this article we are going to explore one such use cases , where ChatGPT is going to help a network engineer or system engineer to write some automation scripts to get our job done
Practical examples on using CHatGPT to generate automation scripts
Backup related script
In this example we are requesting ChatGPT to generate a script which helps us to backup files to a centralized server using scp as the transfer protocol as its encrypted and runs on ssh tunnels
#Request to ChatGPT Can you write a python script to perform hourly backup of a file to a centralized remote server using scp
#Response from ChatGPT
Certainly! Here’s a Python script that performs hourly backups of a file to a centralized remote server using the `scp`
command. This script assumes you have SSH access to the remote server and that`scp`
is installed on both the local and remote machines.#python
import subprocess
import time
def perform_hourly_backup(source_file, remote_server, remote_directory):
while True:
# Generate a timestamp for the backup file
timestamp = time.strftime("%Y%m%d%H%M%S")
# Define the backup filename on the remote server
remote_filename = f"{timestamp}_{source_file}"
# Run scp command to perform the backup
scp_command = [
"scp",
source_file,
f"{remote_server}:{remote_directory}/{remote_filename}"
]
subprocess.run(scp_command)
# Sleep for an hour before performing the next backup
time.sleep(3600)
# Example usage
source_file = "/path/to/local/file.txt"
remote_server = "username@remote.server.com"
remote_directory = "/path/to/remote/directory"
perform_hourly_backup(source_file, remote_server, remote_directory)
Make sure to replace the `source_file`, `remote_server`, and `remote_directory`
variables with your own values. The`source_file`
should be the file you want to back up, the`remote_server`
should be the SSH address of the remote server, and the `remote_directory` should be the directory on the remote server where you want to store the backup files.
The script runs in an infinite loop, performing backups every hour. It uses the `time.sleep()`
function to pause execution for an hour before initiating the next backup. The `scp`
command is executed using the`subprocess.run()`
function, which runs the command in the system’s shell.
Please note that you may need to configure SSH keys or provide a password for the remote server to establish a connection and perform the backup. Additionally, make sure that both the local and remote machines have the necessary permissions and network connectivity for the backup to work successfully.
Detecting host reachability
In this example we have requested ChatGPT to generate a script to ping a destination device and generate a syslog whenever the reachability is down. This script runs forever due to the while true loop. This helps a lot while troubleshooting. For example if we are monitoring whether reachability of a particular destination goes down in random time intervals , these generated syslogs for every 5 secs will help us pinpoint the event and its timestamp better. We also know when it recovered as the syslogs stops if it was reachable again.
#Request to ChatGPT Can you write a script using python to generate a syslog message when a ping fails for a destination
#Response from ChatGPT
Certainly! Here’s a Python script that generates a syslog message when a ping to a destination fails.#python
import subprocess
import syslog
import time
def ping(destination):
# Run ping command to check connectivity
ping_command = ["ping", "-c", "1", destination]
return subprocess.call(ping_command) != 0
def monitor_ping(destination):
while True:
# Perform a ping to the destination
if ping(destination):
syslog.syslog(syslog.LOG_INFO, f"[Ping] Ping to {destination} failed")
# Sleep for 5 seconds before the next ping
time.sleep(5)
# Example usage
destination = input("Enter the destination to ping: ")
monitor_ping(destination)
In this script, the user is prompted to enter a destination(IP address or hostname)
to ping. The`ping()`
function runs a single ICMP echo request to the destination using the `ping`
command. It returns `True` if the ping fails and `False` if it succeeds.
The`monitor_ping()`
function continuously monitors the ping status to the specified destination. If a ping fails, it generates a syslog message indicating the failure.
The script runs in an infinite loop, pinging the destination every 5 seconds. You can modify the sleep duration as per your requirements.
Ensure that you have the necessary permissions to run the`ping`
command and write to the syslog. The generated syslog messages can be viewed in the appropriate log files based on your system’s configuration(e.g., `/var/log/syslog`, `/var/log/messages`, etc.).
Conclusion
In this article , we explored how to leverage the ChatGPT to write Automation scripts to help network and system admins. Like this we can ask ChatGPT to write a lot of scripts based on our use case. We can also ask ChatGPT to customize and update the script if we want some slight modifications. This makes it very useful to generate some tailor made scripts based on the use case.
Occasionally some of the scripts generated might not work well due to some issues in the script , if you have a good understanding of python and other scripting languages we can easily fix it.
If you are interested in more articles on how ChatGPT can help system admins and network engineers, please check our ChatGPT series
https://discoveringsystems.com/category/chatgpt-for-network-engineers/