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 / 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 / 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 / mk_log4j_denylist
Created December 15, 2021 19:51
Generate a blocklist for log4shell scanners for use in nginx
#/bin/bash
# Generates deny list for nginx
# This blocks known bad IP's that are scanning for log4shell exploits
if ! command -v curl >/dev/null 2>&1; then
printf -- '%s\n' "This script requires 'curl'" >&2
exit 1
fi
# Remote source to pull down
@rawiriblundell
rawiriblundell / readlink_f
Created December 9, 2021 00:24
An attempt to make a portable version of 'readlink -f'
@rawiriblundell
rawiriblundell / tzlookup
Created September 17, 2021 00:28
Lookup a timezone based on lat/long coords
tzlookup() {
local err cmd lat long
err=0
for cmd in curl jq; do
command -v "${cmd}" >/dev/null 2>&1 || (( ++err ))
done
(( err > 0 )) && { printf -- '%s\n' "Requires 'curl' and 'jq'" >&2; return 1; }
lat="${1:?No latitude value supplied}"
long="${2:?No longitude value supplied}"
# A portable alternative to exists/get_command/which/type
pathfind() {
OLDIFS="${IFS}"
IFS=:
for prog in ${PATH}; do
if [[ -x "${prog}/$*" ]]; then
printf -- '%s\n' "${prog}/$*"
IFS="${OLDIFS}"
return 0
fi
@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$|^$|^~|^.$' |