Some Background

Last week I had some time off, in between jobs, and I chose to use one of those afternoons to implement a new script in of the new languages I’m expected to use in my new job.

Based on that, the options were:

PHP

Perl

ColdFusion Markup Language

ColdFusion would require me to run a server, I checked and the only good open source resource I was able to find was Railo which unfortunately has been down for quite awhile - off it goes.

PHP would have been a great choice but I have some light experience with PHP and an afternoon’s work wouldn’t help much more in the light knowledge I already have - off it goes.

In comes Perl, which I had no knowledge off and actually wanted to try.

Where the Idea Came From

During that same time period, while I was browsing across Hacker News, I came across this great little project written in Shell - wifi-password.

I found the script so simple and elegant that I wished I had thought about it and written it first. So I just decided to write one with Perl.

Even though it’s only supported in Ubuntu environments (at least that’s the only environment I have tested it with) I found it extremely interesting to write and learn the simple basics of Perl syntax which was my main goal.

The Actual Code

At the beginning we start by using strict and warnings along with a shebang:

#!/usr/bin/env perl
use strict;
use warnings;

We use use strict; because basically it prevents a shitload of, as per the documentantion, “expressions that could behave unexpectedly or are difficult to debug, turning them into errors”.

use warnings; is used to enabled the compiler warnings when interpreting the code.

NOTE: warnings should only be used for non-production running scripts. It’ll trigger warnings while the code is running so it will be annoying for end users if it is used.

I used my because I didn’t intend to write a Perl module neither did I intended for the variables to be used elsewhere other than this file. According to the documentation the my keyword restricts the scope of a variable to “A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval.”.

Then I went to define a simple subroutine that I could call whenever users needed some help:

After that we just call checkDependencies() and we are done with validation.

Next, we check if an SSID was passed. If it wasn’t we just go ahead and check the SSID to which we are connected at the moment. We do this using nmcli, grep along sed, inside a one liner, to retrieve the line from the nmcli which has the “connected” keyword.

We parse the result through a regex that will trim all whitespace that might still be in the string:

I really fancy unless for error handling. It’s a great addition to Perl.

Finally, we just verify that, in fact, there’s a network connection with the defined SSID (this is especially useful if the SSID was given instead of fetched), fetch the password from the NetworkManager files and print the password along with the SSID:

I found that NetworkManager stores its passwords inside files named with the SSID in /etc/NetworkManager/system-connections/[SSID] because of that there’s a need to run this script as a super user. I found this information inside the source code for wifi-password.

Even though it is a very simple script it was a manageable, interesting afternoon spent slowly building the script and learning Perl’s basics.