Skip to content

Useful Bash Snippets

These are a collection of bash snippets I’ve written over the years.

Get machine’s gateway

gateway=`netstat -rn | grep 0.0.0.0 | awk '{print $2}' | grep -v "0.0.0.0"`

Get script’s directory

setupDir="$(dirname $(readlink -f $0))"

Get the current YYYY-HHMMSS

now=$(date +"%Y%m%d-%H%M%S")

Get the active network card

netCard=`route | grep '^default' | grep -o '[^ ]*$'`

Get machine’s lan IP

lanip=`ifconfig | grep -A 1 eth0 | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1`

Get machine’s wan IP

ip=`wget -O - -q "http://icanhazip.com"`
© 2004 - 2017 • All rights reserved.