Fonts: Comic Neue aka "good" Comic Sans  

Comic Sans has long been the whipping boy of bad fonts. There are many other good light/silly sans-serif fonts available, but Comic Neue is a great free alternative. If you must use a font like this, at least use a good one.

Leave A Reply

Javascript: Random element from array  

Function to get a random element from a Javascript array.

function rand_elem(items) {
    var item = items[Math.floor(Math.random() * items.length)];

    return item;
}
Leave A Reply

Perl: ANSI colors  

Perl's Term::ANSIColor is good but sometime it's overkill. I wrote a function to change colors before your print.

$color = color("13_on_5");
$reset = color("reset");

print $color . "Pink on purple" . $reset . "\n";
# String format: '115', '165bold', '10_on_140', 'reset', 'on_173'
sub color {
    my ($fc,$bc,$bold,$ret,$str) = '';
    $str = shift();

    # No string sent in, so we just reset
    if (!$str || $str =~ /reset/i) {
        return "\e[0m";
    }

    # Get foreground and bold
    ($fc,$bold) = $str =~ /^(\d+)(b|bold)?/g;
    # Get the background color (if present)
    ($bc)       = $str =~ /on_?(\d+)$/g;

    if ($bold) { $ret .= "\e[1m"; }
    if ($fc)   { $ret .= "\e[38;5;${fc}m"; }
    if ($bc)   { $ret .= "\e[48;5;${bc}m"; }

    return $ret;
}

The ANSI color numbers can be determined using term-colors.pl.

Leave A Reply

PHP: Get first and last elements of an array  

I often need to grab the first or last element of an array. This method works for either numeric or associative arrays.

$array = array('cats','dogs','fish','horses','squids');

$last  = end($array);   # 'squids'
$first = reset($array); # 'cats'
Leave A Reply

PHP: Test for numeric and associative arrays  

I wanted to test if a PHP array was associative or numeric. Here are two functions that will work for that.

function is_assoc_array($arr) {
    if (!is_array($arr)) { return false; }

    return array_keys($arr) !== range(0, count($arr) - 1);
}
function is_numeric_array($array) {
    if (!is_array($array)) { return false; }

    foreach ($array as $a=>$b) {
        if (!is_int($a)) {
            return false;
        }
    }
    return true;
}
Leave A Reply

PHP: Test for PHPUnit  

I needed to short-circuit a logging routine because every time I ran PHPUnit it would put a bunch of bogus entries in my logs. Here is a simple test to see if we're inside of a PHPUnit test.

function in_unit_test() {
    $ret = class_exists('PHPUnit_Framework_TestCase');

    return $ret;
}
Leave A Reply

IPTables limit source packet rate  

Due to recent NTP reflection attacks I wanted to lock down my server. I want to rate limit incoming packets on port 123 (NTP) by source address. A single NTP source should not send more than 10 packets every 30 seconds. The following IPTables rule will track incoming NTP traffic and ensure no single IP is allowed to send beyond the threshold.

iptables -A INPUT -i eth0 -p udp -m udp --dport 123 -m recent --set --name NTPTRAFFIC --rsource
iptables -A INPUT -i eth0 -p udp -m udp --dport 123 -m recent --update --seconds 30 --hitcount 10 --name NTPTRAFFIC --rsource -j DROP
iptables -A INPUT -i eth0 -p udp -m udp --dport 123 -j ACCEPT # Just to view stats with "iptables -vnL"

The recent IPTables module has some limitations. By default it only tracks the most recent 100 IPs, and the last ten packets from each. If you have a large amount of source IPs hitting you, those limits may not be enough. You can get information about the module and what it can track with:

modinfo xt_recent

Then you can change the amount of packets tracked, and the amount of IPs to track:

echo 250 > /sys/module/xt_recent/parameters/ip_list_tot
echo 60 > /sys/module/xt_recent/parameters/ip_pkt_list_tot

The defaults are pretty good, unless you have a REALLY busy server.

Leave A Reply

PHP: Read from STDIN in CLI mode  

If you need to read data from STDIN in a CLI based PHP script:

$str = file_get_contents("php://stdin");
Leave A Reply

Google is making free metric compatible fonts  

Google has been licensing fonts that are metrically compatible with some common Microsoft fonts.

Arial -> Arimo
Times New Roman -> Tinos
Courier -> Cousine
Cambria -> Caladea
Calibri -> Carlito

Unrelated, but also worth mentioning. Source Code Pro is a fantastic coding font.

Leave A Reply

Vim: Syntax highlight a file without an extension  

I have a file without an extension that I wanted Vim to syntax highlight. Without a file extension Vim is confused about what schema to use to highlight. You can force a specific file type for syntax highlight with a modeline.

// vim: filetype=php
Leave A Reply

Unicode code points  

I needed to get a raw unicode code point in a couple languages.

# Bash

echo -e "\u2591\u2592\u2593"

You have to tell Perl that STDOUT is utf8, otherwise it will complain about "Wide character".

# Perl

binmode(STDOUT, ":utf8"); 
print "\x{2591}\x{2592}\x{2593}\n";

Here is a small PHP function to make utf-8 chars in PHP

# PHP

function utf8($num) {
    if($num<=0x7F)       return chr($num);
    if($num<=0x7FF)      return chr(($num>>6)+192).chr(($num&63)+128);
    if($num<=0xFFFF)     return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
    if($num<=0x1FFFFF)   return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128).chr(($num&63)+128);
    return '';
}

print utf8(0x2591) . utf8(0x2592) . utf8(0x2593) . "\n";
Leave A Reply

Books of 2014  

Also see the list of 2013. The date indicated denotes the date I started reading the book.

2014-01-13 - The girl who loved Tom Gordon - 224 pages
2014-01-22 - Doctor Sleep - 544 pages
2014-02-19 - The Cider House Rules - 576 pages
2014-03-17 - Fight Club - 208 pages
2014-03-24 - Cosmos - 344 pages
2014-04-13 - A Wrinkle in Time - 203 pages
Leave A Reply

Fedora desktop RPM list  

Quick list of RPMs I install on my desktop after a fresh Fedora install.

yum install gkrellm dstat mc nmap pv firefox thunderbird pidgin openvpn qalculate-kde perl-core vim-enhanced screen

Server:

yum install dstat mc nmap pv perl-core vim-enhanced screen
Leave A Reply

Asimov's three laws of robotics  

Isaac Asimov's Three Laws of Robotics

  1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.
  2. A robot must obey the orders given to it by human beings, except where such orders would conflict with the First Law.
  3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Law.
Leave A Reply

Linux: Using automount to mount NFS on the fly  

My home network has an NFS server that I like to mount on my laptop. When I take my laptop to work, the boot hangs trying to find the now non-existent NFS server. The solution that I found was to use automount to mount that directory on demand. When I'm at work, I don't try an access that folder.

If you want to mount your NFS server at /mnt/nfs_server/ Put the following in your /etc/auto.master:

/mnt/nfs_server /etc/auto.master.d/server.autofs

Then create /etc/auto.master.d/server.autofs that has your actual NFS information in it:

* -rw server.domain.com:/home/share/
Leave A Reply