NFS: List exported filesystems

If you want to list what filesystems are exported via NFS on a remote host use this command:

showmount -e

You should get output like this:

Export list for
Leave A Reply

SSH: Batch mode for SSH/SCP scripting

SSH is great for scripting file transfers between two trusted hosts when you're using SSH keys. If you are using SSH keys to automate SSH commands you will want to make sure your SSH commands are using BatchMode. With BatchMode enabled the SSH connection will fail immediately if the key is rejected, instead of failing back to a password prompt. This will prevent your scripts from "hanging" while it waits for you to type a password.

ssh -o BatchMode=true
scp -q -o BatchMode=true
Leave A Reply

PHP: is_exception()

I have an object in PHP that I need to check if it is an exception and act appropriately. Surprisingly PHP does not have an is_exception() function built in so I had to write my own:

function is_exception($obj,$strict = false) {
    if (!is_object($obj)) {
        return false;

    // It's some type of exception
    if ($obj instanceof Exception) {
        if (!$strict) {
            return true;
        // It's a raw exception
        } elseif (get_class($obj) === "Exception") {
            return true;

    return false;

If you pass true as the second parameter it will only return true if it's a raw exception and not an inherited exception.

Leave A Reply

PHP: Relative path between two directories

I need to calculate the relative path between two directories such that:

$a = "/one/two/three/four/";
$b = "/one/two/";

$a to $b has a relative path of ../../ and $b to $a has a relative path of three/four/. This function solves that problem very simply:

// Borrowed from and cleaned up
function relativePath($from, $to, $ps = DIRECTORY_SEPARATOR) {
    $arFrom = explode($ps, rtrim($from, $ps));
    $arTo   = explode($ps, rtrim($to, $ps));

    while(count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0])) {

    return str_pad("", count($arFrom) * 3, '..' . $ps) . implode($ps, $arTo);
Leave A Reply

Perl: Natural Sort

I have a array with a bunch of names like vlan-1, vlan100, vlan34 which do not sort appropriately using Perl's standard sort() function. Sort::Naturally to the rescue! I didn't want to install an entire module for one sort operation, and require a dependency, so I ripped out just the natural sort function and included that in my script.

sub nsort {
    my($cmp, $lc);
    return @_ if @_ < 2;   # Just to be CLEVER.

    my($x, $i);  # scratch vars


    sort {
        # Uses $i as the index variable, $x as the result.
        $x = 0;
        $i = 1;

        while($i < @$a and $i < @$b) {
            last if ($x = ($a->[$i] cmp $b->[$i])); # lexicographic

            last if ($x = ($a->[$i] <=> $b->[$i])); # numeric

        $x || (@$a <=> @$b) || ($a->[0] cmp $b->[0]);

    map {
        my @bit = ($x = defined($_) ? $_ : '');

        if($x =~ m/^[+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee](?:[+-]?\d+))?\z/s) {
            # It's entirely purely numeric, so treat it specially:
            push @bit, '', $x;
        } else {
            # Consume the string.
            while(length $x) {
                push @bit, ($x =~ s/^(\D+)//s) ? lc($1) : '';
                push @bit, ($x =~ s/^(\d+)//s) ?    $1  :  0;


This is a slightly more portable version rather than maintaining the Sort::Naturally dependency.

Leave A Reply

Bash: Use previous expression parameters

I was running a bash command similar to this:

perl /var/www/scott/perl/test/ --size 10 --file /tmp/output.txt

and afterwards I wanted to make a small change to that script. Rather than typing out the full path you can use some Bash history expansion and do this instead:

vim !:1

If you use !: followed by a number, Bash will replace that with the Xth (starting at zero) parameter from the previous command.

Leave A Reply

Reddit: Pinky and the Brain theory

Mind Blown!

The Idea is, that Pinky is in fact, a genius who lives with the insane Brain to keep him in check and to make sure he doesn't do anything too dangerous. There is a good amount of evidence to support this in the show.

We see a few examples, starting with the introduction song. There is a line that goes, "One is a genius, the other is insane, pinky and the brain." We already see a few hints from this. During the course of the show, there is never a question that pinky is not insane. An imbecile? Most certainly, but we never have a reason to believe he is insane. On the other hand, Brain is clearly insane - he is constantly trying to devise ways to take over the world. If Brain is infact insane, this leaves pinky to be, well, the genius

The biggest piece of evidence however, comes from an episode where Brain creates a machine to make Pinky smart. Pinky steps into it and begins to massively outsmart Brain - winning game shows, correcting him, and at the end of the episode - He shows how Brain's machine is inherently flawed and doesn't work at all. Almost immediately afterwords he returns to his bumbling self.

The begs the obvious question - if the machine never worked, how was Pinky so smart, and able to actually point out this design error? Simple, he is infact the genius.

Finally, there's the fact that Brain's inventions almost never work - they blow up in his face, and when they do work, they are sabotaged by none other than Pinky.

There's several more examples within the show, but when you look at the whole it becomes very convincing, that Brain is in fact being watched by Pinky, who is playing off the whole affair.

Borrowed from Ginnex on

Leave A Reply

SQLite: Case insensitive search

If you want to search a field in SQLite you might use a query like this:

SELECT MyID FROM Table WHERE Name = 'Doolis';

This performs a case-sensitive search on the Name field. If you want to search the name field in a case-insensitive manner do this:

Leave A Reply

Linux: Fedora 22 major package versions

Fedora 22 has been released and I gathered the versions of some core packages:

Package Version
Perl 5.20.2
PHP 5.6.9
Vim 7.4.640
Apache 2.4.12
Kernel 4.0.4
Leave A Reply

Linux: Reverse path forwarding headaches

The Linux kernel has a security feature called Reverse Path Forwarding which is designed to ensure that incoming packets are valid for your network. It validates that a packet arriving via a given interface has a valid IP address for that interface. In some situations a packet can arrive on one interface, and leave on a separate interface. If you have a packet like this Reverse Path Filtering kicks in and drops that packet.

This manifests in that you can see the packet arrive (via tcpdump) but nothing after that (i.e. the packet doesn't leave). The Linux IP stack drops the packet before any routing or service can act upon the packet. To log affected packets to syslog you can run:

echo 1 > /proc/sys/net/ipv4/conf/<interfacename>/log_martians

To disable this check completely you can run the following command:

for i in /proc/sys/net/ipv4/conf/*/rp_filter ; do
    echo 0 > $i 

Valid options for rp_filter are:

0 - No source validation.
1 - Strict mode as defined in RFC3704 Strict Reverse Path - Each incoming packet is tested against the FIB and if the interface is not the best reverse path the packet check will fail. By default failed packets are discarded.
2 - Loose mode as defined in RFC3704 Loose Reverse Path - Each incoming packet’s source address is also tested against the FIB and if the source address is not reachable via any interface the packet check will fail.

You can view the current settings for each interface on your box with this command:

sysctl -a | grep -E "net.ipv4.*\.rp_filter"

To make any changes permanent across a reboot set them in /etc/sysctl.conf

net.ipv4.conf.eth0.rp_filter = 0

Keywords: route, loop, egress, ingress, alien

Leave A Reply

Perl: Working with columnar data

I have a text file of data that is in whitespace separated columns that I need to work with. Perl has a command line option -a to enable auto-splitting the input into an array called @F. Using a Perl one-liner you can automatically split at whitespace separation like this:

cat /tmp/file_list.txt | perl -lane 'print "mv $F[3] $F[1]"'

This will output mv commands to rename the file in the 4th column to the 2nd column.

More information available in perlrun.

Leave A Reply

Linux: Debian 8 major package versions

Debian 8 has been released and I gathered the versions of some core packages:

Package Version
Perl 5.20.2
PHP 5.6.7
Vim 7.4.488
Apache 2.4.10
Kernel 3.16.0
Leave A Reply

Vim: Creating a portable copy of your configuration

If you use Vim on any regular basis you've probably created your own custom .vimrc file, and maybe installed a plugin or two. This config is machine specific and is not the easiest thing to move from one machine to another. I found this cool project called myvim that packages up your entire Vim installation into a single portable file. This file is a self-extracting archive of your Vim config that you can transfer to a new machine.

myvim -j /tmp/vim.bakers

This will create a file /tmp/vim.bakers which you can transfer and then run on a new machine.

Leave A Reply

Fedora: Force memcached to only listen on

I've been toying with memcached lately and wanted to ensure that it was only listening on On Fedora this is controlled by the file /etc/sysconfig/memcached. Make sure that the OPTIONS line contains -l

$ cat /etc/sysconfig/memcached 

Leave A Reply

Linux: Count CPU instructions

Zend published this infographic and it got me thinking about CPU instructions. According to the graphic, the Wordpress homepage required 9.4 billion machine instructions to render, but they've optimized PHP7 and it's now down to 2.6 billion. To count CPU Instructions on a Linux box you can use the perf command:

perf stat -e instructions <my_command>

In comparison, some very simple Linux commands take a significant amount of instructions:

# Approximately 640,000 instructions
perf stat -e instructions echo '' 

# Approximately 2 million instructions
perf stat -e instructions cd ~

# Approximately 700,000 instructions
perf stat -e instructions clear
Leave A Reply