Skip to content

Instantly share code, notes, and snippets.

View hongkongkiwi's full-sized avatar
🤓

Andy hongkongkiwi

🤓
View GitHub Profile
@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 / 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 / get-sys-arch
Last active June 30, 2022 07:28
Script for getting system arch in various formats, you simply pass outputs depending on what kind of format you want. e.g. get-sys-arch --x64 --x32 --armhf --arm64
#!/bin/bash -ue
HELP="$0 [--i686|--i386|--x86_32|--x32] [--amd64|--x86_64|--x64] [--armv7l|--armv7|--arm|--armhf] [--arm64|--aarch64]"
I386=''; I686=''; X86_32=''; X32=''; BITS_32='';
ONLY_686=''; ONLY_386=''; ONLY_32=''; ONLY_64='';
AMD64=''; X86_64=''; X64=''; BIT_64='';
AARCH64=''; ARM64='';
ARM_V6=''; ARMV7L=''; ARMV7=''; ARM=''; ARM_HF='';
while [ $# -gt 0 ]; do
case "${1-}" in
'--32bit'|'-32bit') BIT_64='yes'; ONLY_32=''; I686=''; X32=''; ONLY_386=''; ONLY_686=''; I386=''; X86_32='';;
@hongkongkiwi
hongkongkiwi / valid_docker_tag_name
Created June 22, 2022 04:40
One liner to format a git tag / branch name appropriately for a docker tag
# 1. Replace any non valid characters with dash
# 2. Remove any .- from start of name
# 3. Replace duplicate dash with a single one
# 3. Replace duplicate underscore with a single one
# 4. Replace duplicate dot with a single one
# 5. Truncate string to 128 chars (or less)
sed -e 's/[^[:alnum:]\.\_\-]/-/g' \
-e 's|^[.-]\+||g' \
-e 's|-\+|-|g' \
-e 's|_\+|_|g' \
@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 / pkgversion
Created May 6, 2022 08:16
Grab a version nicely from Gitlab (works in CI or non-CI)
# Use CI_COMMIT_TAG if it exists, otherwise lookup for a pkgversion file
PKGVERSION="$CI_COMMIT_TAG:-"$(head -n1 "$CI_PROJECT_DIR/pkgversion" 2>/dev/null)"}"
if [ -z "$PKGVERSION" ]; then
cd "${CI_PROJECT_DIR:-"."}"
# Check if we can find a tag otherwise specifically fetch tags
git describe --tags >/dev/null 2>&1 || git fetch --tags
# Set our package version correctly
PKGVERSION="$(git describe --tags 2>/dev/null)"
# If our package version couldn't be set this way then use Gitlab CI
if [ -z "$PKGVERSION" -a -n "$CI_COMMIT_SHORT_SHA" -a -n "$CI_PIPELINE_IID" -a -n "$CI_JOB_TOKEN" ]; then
@hongkongkiwi
hongkongkiwi / dec_to_hex
Last active May 6, 2022 04:18
Simple shell scripts to convert decimal to hex in bash (and visa versa) with various formatting options
#!/bin/bash -ue
##
# Simple shell script to convert decimal to hex
# Created by hongkongkiwi: https://gist.github.com/hongkongkiwi/02d8bcde0c810f8dff6b6a96d11ce6bd
# Usage:
# ./dec_hex "12356"
# ./dec_hex -n "12356"
# ./dec_hex -n -l "12356"
# ./dec_hex -n -l -o "12356"
##
@hongkongkiwi
hongkongkiwi / rclone-mount-synology
Created February 7, 2022 11:40
RClone mount script for Synology NAS
#!/bin/bash
ACTION="$1"
# RClone Service Series
RCLONE="/bin/rclone"
CONFIG_FILE="/volume1/@rclone/config/rclone.conf"
MOUNT_NAME="${2:-"union_series"}"
MOUNT_POINT="${3:-"/volume1/media/series"}"
MOUNT_CACHE_DIR="/volume1/@tmp/@${MOUNT_NAME}"
@hongkongkiwi
hongkongkiwi / app-wrapper
Created August 22, 2021 03:53
A simple app wrapper script which uses https://github.com/xiezhenye/waitpid to wait for the pid to and handles termination of the app if the script terminates. Call the script with ./app-wrapper syslogd <args>
#!/bin/sh
BIN="$1"; shift
[ -z "$BIN" ] && { echo >&2 "ERROR: no app passed"; exit 1; }
APP_NAME=$(basename "$BIN")
PID_FILE=${PID_FILE:-"/var/run/${APP_NAME}.pid"}
function killapp() { [ -n "$APP_PID" ] && kill -s TERM $APP_PID 2>/dev/null >/dev/nulll; }
trap killapp INT TERM SIGTERM EXIT
rm -f "$PID_FILE" 2>/dev/null
APP_DIR=$(dirname "$BIN")
CUR_DIR="$PWD"
@hongkongkiwi
hongkongkiwi / dockerfile_download_verify_file
Created October 19, 2020 08:39
Dockerfile snippet to download a file and verify it against the checksum and ensure that checksum is correct. Terraform used as an example here.
# You can set a version as a build argument to overwrite the detection
FROM ubuntu:latest
ARG TERRAFORM_VERSION=
# This example uses wget, so make sure it's installed first, could be adapted to use curl
RUN if [ -z $TERRAFORM_VERSION ]; then echo "Finding latest Terraform Version..."; TERRAFORM_VERSION=$(curl -s "https://github.com/hashicorp/terraform/releases/latest/download" 2>&1 | grep -Po [0-9]+\.[0-9]+\.[0-9]+); else echo "Terraform version passed in build argument v${TERRAFORM_VERSION}"; fi && \
echo "Downloading Terraform v${TERRAFORM_VERSION}..." && \
wget -O "/tmp/terraform_${TERRAFORM_VERSION}_linux_${PLATFORM_ARCH}.zip" -q "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_${PLATFORM_ARCH}.zip" && \
wget -O "/tmp/terraform_${TERRAFORM_VERSION}_SHA256SUMS" -q "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_SHA256SUMS" && \