Skip to content

Instantly share code, notes, and snippets.

Avatar
👁️
Miles Richardson

Miles Richardson milesrichardson

👁️
Miles Richardson
View GitHub Profile
View inherit_environment_variables_from_pid_1.md

You can inherit the environment variables from PID 1 by iterating over the list of null-terminated strings in /proc/1/environ, parsing the first characters up to the first = as the variable name, setting the remaining value as that variable, and exporting it.

The Code Snippet

This works with multiline environment variables, and environment variables with arbitrary values, like strings, including = or JSON blobs.

Paste this in your current terminal session to inherit the environment variables from PID 1:

@milesrichardson
milesrichardson / wireshark-remote-capture-ssh-docker.md
Last active Jan 25, 2023
bash command to open wireshark and capture packets in a remote docker container on a remote machine over SSH
View wireshark-remote-capture-ssh-docker.md

capture those packets

run this command on local machine where wireshark is installed (e.g. MacOS)

export raw_pcap="$(mktemp -t pcap-raw)" ; \
echo "Raw pcap: $raw_pcap" ; \
wireshark -k -i <(ssh ubuntu@my-cool-server.example.com '\
  docker run --rm \
 --net container:$(docker ps -qf name=haproxy) \
@milesrichardson
milesrichardson / generate-wireguard-peer.py
Created Aug 2, 2022
Python script to create random wireguard peer by calling`wg genkey`, `wg pubkey` and `wg genpsk` with `subprocess` module
View generate-wireguard-peer.py
# Hopefully it saves you a few minutes looking at the subprocess docs
import os
import subprocess
env = os.environ.copy()
private_key = (
subprocess.run(
["wg", "genkey"],
@milesrichardson
milesrichardson / restart_spotify.sh
Created May 17, 2020
"Spotify cannot play the current song" - So restart it! (restart spotify from mac command line)
View restart_spotify.sh
#!/usr/bin/env bash
# I got sick of constantly getting the error from spotify that it cannot play
# the current song. It was really annoying to have to quit, open spotlight,
# Type "spot" and accidentally open spotlight system preferences, wait for spotify
# to open, etc...
# This script, on a mac, uses osascript to quit and relaunch spotify and press play
osascript -e 'tell application "Spotify" to quit'
@milesrichardson
milesrichardson / FixTmuxWeirdCharacters.md
Last active Nov 5, 2019
How to stop weird characters being inserted on paste in tmux
View FixTmuxWeirdCharacters.md

ChrisJohnsen/tmux-MacOSX-pasteboard#31 (comment)

What happens if you paste (in a pane where you see the problem) while running cat -v? Do you see something like ^[[200~hello world^[[201~? If so, what you are seeing is bracketed paste mode.

You should be able to turn the mode off by running printf '\e[?2004l'

@milesrichardson
milesrichardson / HowToKeepGitHubForkUpToDateWithGitLabMirroring.md
Created Oct 23, 2019
How to keep GitHub forks up to date by leveraging GitLab repository mirroring to and from GitHub
View HowToKeepGitHubForkUpToDateWithGitLabMirroring.md

What you want

  • A public fork of a public GitHub repository, say facebook/react
  • A private fork of the same public GitHub repository
  • Keep both forks always up to date with all branches of the upstream facebook/react repository
  • Selectively publish branches from the private fork to the public fork in order to submit pull requests to facebook/react

What you need

  • A GitHub account
@milesrichardson
milesrichardson / find_symbol.sh
Created Oct 22, 2019
Function for finding where missing symbol is defined
View find_symbol.sh
# Usage: find_symbol _ZN6icu_608ByteSinkD2Ev
# Output list of files on system where given symbol exists
# Symbol should be formatted as given by output of ldd -r
find_symbol() {
local symbol="$1"
shift
while read -r filename ; do
readelf -Ws "$filename" 2>/dev/null | grep "$symbol" && echo "$filename" ;
done < <(find / -type f -name '*.so' -o -name '*.bc' -o -name '*.a')
@milesrichardson
milesrichardson / docker_host_vm_shell.sh
Created Oct 16, 2019
Get shell in docker host VM on mac
View docker_host_vm_shell.sh
docker run -it --rm --privileged --pid=host justincormack/nsenter1
@milesrichardson
milesrichardson / README.md
Last active Dec 6, 2018
Create new mongo database and user with nodejs MongoClient
View README.md

How to create a previously non-existing mongo database and new user from the nodejs mongodb driver using the admin account.

  1. Connect as admin user to new database, using authSource: admin (assuming adminDbName === admin)
  2. Create new user
@milesrichardson
milesrichardson / README.md
Last active Jun 26, 2018
pairwise.js: Given a list, return a list of all pairs in that list via Array.reduce(). Optionally filter and transform (map) the pairs based on items and indices.
View README.md

pairwise.js

pairwise(list, options)

Given a list, return a list of all pairs in that list via Array.reduce()

Optionally filter and transform (map) the pairs based on items and indices.