Skip to content

Instantly share code, notes, and snippets.

@jn0
jn0 / ssh.py
Created August 9, 2017 15:44
Custom paramiko-based python SSH client class to asynchronously run time-limited non-interactive commands and return exit status along with stdout and stderr.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Custom SSH client.
1. It somehow _supports_ async execution.
2. It provides exit status.
3. It jams stdin (ssh -nT).
Run like something "python ssh.py box user=bs command='echo aaa; echo bbb >&2; sleep 40; exit 23'" to test
@jn0
jn0 / rtsp-rtp-sample.py
Created December 2, 2016 08:43
Sample Python script to employ RTSP/RTP to play a stream from an IP-cam (from stackoverflow)
"""
http://stackoverflow.com/questions/28022432/receiving-rtp-packets-after-rtsp-setup
A demo python code that ..
1) Connects to an IP cam with RTSP
2) Draws RTP/NAL/H264 packets from the camera
3) Writes them to a file that can be read with any stock video player (say, mplayer, vlc & other ffmpeg based video-players)
Done for educative/demonstrative purposes, not for efficiency..!
@jn0
jn0 / tzsex.py
Last active May 23, 2023 07:52
Sex with times and zones
# first off, there is *no* *good* builtin TZ handling in Python.
# No. Nothing. At all.
# you have to `pip install pytz pytzdata tzlocal` to survive...
# maybe.
# he-he.
import datetime
import tzlocal
right_now_and_here = datetime.datetime.now().replace(tzinfo=tzlocal.get_localzone())
@jn0
jn0 / pelco_d.py
Created November 27, 2018 09:05
PELCO-D Protocol
# -*- coding: utf-8 -*-
'''The Pelco-D Protocol
Standard Number TF-0002
Version 2 Revision 1 Release Date August 15, 2003
Transmitters will format a single character and receivers will be able to
decipher a single character as: 1 start bit, 8 data bits, 1 stop bit,
and no parity.
@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 / 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 user@example.com -s "Note: warning"`
where `smail.yaml` looks like this:
---------------------------------- >8 -----------------------------------------
mail:
host: smtp.mail.net
sender: bot.1@mail.net
password: !secret mail_password
@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