Skip to content

Instantly share code, notes, and snippets.

View dlenski's full-sized avatar

Dan Lenski dlenski

View GitHub Profile
@dlenski
dlenski / winenv.py
Created June 14, 2025 00:08
Access Windows environment variables from Python under WSL
r'''
Access Windows environment variables from Python under WSL
The :py:data:`winenv` and py:data:`winenv_wslpath` objects are
immutable mappings, lazy-loaded upon initial usage.
py:data:`winenv` contains the unmodified Windows environment variables,
while py:data:`winenv_wslpath` attempts to convert all ``PATH``-like
variables to WSL paths.
@dlenski
dlenski / backport_pathlib_Path_from_uri.py
Created June 10, 2025 19:15
Monkey-patch a backport of `pathlib.Path.from_uri` from Python 3.13+
'''
This is a monkey-patched backport to Python 3.4+ of a useful `pathlib.Path` method added in Python 3.13:
https://docs.python.org/3/library/pathlib.html#pathlib.Path.from_uri
'''
import sys
assert sys.version_info >= (3, 4), "Python 3.4+ is required for `pathlib` support (https://docs.python.org/3/library/pathlib.html)"
if sys.version_info < (3, 13):
from pathlib import PurePath
@dlenski
dlenski / gcloud-start-tunnel-nc
Created February 14, 2025 05:45 — forked from dlenskiSB/gcloud-start-tunnel-nc
SSH ProxyCommand wrapper script for 'gcloud compute start-iap-tunnel'
#!/bin/bash
set -eo pipefail
if [[ $# == 0 ]]; then
cat <<EOF
usage: $(basename $0) [ARGUMENTS FOR 'gcloud compute start-iap-tunnel']
For some reason, 'gcloud compute start-iap-tunnel' lacks a mode where it can
be used as a ProxyCommand for SSH (e.g. proxy between stdin and stdout), so
@dlenski
dlenski / pbcopyhtml
Created February 14, 2025 04:59
Copy from stdin to the clipboard as HTML on Mac
#!/bin/sh
printf "set the clipboard to «data HTML$(cat $@ | hexdump -ve '1/1 "%.2x"')»" | osascript -
@dlenski
dlenski / jwt-parse.sh
Last active December 8, 2021 19:22
Bash script to parse JSON Web Tokens and pretty-print their contents
#!/bin/bash
# Parses JSON Web Tokens (https://en.wikipedia.org/wiki/JSON_Web_Token)
# and pretty-prints their content.
#
# © 2021 Daniel Lenski <[email protected]>, MIT License
jq=$(which jq || echo cat)
for jwt in "$@"; do
@dlenski
dlenski / procnet.py
Last active August 28, 2023 13:54
Print /proc/net/{tcp,udp}{,6} in a more human-readable format
#!/usr/bin/python3
import argparse
from ipaddress import IPv4Address, IPv6Address
from binascii import unhexlify
from struct import unpack
import tabulate
def hexint(x):
return int(x, 16)
@dlenski
dlenski / cert_fingerprint_test.py
Last active January 22, 2025 14:15
Fingerprint-based certificate validation in Python (including pin-sha256)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# This is a demonstration of how to do fingerprint-based certificate
# validation in Python, in the style of OpenConnect:
# https://gitlab.com/openconnect/openconnect/-/blob/HEAD/library.c#L1084-1143
#
# For Python <3.7, we monkey-patch ssl.SSLSocket directly, because ssl.SSLContext.sslsocket_class
# isn't available until Python 3.7. For Python 3.7+, we set ssl.SSLContext.sslsocket_class
# to our modified version (which is sort of monkey-patching too).
@dlenski
dlenski / formatted_link.js
Last active September 23, 2024 16:10
Bookmarklet to copy current page title as a rich-text formatted link
@dlenski
dlenski / geoclue.py
Created December 18, 2019 01:32
Playing around with freedesktop/python-geoclue
#!/usr/bin/env python
# old, Python 2.x only :-(
# https://github.com/freedesktop/python-geoclue
from __future__ import print_function
import Geoclue
from datetime import datetime
print("Geoclue version %s" % Geoclue.VERSION)
@dlenski
dlenski / make_RSA_token.sh
Last active January 20, 2021 17:51
Make a working RSA token from seed, expiration date, and serial number
#!/bin/bash
# Takes SN, EXPIRATION, and SEED environment variables
# (SEED must be 32 hex digits) and converts them to
# an RSA SecurID token in CTF format.
#
# Requires:
# stoken >=v0.9
# perl5
# base64