Skip to content

Instantly share code, notes, and snippets.

View hongkongkiwi's full-sized avatar
🤓

Andy hongkongkiwi

🤓
View GitHub Profile
@hongkongkiwi
hongkongkiwi / update-mergerfs
Created March 7, 2020 06:26
Updates mergerfs to the latest version
#!/usr/bin/env bash
# Read a single char from /dev/tty, prompting with "$*"
# Note: pressing enter will return a null string. Perhaps a version terminated with X and then remove it in caller?
# See https://unix.stackexchange.com/a/367880/143394 for dealing with multi-byte, etc.
function get_keypress {
local REPLY IFS=
>/dev/tty printf '%s' "$*"
[[ $ZSH_VERSION ]] && read -rk1 # Use -u0 to read from STDIN
# See https://unix.stackexchange.com/q/383197/143394 regarding '\n' -> ''
@hongkongkiwi
hongkongkiwi / generate-ssh-key
Last active December 18, 2023 07:45
Simple one liner to generate an SSH key without a password in the default place with the comment as hostname then print out the public key for copy and paste.
HOSTNAME=`hostname` ssh-keygen -t rsa -C "$HOSTNAME" -f "$HOME/.ssh/id_rsa" -P "" && cat ~/.ssh/id_rsa.pub
@hongkongkiwi
hongkongkiwi / generate-dropbear-key
Last active December 17, 2023 16:38
Generate SSH Key in Dropbear with some options (by default it uses default id_dropbear as the name and ed25519 as the type)
#!/bin/sh +ux
# We set the sh +ux flags so that we error on undefined variables and error on bad commands
help() {
echo >&2 "$0 [-f] [-p] [-q] [<priv_key_file>] [<key_type>] [<key_comment>]"
echo >&2
echo >&2 "-q / --quiet to silent all output (except -p if passed)"
echo >&2 "-p / --pubkey to output public key after generation"
echo >&2 "-f / --force to force replacing existing key"
echo >&2
@hongkongkiwi
hongkongkiwi / ubuntu_fastest_package_mirror
Created June 20, 2022 04:49
One liner to find the fastest ubuntu mirror
curl -s http://mirrors.ubuntu.com/mirrors.txt | xargs -n1 -I {} sh -c 'echo `curl -r 0-102400 -s -w %{speed_download} -o /dev/null {}/ls-lR.gz` {}' | sort -g -r | head -1 | awk '{ print $2 }'
@hongkongkiwi
hongkongkiwi / main.tf
Last active November 16, 2023 23:38
Initialises Terraform HTTP backend to Gitlab. Here's a nice script which will ask for details in an interactive way.
# More information can be found at https://docs.gitlab.com/ee/user/infrastructure/#gitlab-managed-terraform-state
terraform {
backend "http" {
}
}
@hongkongkiwi
hongkongkiwi / validate_authorized_keys
Last active October 3, 2023 04:25
Shell script to validate all authorized_keys in one or more files or all files in a directory
#!/bin/sh -ue
REMOVE_COMMENTS_SED='/^[[:blank:]]*(#|$)/d; s/#.*//'
SSH_KEYGEN_BIN="ssh-keygen"
if ! command -v "$SSH_KEYGEN_BIN" >/dev/null 2>&1; then
echo >&2 "I require $SSH_KEYGEN_BIN but it's not installed. Aborting."; exit 255
fi
# Assume all keys are valid until told otherwise
ALL_KEYS_VALID="yes"
@hongkongkiwi
hongkongkiwi / setup_unraid_ssh.sh
Created November 23, 2017 03:57
Small script to setup ssh keys and configs on Unraid to persist after boot. Supports multiple users.
#!/bin/bash
# Add this line into /boot/config/go where username is the user you want to setup, probably root
#/boot/config/ssh/setup_ssh_client.sh "username"
if [[ "$1" == "" ]]; then
echo "Invalid User!"
exit 1
fi
@hongkongkiwi
hongkongkiwi / count.sh
Last active May 24, 2023 06:43
Simple shell script to count all files (or directories) in current directory, optionally can pass in a pattern for find
#!/bin/sh -u
# Count all files
# ./count
# Count only mp3 files
# ./count "*.mp3"
# Count only directories
# ./count "" "d"
# Count only directories ending with blah
# ./count "*blah" "d"
@hongkongkiwi
hongkongkiwi / generate-ssl-certs.sh
Last active April 28, 2023 03:41
Script to generate a Root CA, Intermediate CA and then to sign the Intermediate with the Root.
#!/bin/bash
USER=`id -u -n`
GROUP=`id -g -n`
GENERATE_ROOT_CA_FILE="YES"
GENERATE_CA_DER_FILE="YES"
GENERATE_IM_CA_FILE="YES"
GENERATE_IM_DER_FILE="NO"
GENERATE_DH_FILE="NO"
@hongkongkiwi
hongkongkiwi / firewall-start
Last active April 22, 2023 13:52
Bash script to allow some ports on Asus routers running Merlin firmware. This should be put in your /jffs/scripts and made executable (chmod +x /jffs/scripts/firewall-start). Now handles duplicate rules (it won't add again) and inserting before any final DROP in the INPUT chain. Quite useful ;)
#!/bin/sh
DEBUG="NO"
LOGGER_NAME="firewall"
PORTS="tcp:9443"
WAN="$1"
log() {
if [ "$DEBUG" == "YES" ]; then
echo "$1"