Skip to content

Instantly share code, notes, and snippets.

View ThinGuy's full-sized avatar

craig bender ThinGuy

View GitHub Profile
@ThinGuy
ThinGuy / networkd-disable-eee.sh
Last active February 14, 2024 22:25
Disables Energy Efficient Ethernet (EEE) on all NICs that support it. For use with Netplan (via networkd-dispatcher)
#!/bin/bash
# Install ethtool if needed
[[ $(command -v ethtool 2>/dev/null) ]] || { printf "\nInstalling ethtool. One moment...\n\n";sudo apt install ethtool -yqf; }
# Create an empty networkd dispatcher script with correct owner and permissions
sudo install -o0 -g0 -m0755 /dev/null /etc/networkd-dispatcher/routable.d/99-disable-eee.sh
cat <<'EOF' |sudo tee 1>/dev/null /etc/networkd-dispatcher/routable.d/99-disable-eee.sh
#!/bin/bash
@ThinGuy
ThinGuy / maas-debug.sh
Created March 2, 2023 23:27
function to toggle on/off MAAS debug mode
maas-debug() {
[[ -z ${1} || ${1,,} =~ '-h' ]] && { printf "\nUsage: ${FUNCNAME} <on|off> (-h,--help)\n\n" 1>&2;return 2; }
declare -ag DBG_FILES=($(sudo find 2>/dev/null /etc/maas /var/snap/maas/current/ -maxdepth 1 -type f -iname "r*d.conf"))
[[ ${#DBG_FILES[@]} -ge 2 ]] || { printf "\e[1;38;2;255;0;0mError\e[0m: Could not locate the regiond and rackd.conf files.\n\n" 1>&2;return 2; }
[[ ${1,,} = on && ${#DBG_FILES[@]} -ge 2 ]] && { printf "\nSetting debug mode to \e[1m on\e[0m\n" 1>&2;sudo sed -r -i '/^debug:/{h;s/:.*/: true/};${x;/^$/{s//debug: true/;H};x}' ${DBG_FILES[@]}; }
[[ ${1,,} = off && ${#DBG_FILES[@]} -ge 2 ]] && { printf "\nSetting debug mode to \e[1m off\e[0m\n" 1>&2;sudo sed -r -i '/^debug:/{h;s/:.*/: false/};${x;/^$/{s//debug: false/;H};x}' ${DBG_FILES[@]}; }
[[ ${#DBG_FILES[@]} -ge 2 && ${DBG_FILES[0]} =~ /var/snap/maas/current ]] && { printf "\nRunning \x60sudo snap restart maas\x60...\n" 1>&2;sudo snap restart maas 2>&1|sed -r 's/^/ /g'; }
[[ ${#DBG_FILES[@]} -ge 2 && ${DBG_FILES[0]
@ThinGuy
ThinGuy / mtr.sh
Last active February 11, 2024 02:24
Test all nics using mtr (my traceroute)
mtrv4() {
[[ -z ${1} || ${1} =~ -h ]] && { printf "Usage: ${FUNCNAME} <hostname|IP>\n" 1>&2;return 2; }
for i in $(ip -o -4 a|awk '{if ($2 != "lo") print $2}'|paste -sd' ');do mtr -4 -rw -c 5 -I ${i} ${1};done
};export -f mtrv4
mtrv6() {
[[ -z ${1} || ${1} =~ -h ]] && { printf "Usage: ${FUNCNAME} <hostname|IP>\n" 1>&2;return 2; }
for i in $(ip -o -6 a|awk '{if ($2 != "lo") print $2}'|paste -sd' ');do mtr -6 -rw -c 5 -I ${i} ${1};done;
};export -f mtrv6
@ThinGuy
ThinGuy / node-rename.sh
Created December 3, 2022 01:31
Rename machines based on BMC Info
# The following are bash functions. Either source this file (i.e. source ~/.node-rename.sh) or copy and paste the stanza directly in your terminal
# If you're using normal postgresq
maas-rename-new-machines-prod-db() {
sudo -u postgres psql -F"|" --no-align -P pager=off -t maasdb -c "\
SELECT sip.ip,node.hostname,node.system_id FROM maasserver_bmc bmc \
LEFT OUTER JOIN maasserver_staticipaddress sip ON sip.id = bmc.ip_address_id \
LEFT OUTER JOIN maasserver_node node ON bmc.id = node.bmc_id \
WHERE bmc.ip_address_id IS NOT NULL and power_type LIKE 'ipmi%' ORDER BY sip.ip"|awk -F\| '{print $1"|"$3}'|\
xargs -n1 -P0 bash -c 'IP=${0%%|*};ID=${0##*|};NEWNAME=$(printf "faux-node-%02d\n" ${IP##*.});maas '${MAAS_PROFILE}' machine update $ID hostname=$NEWNAME'
@ThinGuy
ThinGuy / maas-fake-nodes.sh
Created December 3, 2022 01:10
Create fake nodes in MAAS
#!/bin/bash
# Side loading example
# The following will create N fake nodes whose
# hostnames will be "fnode-${POWER_DRIVER}${LAST_IP_OCTET}
# And MAC address will be made up on the fly.
# For this example, ipmi and manual power drivers are supported.
@ThinGuy
ThinGuy / oscap-report.sh
Last active November 4, 2022 15:54
Function to run OpenSCAP CVE Evaluation with options to view report in terminal
oscap-report() {
local REL=$(lsb_release -sr) SHOW=loc;
[[ $((${REL%%.*}%2)) && $((10#${REL##*.})) -eq 4 ]] && { true; } || { printf "\e[1;38;2;255;0;0mSorry\x21\e[0m OVAL Data is only available for LTS release\n\n";return 3; }
local OSCAP_REPORT="$HOME/oscap-eval-report__$(lsb_release -sd|awk '{gsub(/ /,"-");print tolower($0)}')-$(hostname -s).html"
OK() { printf "\e[74G\x20\e[0m[\x20\e[38;2;0;255;0mOK\e[0m\x20]\e[0m\n"; }
FAIL() { printf "\e[74G\e[0m\x20[\e[38;2;255;0;0mFAIL\e[0m]\n"; }
WARN() { printf "\e[74G\x20\e[0m[\e[38;2;255;200;0mWARN\e[0m]\n"; }
local -a PREREQS=(bzip2 html2text libopenscap8 w3m)
local -a PKGS=()
local OVAL_URL="https://security-metadata.canonical.com/oval/com.ubuntu.$(lsb_release -cs).usn.oval.xml.bz2"
@ThinGuy
ThinGuy / pg_dumpall_maas-test-db.sh
Created October 31, 2022 23:36
Run pg_dumpall against maas-test-db
#!/bin/bash
sudo -iu root psql -U postgres -h /var/snap/maas-test-db/common/postgres/sockets -d maasdb -c "ALTER ROLE maas WITH SUPERUSER"
sudo -iu root pg_dumpall -U maas -l maasdb -h /var/snap/maas-test-db/common/postgres/sockets -c|tee 1>/dev/null ~/dump.sql
@ThinGuy
ThinGuy / ip-fwd-status
Created October 25, 2022 01:14
BASH function to get ip forwarding status of all nics
ip-fwd-status() { find /sys/class/net -maxdepth 1 -type l ! -iname "*lo*" -printf "%P\n"|sort -uV|xargs -I@ -P1 sudo sysctl [email protected]; };export -f ip-fwd-status
@ThinGuy
ThinGuy / demo-typer.sh
Last active April 18, 2024 12:04
Read a list of commands then make it look like you're typing in terminal, Use with termtosvg. asciinema, etc.
#!/bin/bash
[[ -f ~/script.cmds ]] || { printf "\nMissing $HOME/script.cmds. Exiting\n";exit 1; }
dprompt() { [[ $HOME = $(pwd) ]] && { local DIR='~'; } || { local DIR=$(pwd); };printf "\e[01;32m$USER@${HOSTNAME}\e[0m:\e[01;34m${DIR}\e[0m$ ";};export -f dprompt
clear
declare -ag DCMDS=()
export DCMD_CNT=${#DCMDS[@]}
while IFS= read -er C;do DCMDS+=("${C%;$*}");done < <(cat script.cmds)
export DCMD_CNT=${#DCMDS[@]}
for D in "${DCMDS[@]}";do dprompt;echo "${D}"|pv -qL 10;bash -c "${D}";done
@ThinGuy
ThinGuy / pnid-regex.sh
Created October 3, 2022 19:20
REGEX for Predictable Network Interface Device Names
export PNID_REGEX=$(curl -fksSL https://raw.githubusercontent.com/systemd/systemd/main/src/udev/udev-builtin-net_id.c|grep -oPa '(?<=prefix = \x22)[^\x22]+'|sort -uV|paste -sd'|'|sed -r 's,l\|ww,[lw],g;s,^|$,\x27')