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 / electricity_bill_modeling
Created December 10, 2023 06:47
Modelling the potential bill for switching from Electric Kiwi to Octopus Energy. Requires data dump from Electric Kiwi
#!/bin/bash
# 7am-9am,5pm-9pm
ek_peak_current="0.2213"
# 9am-5pm, 9pm-11pm
ek_off_peak_shoulder_current="0.1372"
# 11pm to 7am
ek_off_peak_night_current="0.1107"
ek_daily_current="1.99"
@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 / ssh_autocompletion
Created February 7, 2023 20:13
Standard scp autocompletion was breaking on my ssh config structure. This fixes that by focusing on known_hosts only. If it's in the configs, then it should be in known_hosts, right?
__complete_ssh_host() {
local ssh_known_file ssh_known_list partial_word
ssh_known_file="${HOME}/.ssh/known_hosts"
[[ -r "${ssh_known_file}" ]] || return 1
ssh_known_list=$(awk '{print $1}' "${HOME}"/.ssh/known_hosts | tr ',' '\n' | sort | uniq)
partial_word="${COMP_WORDS[COMP_CWORD]}";
mapfile -t COMPREPLY < <(compgen -W "${ssh_known_list}" -- "${partial_word}")
return 0
}
@rawiriblundell
rawiriblundell / get_shell
Last active January 25, 2023 20:58
A function to detect the parent shell
# Because $SHELL is an unreliable thing to test against, we provide this function
# This won't work for 'fish', which needs 'ps -p %self' or similar
# non-bourne-esque syntax.
# TO-DO: Investigate application of 'export PS_PERSONALITY="posix"'
get_shell() {
if [ -r "/proc/$$/cmdline" ]; then
# We use 'tr' because 'cmdline' files have NUL terminated lines
# TO-DO: Possibly handle multi-word output e.g. 'busybox ash'
printf -- '%s\n' "$(tr '\0' ' ' </proc/"$$"/cmdline)"
elif ps -p "$$" >/dev/null 2>&1; then
@rawiriblundell
rawiriblundell / check_user_lastseen
Created September 13, 2020 00:13
checkmk local check for local account auditing
#!/bin/bash
# check_user_lastseen - report on users who have not logged in for a while
# Purpose:
# This script tries to find idle accounts and any orphaned homedirs
# Currently is Linux biased but capacity for portability is there
# Author: Rawiri Blundell
# Copyright: See provided LICENCE file
###############################################################################
# Source the config mapping library
# Provides variables "${thisHost}", "${thisJob}" and
@rawiriblundell
rawiriblundell / regen_knownhosts
Last active January 9, 2023 05:14
Regenerate your known_hosts file after rotating your keys
#!/bin/bash
# Regenerate your known_hosts file after rotating your keys
# Generate a list of hosts that we're more likely to care about, divined from our shell history
# Adjust filtering in the final 'grep -Ev' to suit your needs
# n.b. typos and stale entries will fail in subsequent steps, so don't sweat this too much
get_historical_hosts() {
grep "^ssh " "${HOME}/.bash_history" |
tr " " "\n" |
grep -Ev '^ssh$|^-v+|^-[a-zA-Z]$|^.*.pem$|^".*.pem"$|^raw$|^$|^~|^.$' |
@rawiriblundell
rawiriblundell / fix_jenkins_perms
Created December 21, 2022 10:33
One-off script to recursively capture ownership/perms on a Jenkins directory and to apply them to correct ownership/perms on a target
#!/bin/bash
while read -r file; do
printf -- '%s;%s\n' "$(md5sum < "${file}" | awk '{print $1}')" "$(stat -c "%a;%U;%G;%n" "${file}")"
done < <(find . -type f -print) > data.manifest
51e7d30fab63ba29f86bcf6f807b88cf;674;jenkins;jenkins;./org.jenkinsci.plugins.gitclient.JGitApacheTool.xml
0a363d9165e56856e0f693152fc0fe88;644;jenkins;jenkins;./queue.xml.bak
d99c0a9f0563b0203c6ce7d778a14338;674;jenkins;jenkins;./hudson.plugins.disk_usage.DiskUsageProperty.xml
671c3c9ef67c47d75f73d4d9280e2b28;674;jenkins;jenkins;./org.jenkinsci.plugins.workflow.libs.GlobalLibraries.xml
#!/bin/bash
table_max_width="${2:-80}"
# TO-DO: Add some smarts at this point to minimise issues caused by uneven column widths
# Subtract for our pipechars
# ---------|-----|------|-----|------|-----|------|
# ^ ^ ^ ^ These particular guys
columns_max_width="$(( table_max_width - 4 ))"
@rawiriblundell
rawiriblundell / apt_history
Created April 4, 2022 01:57
Overlay 'apt' with a 'history' arg, as a POC for equivalence to 'yum history'
apt() {
case "${1}" in
(history)
mapfile -t apt_hist < <(zgrep -A 1 "^Start-Date" /var/log/apt/history.log* | grep -v -- "^--$")
left_col_width="$(( "${COLUMNS:-$(tput cols)}" - 30 ))"
# The format of this printf invocation needs to be tweaked
printf -- "| %-${left_col_width}s | %s\\n" "Command line" "Date and time"
printf -- '%*s\n' "${COLUMNS:-$(tput cols)}" | tr ' ' "-"
for (( i=0; i<"${#apt_hist[@]}"; i++ )); do
@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.