Skip to content

Instantly share code, notes, and snippets.

View rawiriblundell's full-sized avatar

Rawiri Blundell rawiriblundell

  • Wellington, New Zealand
View GitHub Profile
@rawiriblundell
rawiriblundell / dnslist
Created May 24, 2021 21:02
A function that attempts to list the dns servers that are used by a host
dnslist() {
case $(uname) in
(Darwin)
printf -- '%s\n' "Attempting lookup test using 'scutil' command..." >&2
scutil --dns |
awk '/nameserver/{ a[$3]++} END { for (b in a) {print b } }'
return 0
;;
([Ll]inux)
# TODO: Update to test against IP addresses rather than 'Global'
@rawiriblundell
rawiriblundell / get_sourceforge
Created April 29, 2021 10:34
Download the latest release file from a sourceforge project
# Usage: get_sourceforge [project] [linux|mac|windows]
get_sourceforge() {
# We require 'curl' and 'jq'
fail_count=0
for binary in curl jq; do
if ! command -v "${binary}" >/dev/null 2>&1; then
printf -- '%s\n' "${binary} is required but was not found in PATH" >&2
(( fail_count++ ))
fi
done
# This is based on one of the best urandom+bash random integer scripts IMHO
# FYI: randInt is significantly faster
# https://unix.stackexchange.com/a/413890
urandInt() {
local intCount rangeMin rangeMax range bytes t maxvalue mult hexrandom
intCount="${1:-1}"
rangeMin="${2:-1}"
rangeMax="${3:-32767}"
range=$(( rangeMax - rangeMin + 1 ))
# A shell conversion of
# https://gist.github.com/caseydunham/508e2994e1195e4cb8e4
convert_ldaptime_to_unixepoch() {
local ldap_offset ldap_timestamp
ldap_timestamp="${1:?No ldap timestamp supplied}"
ldap_timestamp=$(( ldap_timestamp / 10000000 ))
# Calculated as '( (1970-1601) * 365 -3 + ((1970-1601)/4) ) * 86400'
ldap_offset=11644473600
printf -- '%s\n' "$(( ldap_timestamp - ldap_offset ))"
}
@rawiriblundell
rawiriblundell / histrank
Created April 18, 2021 10:57
Sort history by most used commands, can optionally print n lines (e.g. histrank [n])
# Sort history by most used commands, can optionally print n lines (e.g. histrank [n])
histrank() {
HISTTIMEFORMAT="%y/%m/%d %T " history \
| awk '{out=$4; for(i=5;i<=NF;i++){out=out" "$i}; print out}' \
| sort \
| uniq -c \
| sort -nk1 \
| tail -n "${1:-$(tput lines)}"
}
@rawiriblundell
rawiriblundell / throttle
Created April 18, 2021 10:52
A function for throttling stdout
# Throttle stdout
throttle() {
# Check that stdin isn't empty
if [[ -t 0 ]]; then
printf -- '%s\n' "Usage: pipe | to | throttle [n]" ""
printf -- '\t%s\n' "Increment line by line through the output of other commands" "" \
"Delay between each increment can be defined. Default is 1 second."
return 0
fi
#!/bin/bash
delete-branch() {
local unwanted_branches current_branch mode
current_branch="$(git symbolic-ref -q HEAD)"
current_branch="${current_branch##refs/heads/}"
current_branch="${current_branch:-HEAD}"
case "${1}" in
(--local) shift 1; mode=local ;;
@rawiriblundell
rawiriblundell / pseudowords
Created March 23, 2021 21:12
A dictionary of pseudowords, potential for mixing into passphrases
abable
abacreling
abactufficken
abaling
abange
abansery
abild
abilecially
abinallimocreserve
abincength
@rawiriblundell
rawiriblundell / check_linux_patching
Last active February 28, 2022 14:31
CheckMK local check for capturing available updates
#!/bin/bash
# check_linux_lastpatched - Check when a Linux host was last patched and warn
# if a specified time period is exceeded
# Purpose: checkmk local check to ensure we keep Linux hosts patched
# Author: Rawiri Blundell
# Copyright: See provided LICENCE file
# Date: 20211102 partial rewrite, originally written 20170828
# Usage: ./check_linux_lastpatched [warn threshold (days)] [crit threshold (days)]
# n.b. warn threshold defaults to 9 months, crit threshold to 12.
@rawiriblundell
rawiriblundell / carve
Last active March 3, 2021 21:35
Native bash method for pulling a substring out from between two markers
# TODO: Make this more robust, build in validation etc
# This might wind up being a script in its own right rather than a function
carve() {
read -r count1 delim1 _ count2 delim2 <<< "${@}"
case "${count1}" in
([0-9]*) count1="${count1//[!0-9]/}" ;;
(first) count1="1" ;;
(last) count1="last" ;;
(''|*)
printf -- '%s\n' \