Confirm that a certificate matches a private key with OpenSSL

To prep for a maintenance to thwart HeartBleed I wanted to confirm that a new certificate matched a new key that I received from my developers
You can do this easily with two openssl commands:


$ openssl x509 -noout -modulus -in server.crt | openssl md5
c73310ec894a2518a153144e7abb3c19

Now do the same for the private key

$ openssl rsa -noout -modulus -in server.key | openssl md5
c73310ec894a2518a153144e7abb3c19

If these two hashes match, then the certificate and key are a match

Mac OSX one-liners to get specific IP Info

I needed to allow users to print very specific networking information with ease.
A typical user doesn’t want to decipher all of the extra info and steps an ifconfig or similar command can bring.
While building this script, I ended up with quite a few one-liners that could be useful for every day admin tasks

Print ONLY IP Address
ipconfig getifaddr en0

print ONLY Gateway
route -n get default | awk '/gateway: / {print $2;} '

print ONLY Network Mask for Wi-Fi connection
networksetup -getinfo "Wi-Fi" | awk '/Subnet mask:/ {print $3;} '

print ONLY Network Address
ip=$(ipconfig getifaddr en0) && nm=$(networksetup -getinfo "Wi-Fi" | awk '/Subnet mask:/ {print $3;} ') && IFS=. read -r i1 i2 i3 i4 <<< "$ip" && IFS=. read -r m1 m2 m3 m4 <<< "$nm" && printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$(($i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"

Print all DNS servers on one line
cat /etc/resolv.conf | awk '/nameserver / {print $2;} ' | tr "\n" " "

Delete .DS_Store files

OSX Finder automatically creates DS_Store files when you browse to a directory. The DS_Store file contains information about custom attributes in a folder like the position of icons or the choice of a background image. Uploading it with other files opens the risk of outside users to obtain information about your computer or just create unneeded clutter

Often times when I want to FTP upload a directory it will have a .DS_Store file at the root of each folder.
I of course don’t want this uploaded with my FTP transfer.
Execute this command in the folder you want to delete all .DS_Store files from

sudo find . -name ".DS_Store" -depth -exec rm {} \;

PHP file_get_contents fails silently on files greater than 2Gb

Trying to run a script with file_get_contents on a file > 2Gb kept on failing silently
To fix this, I had to recompile my PHP with “D_FILE_OFFSET_BITS=64”
eg:
CFLAGS="-D_FILE_OFFSET_BITS=64" ./configure

http://www.php.net/manual/en/intro.filesystem.php

No external libraries are needed to build this extension, but if you want PHP to support LFS (large files) on Linux, then you need to have a recent glibc and you need compile PHP with the following compiler flags: -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64.

Bash script to find empty folders recursively

I was recently tasked with determining if a folder did not have any files in it other than more empty folders.

The one exception that was made was hidden files, we can forget about these. In this case these were all .DS_Store and “.rsrc” folders

This script will output whether a folder is empty if all other folders inside of it are also empty

 

#!/bin/bash

if [ ! $1 ]
then
    mydir=`/bin/pwd`
else
    mydir=$1
fi

echo "Searching folder: " $mydir

echo "Empty folders found:"
findempty() {
    find "${1:-.}" -mindepth 1 -maxdepth 1 -type d -not -path '*/\.*' | while read -r dir
    do
    if [[ -z "$(find "$dir" -mindepth 1 -type f -not -path '*/\.*')" ]] >/dev/null
        then
            findempty "$dir"
            dirlevel="${dir//[^\/]/}";
            if [[ ${#dirlevel} == 1 ]]
            then
                echo "empty: $mydir${dir:1}"
            fi
    else
        if [[ ${#dirlevel} == 1 ]]
        then
                echo "not empty: $mydir${dir:1}"
        fi
    fi
    done
}

findempty

Hello world!

Hello world! Welcome to my first post.
If you aren’t familiar with me, my name is Joe and I’m a seasoned systems engineer with specialties in a variety of areas. These include Linux, Windows, MySQL, Apache, PHP (and programming), MS Products, and so forth. If you really want a full scope of my expertise, check out my Resume.
This blog isn’t about me but rather the things I learn (tips+tricks), find, or just think are really cool in the SysAdmin world. It seems that I come across something new and exciting nearly every day, so this is where I’m going to begin posting that.
I’m just about to start a new job which includes a 30 minute commute (I’m not driving) where I expect to have time to update and discuss.
I hope you enjoy and learn something new along with me.
-Joe