Skip to content

Instantly share code, notes, and snippets.

@scentoni
scentoni / orl
Created April 7, 2023 19:23
Abuse openssl as a curlstitute
#!/bin/bash
# Usage:
# orl 'https://eu.httpbin.org/#/HTTP_Methods'
orl () {
url=$1
rex="\([^:]*\)://\([^/]*\)\(/.*\)"
uscheme=$(echo $url |sed "s,$rex,\1,")
uauthority=$(echo $url |sed "s,$rex,\2,")
upath=$(echo $url |sed "s,$rex,\3,")
@scentoni
scentoni / hcat
Last active February 22, 2023 05:26
A drop-in replacement for some limited uses of curl, written using only Python3 standard libraries.
#!/usr/bin/env python3
# Makes an HTTP request to the specified URL and sends response to stdout.
# A drop-in replacement for some limited uses of curl, like:
# hcat $URL -H "Authorization: Bearer $TOKEN"
import urllib.parse
import urllib.request
import sys
import argparse
@scentoni
scentoni / dockerfile
Created October 27, 2021 17:28
generate a dockerfile from an image
#!/bin/bash
# https://stackoverflow.com/questions/19104847/how-to-generate-a-dockerfile-from-an-image
case "$OSTYPE" in
linux*)
docker history --no-trunc --format "{{.CreatedBy}}" $1 | # extract information from layers
tac | # reverse the file
sed 's,^\(|3.*\)\?/bin/\(ba\)\?sh -c,RUN,' | # change /bin/(ba)?sh calls to RUN
sed 's,^RUN #(nop) *,,' | # remove RUN #(nop) calls for ENV,LABEL...
sed 's, *&& *, \\\n \&\& ,g' # pretty print multi command lines following Docker best practices
@scentoni
scentoni / hostify.pl
Last active April 7, 2022 19:28
hostify: try to translate everything that looks like IP addresses
#!/usr/bin/perl
# hostify: try to translate everything that looks like IP addresses
# into hostnames. Leave them alone if DNS doesn't resolve them.
# echo "Please tip your 127.0.0.1 well." |hostify
# Please tip your localhost well.
use Socket;
sub host { return gethostbyaddr(inet_aton($_[0]), AF_INET) // $_[0]}
@scentoni
scentoni / ibitize.py
Created October 14, 2021 17:38
ibitize: take an integer argument, return a string using ibi binary prefixes
#!/usr/bin/python
# ibitize: take an integer argument, return a string using ibi binary prefixes
# echo '32*2^30'|bc
# 34359738368
# echo 'l(34359738368)/l(2)' |bc -l
# 35.00000000000000000036
# ibitize 34359738368
# 32.0 Gi
@scentoni
scentoni / brewdesc.sh
Created May 5, 2021 17:31
pretty-print installed brew packages
brew info --json --installed |jq -r '.[] |{name: .name, desc: .desc} |join("\t")' |column -ts $'\t'
@scentoni
scentoni / decimalapproximation.py
Last active May 30, 2020 02:04
Exploring the Python decimal module and approximations of transcendental numbers
#!/usr/bin/python2.7
# How many digits can I remember? How good is that approximation?
import math
import decimal
def compute_pi():
"""Compute Pi to the current precision.
>>> print(pi())
3.141592653589793238462643383
@scentoni
scentoni / res.sh
Created November 28, 2017 18:00
reset terminal size
res() {
old=$(stty -g)
stty raw -echo min 0 time 5
printf '\0337\033[r\033[999;999H\033[6n\0338' > /dev/tty
IFS='[;R' read -r _ rows cols _ < /dev/tty
stty "$old"