Skip to content

Instantly share code, notes, and snippets.

@renier
renier / goexplain.sh
Created May 1, 2024 21:35
Lists out the dependencies importing the module you specify all the way up to the project's main module
#!/bin/bash
home_module="$(go list -m | head -1)"
cache="$(mktemp)"
gomodule="go@$(go version | cut -d ' ' -f 3 | sed -e 's/^go//' | cut -d '.' -f 1,2)"
function explain() {
local importers="$(go mod graph | grep " ${1}" | cut -d ' ' -f 1)"
local importer;
for importer in $importers; do
@renier
renier / multi_page_image.md
Last active June 2, 2020 18:35
Breaking an image into multiple pages

I sometimes use Awesome Screenshot Plus to print a web page. It comes in handy as not all web pages are optimized for printing. This can result in a very long image if the content is multiple pages long, which is also not great for printint it out, since everything will come out as a single page due to scaling (on OSX).

There are probably other ways to solve this, but I've found ImageMagick to work very well in these cases. You'll need to know how many pages you need. For that, you'll need to know the dimensions of the source image. You can also gather this info with ImageMagick.

$ convert doc.jpg -print "Size: %wx%h\n" /dev/null

Now that you know the pixel width and height, use some math to figure out how many pages you'll need. The following is an example for letter size pages (8.5 x 11 inches):

@renier
renier / generateRandomNumber.js
Last active November 22, 2019 19:37
Generate random number with bit length specified in Node.js
const crypto = require('crypto');
// Generates a random number of the bit length specified in base 16 format (hexadecimal).
// bitLength should be a multiple of 8.
function generateRandomNumber(bitLength) {
return crypto.randomBytes(bitLength / 8).toString('hex');
}
// > generateRandomNumber(64)
// '9c493dcad246f3c1'
@renier
renier / search_and_cmd.sh
Created August 16, 2019 02:16
Execute commands on all found files even if they have spaces and quotes in their names
while read f; do
$(cmd) -- "${f}"
done < <(find . -name "*.ext" -print0 | xargs -0 -n1 grep -Hsl "foobar")
@renier
renier / fast_harden_ubuntu.sh
Last active February 24, 2019 05:35
Fast Harden Ubuntu
#!/bin/bash
apt-get install ufw -y
apt-get install denyhosts -y
# Configure firewall
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw --force enable
@renier
renier / gowo.sh
Last active March 16, 2018 17:39
gowo - Go workspace switcher
# Usage: gowo my_go_workspace
# Will set GOPATH and update PATH with the bin dir. Will remove previous GOPATH bin dir from the PATH.
# Opinionated about the root dir where Go workspaces go. Change if you want.
# Put this in your ~/.bash_profile
export GOPATH=$HOME/Projects/go
export PATH=$PATH:$GOPATH/bin
function gowo() {
@renier
renier / gen_cert.sh
Created October 30, 2016 08:06
Generate an SSL certificate
#!/bin/bash
DOMAIN=$(hostname -f)
export PASSPHRASE=$(head -c 500 /dev/urandom | tr -dc a-z0-9A-Z | head -c 128; echo)
subj="
C=$1
ST=$2
O=$3
localityName=$4
commonName=$DOMAIN
organizationalUnitName=$5
@renier
renier / super_harden_ubuntu.sh
Last active February 10, 2019 02:05
Super Harden Ubuntu
#!/bin/bash
# https://developer.ibm.com/answers/questions/462237/error-groot-must-be-grub-root-device-on-ubuntu/
sed -i -e 's/LABEL=cloudimg-rootfs/(hd0)/' /boot/grub/menu.lst
apt-get update > /dev/null
apt-get install unattended-upgrades -y
timeout 20m unattended-upgrade
apt-get autoremove -y
apt-get autoclean -y
@renier
renier / harden_ubuntu.sh
Last active September 8, 2017 05:56
Harden Ubuntu
#!/bin/bash
apt-get update
apt-get upgrade -y
apt-get autoremove -y
apt-get autoclean -y
apt-get install ufw -y
apt-get install denyhosts -y
# Configure firewall
@renier
renier / httparty_uses_basic_auth_creds_in_url.rb
Created November 17, 2014 19:21
Make HTTParty pick out basic auth credentials from URL
module HTTParty
module ClassMethods
private
alias_method :orig_perform_request, :perform_request
def perform_request(http_method, path, options, &block)
if path.include? '@'
options ||= {}
scheme_auth, host_uri = path.split('@')
scheme, creds = scheme_auth.split('//')