Anecdotes from a Linux Systems Administrator. /root

Home About Books Blog Portfolio Archive

Tag Cloud

Currently Reading

MCA Microsoft Certified Associate Azure Administrator Study Guide: Exam AZ-104
MCA Microsoft Certified Associate Azure Administrator Study Guide: Exam AZ-104
308 / 435 Pages

Latest Book Reviews

Latest Posts

July 12, 2016

Python if __name__ == '__main__' Ruby equivalent

by Alpha01

Python is by no chance my favorite language to work in, however I always loved the way you can beautifully write your modules and easily test them via if __name__ == '__main__' statement. I’ve been doing a lot of Ruby programing these past few weeks, and I came across a situation were I needed this exact feature in Ruby.

My problem

I needed to run some unit tests to a Ruby based TCP server that gets spawn as daemon. The program is completely command line, and once spawned, their isn’t any code to communicate with its child process. The unit tests itself aren’t exactly to complicated. I simply need to make sure that the TCP server starts, verify the status of it using its PID, and be able to kill the process. I needed to run the unit tests without heavily modifying the existing program, and the best best to accomplished it was using a similar if __name__ == '__main__' Python approach. Lucky for me, in the Ruby world we can accomplish the awesome if __name__ == '__main__' Python statement via if __FILE__ == $0.


Here is a test module called test-module.rb

#!/usr/bin/env ruby

if __FILE__ == $0
  puts "Executed via command line."
  puts "Included."

Now, if we run this test-module.rb from the command line, the if __FILE__ == $0 block will evaluate to true.

alpha03:tests $ ./test-module.rb
Executed via command line.

If the module gets included the ```if FILE == $0`` block will evaluate to false. Example script called test.rb

#!/usr/bin/env ruby

require './test-module'

Running the test.rb script that required test-module.rb

alpha03:tests tony$ ./test.rb


Ruby rocks!

Tags: [ ruby python ]
April 21, 2016

Time Stamping Bash's command history

by Alpha01

It seems sharing your dot (.) config files is an act that all cool kids do these days. I won’t be sharing my Bash configs, however I will share one cool Bash shell trick of time stamping your command history, I use in all of my systems and servers. This is accomplished using the HISTTIMEFORMAT environment variable. Using standard date format control output syntax, it’s fairly easy to customize the command history time stamp to whatever time format output you prefer. Finally, in addition to using a customized HISTTIMEFORMAT value, I also add the HISTSIZE environment variable. This environment variable lets you override the default command history count to a much larger history count.



Sample output:

 3318  21/04/2016 04:42:20 ls
 3318  21/04/2016 04:43:29 vim .bashrc
 3319  21/04/2016 04:43:37 vim .bash_profile
 3320  21/04/2016 05:03:58 man date
 3321  21/04/2016 05:07:43 history |tail
 3322  21/04/2016 05:07:47 history |tail -5
Tags: [ bash ]
April 20, 2016

Best scripting/programming language to learn from an Ops perspective

by Alpha01


Knowing some sort of programming is an absolute must for any systems administrator. Hell, even in the Microsoft world, the necessity can’t be anymore apparent with the creation of PowerShell. So what is the best language to learn for a beginner? This is a question that many junior sysadmins or students asked themselves when starting of.

Having been a systems administrator for well over eight years now, and worked with Bash, Perl, PHP, Python, and Ruby extensively within those eight years. I think this is a two part answer. In my opinion the best language to start is Bash/shell. Bash/shell is everywhere. Having an advanced Bash knowledge automatically makes you a better sysadmin. This is because Bash/shell scripting is practically glued to other external *nix userland tools (grep, sort, tail, awk, sed, etc..). Knowing Bash extensively, makes you a better command line user. The second of the answer to which is the best scripting/programming language to learn, depends on what environment you work in, or want to work in. If you work or want to work in a Ruby on Rails shop, then obviously you would want to opt learn Ruby, likewise if you work in a WordPress shop, you should opt for learning PHP.

Programming vs Scripting

A systems administrator is NOT a software engineer. There is a huge difference between scripting some sort of convoluted process (automation) and in architecting a complex piece of software. So it is important to acknowledge that the skill set between the both is not the same. Most of the religious wars between a programming language preference people love to display on the internet, is mainly from a developers perspective. Whether the arguments are valid or not, in my opinion they are irrelevant from a sysadmin’s point of view. Basically we must know how to use an API and not necessarily how to create one. While in fact it helps to know how to construct your objects by knowing how to create elegant classes, methods, and attributes. It’s not vital for a sysadmin to know the exact details on how the API/library was created.

Programming Experience

Once you know a scripting language reasonably well. By knowing either Ruby, Perl, Python, or PHP, you practically can’t avoid being already familiar with any of the other scripting languages mentioned. These languages are so similar, it’s fairly easy to pick up book and learn another scripting language in a short time span.

It’s no secret Ruby is my favorite language, however I’m not overly religious about it. It’s not the end of the world if I don’t get to use Ruby everyday. In fact, if I need to write a script really fast. Usually my first choices are either Bash or Perl. This is mainly because these are the languages I’ve known the longest, and for reason I’m able to program in much faster. For me it all boils down to which language will take me less time hack around with and which tool is best for the job. This usually means, is there an existing program, API/library out there that does much of the work I need to solve my problem.

Thus said, when I first started my career, Bash and Perl the defacto scripting languages for a *nix admin to know, now in days it seems Python and Ruby are what companies prefer. Even now with the popularity of NodeJS, soon JavaScript will come into the front along with the other traditional major scripting languages.

Happy hacking!

Tags: [ php perl go python bash ruby ]
December 8, 2015

Packt Publishing Free E-Books crawler

by Alpha01

I’m a big fan of Packt Publishing, and have purchased quite a few books from them. So when I first heard a couple of months back that they were going to give out free e-books everyday, my jaw literally dropped.

I’ve normally been manually checking the site everyday for books that I might be interested on reading. The problem with this, is that their have been days that I missed out getting some free books that I would’ve love to read. So I wrote a short script that would notify me if there’s a free book available that I might be interested in reading. I would’ve love if Packt Publishing provided an rss feed so I can easily get notifications of their free books. Thus said, I really can’t complain since they’re already kind enough to give the world free e-books to spread knowledge.

Tags: [ php ]
November 9, 2015

Locking Down WordPress Access with Varnish 3.x

by Alpha01

I have Varnish in front of all my WordPress sites and configured all /wp-admin traffic use https via Nginx. See

So to lock down access to my WordPress site’s requires both Varnish and Nginx configs to be modified.

Block at the http Varnish level:

sub vcl_recv {
    if ((req.url ~ "wp-(login|admin)") && (client.ip !~ MY-IP-ADDRESS)) {
                error 403 "Fuck off";

Block at the https Nginx level (using as an example):

location /wp-admin {
        allow   MY-IP-ADDRESS;
        deny all;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
location /wp-login.php {
        allow MY-IP-ADDRESS;
        deny all;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
Tags: [ security varnish nginx wordpress ]