Skip to content

Instantly share code, notes, and snippets.

@mtik00
mtik00 / sslcheck.sh
Created October 2, 2023 22:48
SSL/TLS cert check using openssl
#!/usr/bin/env bash
set -euo pipefail
SERVERNAME=${1:-""}
PORT=${2:-443}
if [[ -z "${SERVERNAME}" ]]; then
printf "Usage:\n"
printf " sslcheck <servername>\n"
printf " sslcheck <servername> <port>\n"
@mtik00
mtik00 / progressbar.py
Last active July 12, 2023 17:35
Python progress bar
class ProgressBar:
"""
A progress bar that's heavily inspired by click.ProgressBar.
Required imports:
import shutil
import sys
import time
import unicodedata
@mtik00
mtik00 / request.py
Last active June 26, 2023 19:16
stdlib URL request function
import json
import urllib.parse
import urllib.request
def request(
url: str,
method: str | None = None,
data: dict | None = None,
query_params: dict | None = None,
raise_on_error: bool = True,
@mtik00
mtik00 / ipsum.py
Created June 13, 2023 17:55
Create "ipsum" test using Star Wars scripts from forceipsum.com
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""A script to print "ipsum" text from the Star Wars universe through Force Ipsum.
https://forcemipsum.com
"""
import argparse
import json
import sys
import urllib.request
from secrets import SystemRandom
@mtik00
mtik00 / logger.py
Last active June 13, 2023 18:19
Python logger with colors and sensitive string masking
#!/usr/bin/env python3
import logging
import os
from pathlib import Path
SENSITIVE: list[str] = []
TIMEZONE: str = "America/Denver"
# https://no-color.org/
NO_COLOR: bool = os.getenv("NO_COLOR", "") != ""
@mtik00
mtik00 / git-setup.sh
Last active July 13, 2023 15:21
Quick and dirty setup for git and bash aliases.
#!/usr/bin/env bash
# Quick and dirty setup for git and bash aliases.
[ ! -f ~/.git-aliases ] && cat <<-END > ~/.git-aliases
alias g='git'
alias gd='git diff'
alias gf='git fetch --all --prune'
alias gfpr='git fetch --all --prune && git pull --rebase'
alias gg='git log --graph --pretty=format:'\''%C(bold)%h%Creset%C(magenta)%d%Creset %s %C(yellow)<%an> %C(cyan)(%cr)%Creset'\'' --abbrev-commit --date=relative'
alias gh='cd "$(git rev-parse --show-toplevel)"'
@mtik00
mtik00 / tasks.json
Last active January 29, 2023 01:54
A simple vscode task to run the current Javascript file in a node:alpine docker container
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "run file",
"type": "shell",
"command": "docker run --rm -it -v ${workspaceFolder}:/usr/src/app node:alpine /usr/src/app/${relativeFile}"
}
@mtik00
mtik00 / array_join.sh
Last active December 27, 2022 20:12
Joining an array in Bash (like " ".join() in Python)
function array_join() {
# Join an array with a a separator character.
# WARNING: This only works in bash. You should also pass the array by
# reference. Example:
# my_array=( "one" "two" "three")
# joined=$( array_join my_array "|")
local -n arr=$1
local sep=${2:-" "}
printf -v result "%s${sep}" "${arr[@]}"
@mtik00
mtik00 / stdin-check.py
Last active June 13, 2023 18:21
read password from stdin
# You can check to see if someone ran your script with `echo -n test | myscript.py`.
# In that case, there's no tty attached. If there _is_ an attached tty, then you
# can just prompt the user like normal.
import sys
def read_secret_key():
if sys.stdin.isatty():
return getpass("Enter the secret key: ")
else:
return sys.stdin.readline()
def format_timespan(seconds: float) -> str:
"""Formats the number of seconds in h:m:s"""
hours, seconds = divmod(seconds, 3600)
minutes, seconds = divmod(seconds, 60)
return f"{hours:d}h:{minutes:02d}m:{seconds}s"