Skip to content

Instantly share code, notes, and snippets.

View lapo-luchini's full-sized avatar

Lapo Luchini lapo-luchini

View GitHub Profile
@lapo-luchini
lapo-luchini / zsh_history_sort.js
Created October 8, 2024 20:20
Sort extended ZSH history (works with multi-line entries)
#! /usr/bin/env node
const fs = require('fs');
const lines = fs.readFileSync(process.argv[2], 'utf8').split(/(?<=[^\\])\n/);
lines.sort((a, b) => a.split(/:/)[1] - b.split(/:/)[1]);
fs.writeFileSync(process.argv[2], lines.join('\n'), 'utf8');
@lapo-luchini
lapo-luchini / hetznerInvoicesCSV.js
Last active November 29, 2023 11:25
Download all CSV invoices from Hetzner
let elem = Array.prototype.slice.call(document.getElementsByClassName('btn-download')).map(e=>e.href).filter(u=>u.endsWith('/csv'));
let numbers = elem.map(u=>/invoice[/]([^/]+)/.exec(u)[1]);
let filename = 'invoice-' + numbers[0] + '-' + numbers[numbers.length - 1] + '.csv';
let csv = '';
for (let url of elem)
csv += await (await fetch(url)).text();
let a = document.createElement("a");
a.download = filename;
a.href = a.href = "data:text/csv,"+encodeURIComponent(csv);
a.click();
@lapo-luchini
lapo-luchini / decodeKNX.mjs
Created July 1, 2023 14:46
Decode encrypted KNX project using NodeJS
import util from 'node:util';
import * as fs from 'node:fs/promises';
import * as crypto from 'node:crypto';
import Minizip from 'minizip-asm.js';
const
reFile = /^P-[0-9A-F]{4}([/]0[.]xml|[.]zip)$/,
pbkdf2 = util.promisify(crypto.pbkdf2);
export async function decodeProject(proj, password) {
@lapo-luchini
lapo-luchini / satispay-qif.sh
Created July 1, 2023 11:29
Convert SatisPay CSV export to QIF format
#!/bin/sh
export LANG=C
cat Satispay-*.csv | \
gawk -F ' *, *' '
NR>1 {
match(substr($5, 2), "^([0-9]{1,2}) ([a-z]{3}) ([0-9]{4})$", a)
switch (a[2]) {
case /gen|jan/: a[2] = "01"; break;
case "feb": a[2] = "02"; break;
case "mar": a[2] = "03"; break;
@lapo-luchini
lapo-luchini / tmuxSetup.sh
Last active February 18, 2023 17:43
Open a few `tmux` tabs with specific name and commands
#!/bin/sh
awk -F"\t" -vq="'" '{
t=system("tmux new-window -n " q $1 q)
system("tmux send-keys -t " q t q " " q $2 q " C-m")
}' <<EOF # commands can contain ", but avoid ' and tabs
tab1 echo command on tab 1
tab2 echo command on tab 2
tab3 echo tab 3 ; echo "tab 3 cmd 2"
EOF
@lapo-luchini
lapo-luchini / md5le.js
Last active May 7, 2022 12:50
Automate solution of MD5LE
// https://md5le.vercel.app/
const
url = 'https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt',
fs = require('fs').promises,
hash = require('crypto').createHash('md5'),
rl = require('readline/promises').createInterface({
input: process.stdin,
output: process.stdout
}),
@lapo-luchini
lapo-luchini / createPDF.html
Created February 24, 2022 10:33
Create a custom PDF in base64 form (with preview)
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Generate PDF</title>
<script src="https://github.com/devongovett/pdfkit/releases/download/v0.6.2/pdfkit.js"></script>
<script src="https://github.com/devongovett/blob-stream/releases/download/v0.1.2/blob-stream-v0.1.2.js"></script>
<style>
html,
@lapo-luchini
lapo-luchini / zrepl_cert_gen.sh
Created October 5, 2021 23:37
A script to create a local CA for you zrepl installation. Uses elliptic curves and SANs to be compatible with latest go.
#!/bin/sh
if [ $# -ne 1 ]; then
echo "Usage: $0 FQDN"
exit 1
fi
cd `dirname "$0"`
if [ ! -f ca.crt ]; then
openssl ecparam -genkey -name prime256v1 -out ca.key
openssl req -x509 -new -SHA256 -nodes -days 3652 -subj "/CN=Root CA/OU=zrepl/O=YourName/C=IT" -key ca.key -out ca.crt
fi
@lapo-luchini
lapo-luchini / number100.js
Last active October 5, 2021 22:53
Generates flashcards to study multiplications and division of numbers up to 100. Can be imported into Anki via the File→Import option, use "Basic" type cards.
// generates flashcards to study multiplications and division of numbers up to 100
// can be imported into Anki via the File→Import option, use "Basic" type cards
let x = {};
for (let a = 2; a <= 100; ++a)
x[a] = [];
for (let a = 2; a < 100; ++a)
for (let b = a; b < 100; ++b) {
let m = a * b;
if (m > 100)
continue;
@lapo-luchini
lapo-luchini / smartmon_exporter.sh
Last active February 19, 2021 16:13 — forked from hadret/smartmon.sh
smartmon.sh script adapted for FreeBSD
#!/bin/sh
# Script informed by the collectd monitoring script for smartmontools (using smartctl)
# by Samuel B. <samuel_._behan_(at)_dob_._sk> (c) 2012
# source at: http://devel.dob.sk/collectd-scripts/
# TODO: This probably needs to be a little more complex. The raw numbers can have more
# data in them than you'd think.
# http://arstechnica.com/civis/viewtopic.php?p=22062211
# can be add to /etc/crontab like this, and node_exporter will see it automatically: