Skip to content

Instantly share code, notes, and snippets.

View markscottwright's full-sized avatar

Mark Wright markscottwright

  • Washington, DC
View GitHub Profile
@markscottwright
markscottwright / p12-to-pems.py
Last active July 17, 2024 02:03
expand a pkcs12 file into certificate and key files in PEM format, in python
# require cryptography and asn1crypto
import base64
import os
from asn1crypto.keys import PrivateKeyInfo
from asn1crypto.pkcs12 import Pfx, SafeContents
from cryptography.hazmat.primitives.hashes import SHA256
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
@markscottwright
markscottwright / zoom.vim
Last active January 22, 2024 11:24
How to zoom in and out in linux gvim
nmap <C-S-+> :call ZoomIn()<cr>
nmap <C-_> :call ZoomOut()<cr>
function! ZoomIn()
let font_name=&guifont
let font_parts=split(font_name)
if len(font_parts) > 1
let font_size=str2nr(font_parts[1])
if font_size!=0 && font_size<64
let font_name=font_parts[0] . " " . (font_size + 2)
@markscottwright
markscottwright / distinguishedname.py
Last active January 10, 2024 20:51
How to parse a DN in python
import string
from io import StringIO
__all__ = ['parse_dn', 'dn_to_string']
class _Peekable:
def __init__(self, wrapped):
self.wrapped = wrapped
self.last_char = None
@markscottwright
markscottwright / set-audio
Created October 30, 2023 20:54
How to set the output audio device in modern Ubuntu-derivatives
#! /usr/bin/python3
# Note: use wpctl set-default <number> to use this information
from subprocess import check_output
import os
import re
import sys
'''
pw-cli ls | awk '/^\s*id/{ id=$2; sub(",", "", id) }/\s*node.description/{ name=substr($0, index($0, "=")+2); gsub("\"", "", name) } /\s*media.class.*Audio.Sink/ { print( id " " name ) }'
echo
echo use wpctl set-default NUMBER to use this information
@markscottwright
markscottwright / create-test-pod.sh
Created October 29, 2023 01:09
Debug kubernetes pod
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: ubuntu
labels:
app: ubuntu
spec:
containers:
- image: ubuntu
@markscottwright
markscottwright / mod_metrics.c
Created October 24, 2023 13:10
Apache module that exports metrics in Prometheus format
/*
* Need to install APR and HTTPD source
* https://github.com/apache/apr
* https://github.com/apache/httpd
* Build with `apxs -c -a mod_metrics.c`
* Install with:
* `sudo /PATH/TO/libtool --mode=install install mod_metrics.la /PATH/TO/modules/`
*/
#include "httpd.h"
#include "http_core.h"
@markscottwright
markscottwright / mysnapshot.py
Created July 6, 2023 19:33
Quick and dirty snapshot testing in python
class MySnapshot:
NO_ARGUMENT = object()
def __init__(self, expected_value=NO_ARGUMENT):
self.expected_value = expected_value
def __eq__(self, other):
if self.expected_value is self.NO_ARGUMENT:
from inspect import currentframe, getframeinfo
caller_frame = getframeinfo(currentframe().f_back)
@markscottwright
markscottwright / sample.py
Created July 5, 2023 11:46
My PyCharm console startup script
import sys
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])
from pprint import pprint as pp
try:
import dotenv
dotenv.load_dotenv()
except ModuleNotFoundError:
pass
print('Python %s on %s' % (sys.version, sys.platform))
@markscottwright
markscottwright / parse_jks.py
Created July 2, 2023 20:37
How to parse a JKS in Python
from pprint import pprint
import dataclasses
import struct
from datetime import datetime
import cryptography.x509
from cryptography.x509 import Certificate
def next_long(f):
return struct.unpack(">Q", f.read(8))[0]
@markscottwright
markscottwright / pretty.java
Created December 8, 2022 23:45
How to pretty print json using java and Jackson
HashMap<String, Object> map = new ObjectMapper().readValue(jsonString, new TypeReference<HashMap<String, Object>>() {});
String prettyJson = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(map);