Skip to content

Instantly share code, notes, and snippets.

@jn0
jn0 / ssh-reuse-session.sh
Created April 7, 2022 10:14
how to reuse ssh session (and do not enter password more than once)
#!/bin/bash
# from https://serverfault.com/a/842825/200976
# Create ssh-mux (SSH multiplex) dir only if not exists
[[ ! -d dir ]] || mkdir ~/.ssh/ssh-mux
apache_site_path_source="/var/www/source_site"
apache_site_path_target="/var/www/target_site"
@jn0
jn0 / cx.py
Created February 15, 2022 17:00
How to parse/fetch attributes from a CryptoPro generated certificate
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat._der import DERReader, SEQUENCE
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends.openssl.backend import hashes
from binascii import hexlify
import sys
import json
with open('oids.json', 'rt') as f:
@jn0
jn0 / smail.py
Last active January 27, 2022 09:21
Simple SMTP Mail Sender
#!/usr/bin/env python3
'''
Run as `echo MESSAGE | smail.py smail.yaml [email protected] -s "Note: warning"`
where `smail.yaml` looks like this:
---------------------------------- >8 -----------------------------------------
mail:
host: smtp.mail.net
sender: [email protected]
password: !secret mail_password
@jn0
jn0 / patch.py
Last active August 13, 2022 09:04
Two-pass multi-run-safe micro (85 LOC) patcher in python
#!/usr/bin/env python3
'''
Two-pass re-run safe micro (85 LOC) patcher in python.
Sample patch config (yes, it's in YAML):
---->8----
version: 0
title: tribute to sublimetext_4126_crack_linux.py by dmknght @github
file: /tmp/sublime_text
# size: 8862888 # optional check for file size
@jn0
jn0 / ovpnctl.py
Last active January 10, 2022 07:36
Updated controller for OpenVPN with TOTP static challenge support (it runs openvpn on its own)
#!/usr/bin/env python3
'''
Configure sudo(8) to run openvpn(8) without password prompt by adding
you ALL = NOPASSWD: /usr/sbin/openvpn
to your sudoers file. The script will try to verify your sudo(8) config.
Note: the command to run is
sudo openvpn --config myconfig.ovpn
where `myconfig.ovpn` is your actual config.
@jn0
jn0 / ovpnctl.py
Last active December 23, 2021 16:06
OpenVPN controller (via management socket protocol) to employ TOTP static challenge/response feature auth
#!/usr/bin/env python3
'''
Start your OpenVPN config with
client
management myconfig.socket unix
management-query-passwords
lines.
Make sure the config has
static-challenge "Enter PIN: " 1
@jn0
jn0 / md2pdf.sh
Created June 2, 2021 13:07
markdown to PDF converter (and filter)
#!/bin/bash
# sudo apt install them first:
# pandoc evince # pandoc + evince
# texlive-xetex # xelatex
# ttf-mscorefonts-installer # Georgia TTF
args=( "$@" )
tmpd="$(mktemp -p /tmp -d md2pdf.$$.XXXXXXXXXX.tmp)"
@jn0
jn0 / ddict.py
Created January 13, 2021 13:02
DottedDict replacement
#!/usr/bin/env python3
'''
Simple dict with dot-syntax access to its data.
Mostly for configs (the keys are already known).
Example:
cfg = DottedDict().load_json('config.json')
print(cfg.application.db.name)
'''
from collections import UserDict
@jn0
jn0 / sql-to-string-with-params.py
Created October 8, 2020 14:29
How to print *full* SQL from SQLalchemy with all the parameters substituted (still may need fixes for psql)
def SQL(query):
sql = str(query)
prm = query.compile().params.copy()
for p in prm:
t = ':' + p
sql = sql.replace(t, f'{prm[p]!r}')
return '\t'.join(sql.splitlines())
@jn0
jn0 / .bash4git.sh
Created September 25, 2020 13:29
bashrc feature to avoid wrong edits
# Usage: source it from ~/.bashrc before unsetting color_prompt
# it will notify you when you're about to crack a git repo at wrong branch...
_is_git() {
local wd=`pwd`
while [ -n "$wd" -a "$wd" != '/' ]; do
[ -d "$wd/.git/." ] && { echo "$wd"; return 0; }
wd="$(dirname "$wd")"
done
return 1