Skip to content

Knowing How To Write Code Is Liberating

Last August, I decided it was time to go back to school and finish up my degree. In some ways it feels like a pointless endeavor because I’ve been writing code for years now, and I often don’t feel like I learn much at school. That was until a few days ago. This semester I am taking an Information Security class, which is very exciting to me as InfoSec is one of my passions. In my class, we’ve started going over brute forcing passwords which includes generating word lists. Since this is an introduction class, I went into it with an attitude that it would be boring and I’d just waltz right through it (and so far I have), but recently this class forced me to think differently about education.

As I mentioned before, we are going over generating word lists. To ease our way into it, the first assignment was to generate a list of possible passwords that are 4 characters long and only numeric. The instructions for the assignment were to go download some software that would allow me to just click a few options and it would produce what I needed. My first thought was, “Ugh… I don’t want to go find this software and get it installed in my Windows VM just for this.” And that’s when my thought process changed. It occurred to me that I already have everything I need at my finger tips. I know how to write code. In just a few minutes I wrote a Perl scripts and then condensed it down to this one liner.

perl -e '$i=0;while($i<=9999){printf("%04d\n", $i++)}' > list.txt

Pretty simple while loop that prints out a number with zero padding to make sure its 4 characters long. I just turned a 30 minute project into a 5 minute project, and most of that time was because I originally wrote it as a script and then turned it into a one liner.

Then I started thinking about some problems I’ve solved lately.

My wife and I use OSMC (Open Source Media Center) on a RaspberryPi for watching TV. Sometimes the Pi freezes and I have to SSH into and manually reboot it. This is great until it freezes and I’m at work. My wife is a very intelligent person, but she’s not really into learning how to SSH into something and reboot it. So I wrote a quick little desktop program for it.

use strict;
use warnings;
use Gtk2 -init;
my $output = `ping -c 1 osmc.local | head -1`;
$output =~ /(\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3})/;
my $pi_ip = $1;
my $window = Gtk2::Window->new('toplevel');
my $restart = Gtk2::Button->new('Restart');
$restart->signal_connect(clicked => sub { my $cmd = qq{ssh -o "StrictHostKeyChecking no" -i /path/to/key osmc\@$pi_ip '/usr/bin/sudo /sbin/shutdown -r now'}; `$cmd`; Gtk2->main_quit });

Yeah, that’s some pretty rudimentary regex, and I’m using backticks instead of something like IPC::Open3, and I’m using an SSH key with a blank password, and why am I getting the IP from the output of <code>ping</code>? I have me reasons. And I didn’t write this to be the most secure thing possible. I wrote it to give my wife a way to click a button to restart the Raspberry Pi when I’m away from home. I’ll probably rewrite it 5+ times just because I thought of a different way to do it. (Or not, because I recently got a FireTV Stick and side loaded Kodi into it.)

One more example. I use Pianobar (a CLI wrapper to Pandora). I use vim for writing code so I’m virtually always in a shell. It makes it easy to switch to a different tab to manage what I’m listening too. I’ve also used terminal-notifier (a pretty cool way to generate notifications on a Mac). Pianobar provides an event API. For example, when a song starts it will emit an event that gives the name of the song, the artist, and some other information. So I wrote a script that listens for that event to give me a notification when a new song starts.

use strict;
use warnings;
use IPC::Open3;

if (my $action = shift @ARGV) {
if ($action eq ‘songstart’) {
my $info = {};
while () {
chomp $_;
my @thing = split /\=/, $_;
$info->{$thing[0]} = $thing[1];

my @cmd = (‘/usr/local/bin/terminal-notifier’);
if ($info->{coverArt}) {
push @cmd, (‘-appIcon’, $info->{coverArt});
if ($info->{artist}) {
push @cmd, (‘-subtitle’, $info->{artist});
if ($info->{title}) {
push @cmd, (‘-title’, $info->{title});
push @cmd, (‘-sender’, ‘1’, ‘-remove’, “‘ALL'”);
if ($info->{stationName}) {
push @cmd, (‘-message’, $info->{stationName});
my ($in, $out);
my $pid = open3($in, $out, $out, @cmd);
waitpid($pid, 0);
if ($action eq ‘userlogin’) {
print “\n\n”;
printf “%20s\n”, ‘HELLO GARTH’;
print “\n\n”;

Here’s an example of the notification.

Terminal Notification




It’s been really handy. An addition I plan on making is making the notification clickable to skip the song.

UPDATE: Today, I wrote the addition to skip songs by clicking the notification. Its been surprisingly awesome. I wish I would’ve done it a little sooner.

I’ve learned that education isn’t about bending my mind to fit into the paradigm a teacher imposes upon me, which seems to be what a lot of my college professors believe education is. Education is about building upon the knowledge I already have. Education is improving my skills. Education is learning how to think.

Published inCodeeducationliberationperl