Skip to content

Instantly share code, notes, and snippets.

View ThinGuy's full-sized avatar

craig bender ThinGuy

View GitHub Profile
@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')
@ThinGuy
ThinGuy / sudo-show.sh
Last active October 2, 2022 18:42
Illustrate sudo preserving the users's environment ($HOME is this case)
# Show value of $HOME when user does not preserve the environment
sudo bash -c 'echo $HOME'
# Show the value of $HOME when -E to preserve the sudo user's environment
sudo -E bash -c 'echo $HOME'
# Tip: If both are the same:
#
# - You ran this command directly as root (naughty!)
@ThinGuy
ThinGuy / sudo-env.sh
Last active October 20, 2022 23:39
Create bash arrays for the checked, removed, and preserved sudo environmental variables. Add to ~/.bash_rc
declare -ag SUDO_CHCK=($(sudo sudo -V|awk '/(safety|sanity):[\r]?$/,/remove:[\r]?$/{if (/^E|^Lo/) next;gsub(/\t|\r/,"");print}'|sort -uV))
declare -ag SUDO_REMV=($(sudo sudo -V|awk '/remove:[\r]?$/,/preserve:[\r]?$/{if (/^E|^Lo/) next;gsub(/\t|\r/,"");print}'|sort -uV))
declare -ag SUDO_KEEP=($(sudo sudo -V|awk '/preserve:[\r]?$/,/^Loc/{if (/^E|^Lo/) next;gsub(/\t|\r/,"");print}'|sort -uV))
@ThinGuy
ThinGuy / add-repo.sh
Last active October 20, 2022 23:41
Bash function to add a repo with new "signed-by" line in apt sources
add-repo() {
add-repo_usage() {
# Help Blue, Help Blue Bold, Help Blue Italic, Italic Text and Reset Text
local HB='\e[38;2;72;226;225m' HBB='\e[1;38;2;72;226;225m' HBI='\e[3;38;2;72;226;225m' IT='\e[3m' RT='\e[0m'
printf "\n\e[2G${HBB}${FUNCNAME%_*}${RT}: ${IT}Add a repo using the new \x22signed-by\x22 feature${RT}\n\n"
printf "\e[2G${HBB}Usage${RT}:\n\n"
printf "\e[5G${FUNCNAME%_*} [ options ]\n\n"
printf "\e[2G${HBB}Options${RT}:\n\n"
printf "\e[5G-n,--repo-name\e[22GRepo Name\n\n"
printf "\e[5G-u,--repo-url\e[22GRepo URL (in sources.list format)\n\n"
@ThinGuy
ThinGuy / ci-rerun.sh
Created September 25, 2022 20:56
Re-run cloud-init without reboot (run as root if UID 1000 did not get setup on Ubutnu)
#!/bin/bash
cloud-init clean --logs
cloud-init init --local
cloud-init init
cloud-init modules --mode=config
cloud-init modules --mode=final
@ThinGuy
ThinGuy / amt-check.sh
Created June 30, 2022 01:29
Check for AMT on NUC or other AMT-enabled PC
amt-check() {
local G=https://github.com/mjg59/mei-amt-check.git
if [[ ! -f /srv/$(basename ${G%.*})/mei-amt-check ]];then
sudo chown -R $(id -un 1000):$(id -gn 1000) /srv
git clone ${G} /srv/$(basename ${G%.*})
cd /srv/$(basename ${G%.*})
make
fi
[[ -x /srv/$(basename ${G%.*})/mei-amt-check ]] && { sudo /srv/$(basename ${G%.*})/mei-amt-check; }
};export -f amt-check
@ThinGuy
ThinGuy / lxd-maas-microcloud-2022.sh
Created June 29, 2022 19:26
Updated lxd-maas microcloud
#!/usr/bin/env bash
# vim: set et ts=2 sw=2 filetype=bash :
[[ $_ != $0 ]] || { printf "\n\e[2G\e[1mPlease source this script!\e[0m ( e.g. \"source ${BASH_SOURCE[0]}\" )\n\n"; sleep 1;exit; }
maas-microcloud-lxd() {
[[ -n ${FDEBUG} && ${FDEBUG} = true ]] && { set -x &>/dev/null; }
local DESC="${FUNCNAME}: Deploy a LXD Cluster via MAAS command line"
local CI_TZ='America/Los_Angeles'
local CI_LC='en_US.UTF-8'
local CI_TC=${CI_LC:3:2}
@ThinGuy
ThinGuy / get-lxd-migrate.sh
Created June 29, 2022 00:03
Download latest lxd-migrate
sudo wget -qO /usr/local/lxd-migrate --show-progress --progress=bar:force:noscroll $(curl -sSlL https://github.com/lxc/lxd/releases/latest|awk -F'"' '/href.*lxd-migrate/{print "https://github.com"$2}')
sudo chmod +x /usr/local/bin/lxd-migrate
@ThinGuy
ThinGuy / zfs-snapshots
Created June 24, 2022 22:56
Disable / Enable ZFS auto-snapshots
zfs-disable-snapshots() {
systemctl --user stop zsys-user-savestate.timer
systemctl --user disable zsys-user-savestate.timer
sudo mv /etc/apt/apt.conf.d/90_zsys_system_autosnapshot /etc/apt/apt.conf.d/90_zsys_system_autosnapshot.disabled
};export -f zfs-disable-snapshots
zfs-enable-snapshots() {
systemctl --user start zsys-user-savestate.timer
systemctl --user enable zsys-user-savestate.timer
@ThinGuy
ThinGuy / install-kasa.sh
Created June 24, 2022 22:49
Install python-kasa on Ubuntu
#!/bin/bash
declare -ag KASA_PKG_LIST=();
for i in python3.10-venv build-essential python3 python3-pip python3-poetry python3-cachecontrol python3-argcomplete python3-all-dev python3-wheel python3-setuptools python-all-dev python-setuptools python3.10-venv python3-virtualenv;do
[[ $(dpkg -l $i|awk '/'${i}'/{print $1}') = ii ]] && { true; } || { PKG_LIST+=("${i}"); };
done
[[ ${#KASA_PKG_LIST[@]} -ge 1 ]] && { sudo apt install ${KASA_PKG_LIST[@]} -yqf --auto-remove --purge -o Acquire::ForceIPv4=true; } || { true;printf "\n\nAll package dependencies already installed\x21\n\n"; }
python3 -m pip install --user --upgrade pip
python3 -m pip install --user virtualenv
python3 -m pip install --user python-kasa --pre