Get WAN IP address via shell

Sometimes one needs to get the WAN IP address via shell.

#!/bin/sh

set -e
set -u

C_DIG="$(which dig)"

DEBUG=0

while [[ ${#} -gt 0 ]]; do
    KEY="${1}"

    case "${KEY}" in
        -d|--debug)
            DEBUG=1
            shift
        ;;
    esac
done

QUERY_TYPE="A"
DIG_OPTIONS="+short"

DNS_SERVER="resolver1.opendns.com"
QUERY_VALUE="myip.opendns.com"

# short version: dig @resolver1.opendns.com A myip.opendns.com +short
IP_ADDRESS="$(${C_DIG} @${DNS_SERVER} ${QUERY_TYPE} ${QUERY_VALUE} ${DIG_OPTIONS})"
RC=${?}

# if the IP address could be successfully retrieved by querying OpenDNS echo it and exit
if [ ${RC} -eq 0 ]; then
    if [ ${DEBUG} -eq 1 ]; then
        echo "Server queried: OpenDNS"
    fi

    echo "${IP_ADDRESS}"

    exit 0
fi

DNS_SERVER="ns1-1.akamaitech.net"
QUERY_VALUE="whoami.akamai.net"

# short version: dig @ns1-1.akamaitech.net A whoami.akamai.net +short
IP_ADDRESS="$(${C_DIG} @${DNS_SERVER} ${QUERY_TYPE} ${QUERY_VALUE} ${DIG_OPTIONS})"
RC=${?}

# if the IP address could be successfully retrieved by querying Akamai echo it and exit
if [ ${RC} -eq 0 ]; then
    if [[ "${DEBUG}" == "-d" ]]; then
        echo "Server queried: Akamai"
    fi

    echo "${IP_ADDRESS}"

    exit 0
fi

DNS_SERVER="ns1.google.com"
QUERY_VALUE="o-o.myaddr.l.google.com"
QUERY_TYPE="TXT"

# short version: dig @ns1.google.com TXT o-o.myaddr.l.google.com +short
IP_ADDRESS="$(${C_DIG} @${DNS_SERVER} ${QUERY_TYPE} ${QUERY_VALUE} ${DIG_OPTIONS})"
RC=${?}

# if the IP address could be successfully retrieved by querying Google echo it and exit
if [ ${RC} -eq 0 ]; then
    if [[ "${DEBUG}" == "-d" ]]; then
        echo "Server queried: Google"
    fi

    echo "${IP_ADDRESS}"

    exit 0
fi

exit 1

Source

This has been taken from https://unix.stackexchange.com/a/81699 and put into a small script to provide some kind of failover if one service might not be available.

MySQL password hash creator

#!/bin/sh

C_AWK="$(which awk)"
C_SHA1SUM="$(which sha1sum)"
C_TR="$(which tr)"
C_XXD="$(which xxd)"

echo -n "MySQL password: "

read PASSWORD

if [ ${#PASSWORD} -eq 0 ]; then
    echo "Invalid password length: 0"

    exit 1
fi

FIRST_HASH="$(echo -n "${PASSWORD}" | ${C_SHA1SUM} | ${C_AWK} '{print $1}')"
FIRST_HASH_BINARY="$(echo -n "${FIRST_HASH}" | ${C_XXD} -r -p)"
SECOND_HASH="$(echo -n "${FIRST_HASH_BINARY}" | ${C_SHA1SUM} | ${C_AWK} '{print $1}')"
SECOND_HASH_UPPER="$(echo -n "${SECOND_HASH}" | ${C_TR} [a-z] [A-Z])"

echo "*${SECOND_HASH_UPPER}"

exit 0

Git change author/committer name/e-mail address

A small script to change the name and/or e-mail address of the author and/or committer in a Git repository (completely).

There actually is a difference between the author and the committer. The author wrote the code initially and the committer committed it to the repository.

 

Linux opened files

Amount of max. open files

per PID (row “Max open files”)

 

Top 20 processes

(found on Ask Ubuntu)

NetworkManager disable MAC randomization

The NetworkManager automatically changes the MAC address of the WiFi adapter every time it searches for / connects to a network.

This does not come in handy if you have a MAC filter active …

To disable the MAC address randomization create the file

/etc/NetworkManager/conf.d/100-disable-wifi-mac-randomization.conf

with the content