Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am lachesis on github.
  • I am aftbit (https://keybase.io/aftbit) on keybase.
  • I have a public key whose fingerprint is 9E15 397E 4D53 7E3A 3A23 8F87 E620 C8A7 4BAF 5D09

To claim this, I am signing this object:

@lachesis
lachesis / Makefile.patch
Created May 7, 2015 04:49
Naive implementation of CSV parsing using nested strtok and a mmap'd file
--- a/Makefile 2010-08-23 08:46:24.000000000 -0700
+++ b/Makefile 2015-05-06 21:20:20.177910661 -0700
@@ -100,15 +100,15 @@
################
## CHANGE NAME OF ANSI COMPILER HERE
################
-CC =
+CC = gcc
# Current values for DATABASE are: INFORMIX, DB2, TDAT (Teradata)
# SQLSERVER, SYBASE, ORACLE
@lachesis
lachesis / letsencrypt_notes.sh
Last active December 13, 2023 11:02
Set up LetsEncrypt using acme.sh without root
# How to use "acme.sh" to set up Lets Encrypt without root permissions
# See https://github.com/Neilpang/acme.sh for more
# This assumes that your website has a webroot at "/var/www/<domain>"
# I'll use the domain "EXAMPLE.com" as an example
# When this is done, there will be an "acme" user that handles issuing,
# updating, and installing certificates. This account will have the following
# (fairly minimal) permissions:
# - Host files at http://EXAMPLE.com/.well-known/acme-challenge
@lachesis
lachesis / sb6190_notes.md
Last active November 13, 2016 04:43
Arris SB6190 bug notes

I'm experiencing the weirdest internet issue. I use Comcast Xfinity in El Cerrito, CA. I have a new Arris SB6190 cable modem running firmware version 9.1.93N. A few days ago, I noticed that new tabs start taking a few seconds to load, and curl requests to example.com took ~5.1 seconds. However, if I specified -4 or -6 to curl, it resolved immediately. I immediately suspected DNS, but "host" resolved immediately as well.

So this was too weird to ignore. I set my resolv.conf to point to 8.8.8.8 (instead of my EdgeRouter X) and did:

time strace -f -T curl -s http://www.example.com > /dev/null 2> bad
time strace -f -T curl -4 -s http://www.example.com > /dev/null 2> good

Now if you look at the resulting logs, you'll see that curl starts a new thread to handle DNS resolution. In the good logs, the relevant bit looks like:

[pid 12438] socket(AF_INET, SOCK_DGRAM|SOCK_NONBLOCK, IPPROTO_IP) = 3 <0.000005>

@lachesis
lachesis / 2fa.py
Created September 21, 2018 20:07
Quick Py2/Py3 TOTP Generator
from __future__ import print_function, division, unicode_literals
import sys, time, re, struct, base64, hmac, hashlib
def get_hotp(secret, counter):
"""Return the HMAC-Based One-Time Password for the the given secret (base32 encoded) and the counter.
>>> [get_hotp('GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', i) for i in xrange(10)]
[755224, 287082, 359152, 969429, 338314, 254676, 287922, 162583, 399871, 520489]
"""
secret = re.sub(r'\s*', '', secret) # remove whitespace
secret = secret.upper() # uppercase
if len(secret) % 8 != 0:
@lachesis
lachesis / statds-last.sh
Created June 11, 2019 02:42
Get last value of statsd gauge from grafana server
#!/bin/bash
# Get last value of statsd gauge from grafana server
# Set env vars:
# GRAFANA_BASE="https://www.example.com" <-- your grafana base URL
# GRAFANA_API_KEY="eyJryJzd.....IxMj0F9" <-- your grafana API key
curl --compressed --silent \
"$GRAFANA_BASE/api/datasources/proxy/1/render" \
-H "Authorization: Bearer $GRAFANA_API_KEY" \
--data-urlencode "target=summarize($1, "'"1h", "last")' \
#!/usr/bin/env python3
# Subscribes contacts in a CSV file to a Close campaign
# Requires python3.7 or newer (f-strings)
# Prepare: save this script in close_sub.py and `pip3 install closeio`
# CSV should be two columns with email followed by exact Sequence name on Close
# Usage: KEY=api_yourcloseiokey LIVE=0 python3 close_sub.py leads.csv
import csv
import fileinput
import os
import re
@lachesis
lachesis / airnow.py
Last active December 3, 2022 20:25
Display AQI for a given LAT/LNG using data from https://fire.airnow.gov (AirNow, AirSis, Purple Air)
#!/usr/bin/python3
# Prints current PM2.5 concentrations based on fire.airnow.gov map
# pip3 install requests pytz geobuf
# usage: LAT=37.9053745 LNG=-122.3048239 MAX_FEATURES=6 MAX_DIST=11 python3 airnow.py
# optional, for AQI instead of raw concentration (µg/m³): pip3 install python-aqi and pass AQI=1
import collections
import datetime
import math
import os
import requests
#!/usr/bin/env python3
# Get duke energy hourly electricity usage
# before running: pip3 install requests
# use env vars to set username, password, and lookback days
# e.g. DUKE_USERNAME=myemail@example.com DUKE_PASSWORD=hunter11 DUKE_DAYS_BACK=14 python dukeusage.py
import os
import datetime
import json
import re
@lachesis
lachesis / expiringlru.py
Last active December 3, 2022 20:25
Python expiring LRU
# License: Public Domain, use as you like, no warranty
import time
import threading
import pylru # from pip
class ExpiringLRUCache(object):
def __init__(self, size, exp=7200):
self.exp = exp
self.size = size
self.lru = pylru.lrucache(size)