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.
Creating a Narrative
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).
Starting Simple
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:
Question: 1
Write a python3 script that logs into a debian server and runs a command
More Servers, No Passwords
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:
Question: 2
Use this script to loop through multiple servers with key based authentication
Arguments And Files
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.
Question: 3
Use this script to run multiple commands passed in from a file as a command line argument
Translating Python 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.
Question: 4
Create this script in Go
Conclusion
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?