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 date and time (YYYYMMDD-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=`ip addr | grep 'state UP' -A2 | tail -n1 | awk -F'[/ ]+' '{print $3}'`

Get machine's wan IP

wanip=`wget -O - -q "http://icanhazip.com"`