Using ChatGPT for automating your Cisco ASA tasks
Introduction 123
OpenAI ChatGPT is a natural language processing tool that allows users to communicate with a computer or system using natural language commands. It is part of the OpenAI GPT (Generative Pre-training Transformer) family of language models and was designed to provide a flexible and powerful platform for natural language processing tasks, including chatbot development and automated code generation.
One of the key features of OpenAI ChatGPT is its ability to automate code writing tasks. By using natural language commands, users can instruct ChatGPT to generate code in a variety of programming languages, including Python, Java, and C++. This can help improve the efficiency of code writing and reduce the risk of human error, particularly for complex or repetitive tasks.
In addition to code generation, OpenAI ChatGPT can also be used for a wide range of other natural language processing tasks, such as language translation, text summarization, and chatbot development. It is highly customizable and can be trained to understand specific commands and contexts, making it a versatile and powerful tool for natural language processing.
Use case for network/security engineer
OpenAI’s ChatGPT is definitely a game changer in many areas. It is able to automate a lot of simple (and not so simple) tasks. I asked it to create a Python script that will be able to do something that we are already doing with our own script.
My query was something like this: Create a python script, based on Netmiko, that will check the list of Cisco ASA firewalls from “firewalls.txt” file, and then run the list of commands on them from the other file, named “commands.txt” and save the output from every firewall into separate text file. Ask the user to provide username and password when running a script.
And the result was exciting. It understood my request pretty well and managed to create a piece of code that did what I was asking about. With very few small updates that script was ready to go.
Here is the code it generated (my few updates were formatting-related):
import getpass
import datetime
import os
from netmiko import ConnectHandler
# Read the list of firewalls from the file
with open('firewalls.txt', 'r') as f:
firewalls = f.read().splitlines()
# Read the list of commands from the file
with open('commands.txt', 'r') as f:
commands = f.read().splitlines()
# Prompt the user for the username and password
username = raw_input("Enter the username: ")
password = getpass.getpass("Enter the password: ")
print("Starting collecting details, be patient")
def run_commands(firewall, name):
# Connect to the firewall using Netmiko
device = ConnectHandler(ip=firewall, device_type="cisco_asa", username=username, password=password)
# Run the "terminal pager 0" command to disable paging
device.send_command("terminal pager 0")
# Get the hostname of the firewall
hostname = device.send_command("show hostname")
# Run each command on the firewall and save the output
output = ""
for command in commands:
result = device.send_command(command)
output += "{}\n{}\n".format(command, result) + "\n" + ("-"*30) + "\n"
# Write the output to a file named with the corresponding line from the firewalls.txt file
filename = "{}.txt".format(name)
filepath = os.path.join("output", filename)
with open(filepath, 'w') as f:
f.write("="*30 + "\n" + hostname + "="*30 + "\n" )
f.write(output)
# Run the commands on each firewall
for firewall in firewalls:
run_commands(firewall, firewall)
print("Done! Check the results in output folder")
Summary
It is really awesome what we can now achieve without even knowing any programming language (ok, maybe knowing basics only) but being able to properly query chatGPT and give it well understood tasks.