Configuring Systems With ChatGPT, Python, and Go

I recently wrote a blog about generating Terraform with ChatGPT. In that experiment, I was using GPT-3.5. It didn’t take much to trip up the galactic AI in many of the experiments I did with Terraform and beyond. According to OpenAI GPT-4 soars past its predecessor in its advanced reasoning capabilities. Let’s use this model to build code in Python and Go. How will the super titan fair in the Sysadmin arena? Let’s take on some tedious backend work head-on with the biggest brain around and see what happens.

Intro
Intro

In the vast expanse of the Digital Frontier, there were the sysadmins. These Server Sheriffs were the guardians of data prairies. Notorious for their unwavering dedication to their craft, Server Sheriffs ensured that systems always stayed up (and up to date). When I did a lot of sysadmin work, AD-HOC scripting was serious business.

Let’s not kid ourselves. AD-HOC scripting is STILL serious business. One common task was logging into a ton of systems and running a set of commands. For this little experiment, I launched three VMs running Debian 11.3 in the lab - (VMware ESXi hypervisor).

In my experience testing ChatGPT so far, I have found a lot of success in asking a simple question first and then refining the output with more thoughtful questions once I have an idea of what it is throwing back at me. Let’s start with the following:

Write a python3 script that logs into a debian server and runs a command

Question 1
Question 1

Observations
The first pass was a success! Even though I’m a cowboy, I’m hard-coded to not hard-code passwords in files. Before I ran the script, I copied it out of ChatGPT, and tweaked the password var to use getpass. I also filled in the username, server IP, and command. To that point, I would rather not use passwords at all. And logging into a single server is hardly useful. Logging into many servers is. Let’s ask ChatGPT to help us out here.

At this point, I put on a fierce glare, look the AI straight in the eyes, and murmur, “Is that all you got, you bucket of bolts?” For the second pass I created a key pair with ssh-keygen and SCP’d it to each of the Debian VMs. And with that, I ask my next question:

Use this script to loop through multiple servers with key based authentication

Question 2
Question 2

Another Small Step
This question only required a little of an update to the script. Merely updating the function input to key_filename, adding a variable for the key, and constructing a For Loop for the cattle, I mean servers. I went ahead and switched out the command to apt-get update. Anyone could build this script, right? After testing, I glanced over and saw ChatGPT’s spurs glinting like silver in the blazing sun.

One common task I would do in my sysadmin days was, adding multiple commands in a file and passing that file into the script as a command line argument. This is something I started doing many moons ago first with bash scripting, then Perl, and onwards to Python. This was a great approach to automation before APIs took over the world.

Use this script to run multiple commands passed in from a file as a command line argument

Question 3
Question 3

Winner Winner
I added some commands in commands.txt in my local dir and passed it in with my command. Not surprisingly at this point, everything was successful. In the middle of the town, as the church bell struck high-noon, I took a sideways glance at ChatGPT and tipped my hat. ChatGPT glanced back at me as a tumbleweed passed quietly between us. The AI Titan then murmured, “Would you like me to translate that script to GO?”

A long time ago, I worked on a project that took several enormous Perl scripts and attempted to transpose them to Python. These scripts were hacked together like nothing I’d ever seen. We quickly realized the best path forward was starting from scratch. I have been spending more time in Go lately, so let’s see if our AI Superpower is multilingual.

Create this script in Go

Question 4
Question 4

Color me Impressed
In some of my testing, I went back to some fairly dated Perl scripts I had lying around and had ChatGPT translate them to some of the newer languages the cool kids are using now. I then opened a new chat and had this rattletrap convert the new scripts back to Perl and thought, so that is what really efficient Perl would have looked like!

Asking ChatGPT questions is kind of like building an MVP (minimum viable product). You don’t ask for the finished product on the first pass. That initial request forms the baseline, e.g., I want a script that can connect to this and run that. You, as the consumer, can then do some testing and then provide feedback by means of asking additional questions. This makes me think of a customer feedback loop. In this case, though, you are the customer and, to some extent, the builder. In my next round of experiments, I’ll be setting my aim on networking. Perhaps some OSPF and BGP?