Code from an old Python game I use to work on

The Setting

You work in an office, you sit at a desk, looking at word documents, spreadsheets, *shutters* PDFs, and it’s TEDIOUS. This isn’t 2007 anymore, why are we doing this to ourselves? Sometimes it’s out of your control, but if you do have a level of control over whatever process is responsible for dropping a spreadsheet in your lap that you have to go manually edit, you can automate it!

If you’ve never coded before, you should pick it up. Plain and simple. Let’s talk about why and then I will provide some great training resources, and in future articles I will do my best to create generic, easily editable scripts that you can use in a day to day basis. 

Why should you learn to code?

Let me get this out of the way, I don’t expect anyone reading this to be able to develop Enterprise grade applications, or even have the desire to. What I mean when I say ‘learn to code’ is to simply be able to automate the boring stuff, ironically this is one of the training courses I recommend. 

Let’s look at it this way, if there is some task at work that you do multiple times a day, week, month, whatever that you do the same way each time (or close to the same way). Then you should look into automating it. Here is a great example that I encountered a few years ago:

I use to work at a smaller company, maybe a few hundred computers: laptops, desktops, servers, some network gear, and switches. We needed to keep a running inventory each month of the active machines on our network. I was responsible for updating that spreadsheet I inherited from the previous person. It looked something like this:

IP Address Hostname OS Last Active Decommissioned?
192.168.1.56 example1.domain.net Windows 7 11/12/16 no
192.168.1.57 example2.domain.net Windows 7 11/12/16 no
192.168.1.58 example3.domain.net Windows XP 10/1/14 yes
192.168.1.59 example4.domain.net RHEL 6 11/12/16 no

You get the idea. 

I would have to go through all of the rows, ping the IP from a specific server that had access to everything on the network, and record the data. Since we were a smaller company, we didn’t have the luxury of a true inventory system and the patch manager software (No idea what that was), didn’t give us everything we needed to maintain the inventory through that alone. I spent some time trying to figure out what my options were. I finally stumbled on bash. Since we were using a linux machine to ping all of the devices, I figured bash would work well. I did some basic training courses and began working towards my goal. 

The goal

I had a rough idea of what I wanted this script to do:

  • Ping the devices from a spreadsheet
  • If a response was received, record that in a spreadsheet
  • Update the Last Active column
  • Update a few other columns I didn’t add here because I forgot all of them.. it’s been a few years
  • Email me the output and save a copy to a NAS device

Simple right? Kind of. I ran into some issues, mostly from lack of knowledge, but after some tinkering around I got a working script. 

It was able to take a list of IPs, ping them, grab any additional details it could, like the hostname, and email me those results. I could then take those and update the spreadsheet manually. It was faster, though I felt like I should be able to do more. I wasn’t getting good answers from the bash side of things. That could be from lack of knowledge overall on what to search, or I just suck at Googling. 

I eventually found myself looking at Python scripts from an old StackOverflow post. I picked up Python much quicker and also noted that the linux server had Python installed natively. I wrote a quick ping script to recreate what I made in bash, into Python. It did okay, I still had to create a mail script in bash and call that in the Python script, but within a few days, I had it back to working order. The next step was to create a spreadsheet within Python that was an exact replica of the original, without having to open Excel on my own. 

I found a few Python modules to help with this and got to work tinkering and testing the script. After a few more days of working in my spare time I finally had a completely working script. I even learned about cron jobs in the process. After all that, I was able to set my script to run each month on the same day, perform the task, and email me the results. I took a look at one of the spreadsheets and compared it to an old, manual copy. I noticed something interesting; the original spreadsheet had about 400 rows or so, and the new one had over 500! Oh no, it’s broken again. 

To make a long story short, I actually discovered that my automated script was actually right. Over time, we had somehow lost track of some of the newer devices on the network, and some hosts that were decommissioned were actually still online, the ping had just failed one month and it was removed from the spreadsheet. So, not only did my script save my some headache, it also got us better data!

Ok, So What? You’re A Nerd, That’s What You Do.

True, random header, I do consider myself a nerd. However, I knew nothing about programming a few weeks prior to this. I was a young, college dropout with a basic job and no direction. So with that, I believe that anyone could, and should, learn to code. I’ve been developing scripts and some random applications for a few years now. I am no expert but I have learned quite a bit. If you work in an office, whether you work with nerds or you do sales, there is most likely an automation solution you could do. Think about it, if you work on a computer all day, you’re working in different applications: Word, PowerPoint, Chrome, some super expensive product your company bought so you could do you job. All of those were developed by someone. 

You could easily learn to create a Python script that manipulates a spreadsheet in a programatic way to speed up your job and give you more time to surf Reddit… I mean, get more work done! 

I don’t think, especially with how easy programming languages are to understand versus 20 years ago, that it’s difficult to get started coding. I believe it all comes down to your level of commitment. If you have no interest in coding (then I assume you just like my articles: Hi by the way!), then you probably won’t go very far in learning to code. If you want to but don’t know how to get started, allow me to give you some guidance.

The Decision

You first need to decide what language makes the most sense to work in for your setting. If you work with Web applications all day (you go to a website and work in there most of the day) then you might want to look into JavaScript. If you work on a large number of platforms, Windows, Mac, Linux, Web apps, Excel, whatever, Python might make more sense for you. 

I know enough about JavaScript to be dangerous, but Python is where I live most of the time, so I am going to focus on that for now. Maybe if I get better at JS then I can write some posts to help you get started there. I will try to come back and link anything I post to this article so I can help future readers really get started. 

There are more options than just Python or JavaScript. PowerShell is a good choice for interacting with Microsoft products. Java is good for… something I’m sure. Bash is good if you live in Linux (though I have friends who would argue that Python is better and can do the same functions). At the end of the day you pick what you want. I personally, highly recommend Python. It’s super easy to understand and a great first language for most people. 

Side note: Make sure you use Python 3, Python 2 is still around but it’s not going to be supported much longer. 

Training Courses

I already linked Automate the Boring Stuff above, but here it is again. Al Sweigart knows what he is talking about.

Code Academy is a great place to start and get familiar with the syntax, but overall it doesn’t go too in depth with the language. I recommend doing the first few chapters and then moving on to something else. 

Youtube has a large number of python tutorials. A quirky, but effective one that I found recently and I really love is Socratica. They have lots of stuff on the channel, but check out their python playlist. They broke everything down into short, easy to digest bites with examples you can follow along with and they explain things really well. I highly recommend visiting them at some point. They also give good use cases as to when and why you would want to use these different functions and techniques in your job. 

If any of this was helpful, feel free to leave a comment, subscribe. Let me know what you think and if you’re interested in seeing some examples, let me know and I will dust off my old scripts, update them and post them when explanations. 

The Actual Why

Since I haven’t explained the benefits other than it might make your life easier at work, I should add, you will also look like a rockstar. If you work in an office setting where you’re not surrounded by nerds, being able to code, even at a basic level, will help you stand out from everyone else in the office.

If you’re not interested in that, then why are you reading this article? Go somewhere else. If you are interested in looking like a genius, then I highly recommend learning to code, doing a few projects at home or for your office, then show off your skills to the world! Or, at least the people you work with and your boss.

Published by Guindel

90's nerd, dad, retro computer enthusiast. I created a blog as a way to talk about things I find interesting, care about, and try to help other people in similar places. Hit me up if you want me to write about something!

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: