Skip to content

Instantly share code, notes, and snippets.

@lmlsna
lmlsna / config
Last active October 17, 2017 18:16
Enable a lxc container to use a tuntap device
# Add these lines to the container's config
lxc.cgroup.devices.allow = c 10:200
lxc.hook.start /usr/local/bin/mktun
@lmlsna
lmlsna / ssh-on-first-boot-rpi.sh
Last active November 18, 2017 00:17
script to enable SSH on a fresh Raspbian (Raspberry Pi) install
#!/bin/bash
# Enables SSH on first boot by touching a blank file called `ssh` in the boot directory
# Requies the $disk argument (in the format /dev/sdX) to be set or passed as the fisrt command line flag.
disk="$disk$1"
if [[ -z "$disk" ]]; then
echo "You need to pass the install device (/dev/sdX) with the \$disk arg or as the 1st param"
exit 1
fi
@lmlsna
lmlsna / python2rc.py
Created November 4, 2017 11:27
Enable tab completion in an interactive python2 shell
'''
This script enables tab competion for functions in the Python2 interactive shell.
It requires the readline and rlcompleter packages (install with pip) and set PYTHONSTARTUP
to this file's location as an enironmental variable in your .bashrc file.
'''
try:
import readline
except ImportError:
pass
@lmlsna
lmlsna / uniq
Created November 18, 2017 00:17
Bash script for a better version of the `uniq` shell command
#!/bin/bash
# uniq - The default version of the uniq command only finds uniques that are adjacent
# and has no flag to find non-adjacent lines... so pretty useless. This is a
# bash script that actually does what you would expect uniq to do.
# for every piped in line:
for w in $(cat); do
# if we haven't already added it before
if [[ "$(grep -o "$w" <<< "$once")" == "" ]]; then
# add it + newline
@lmlsna
lmlsna / count-frames.sh
Last active February 19, 2024 09:33
Return the number of frames in a video file using ffprobe (ffmpeg)
#!/bin/bash
# Print the number of video frames in $1
ffprobe -count_frames -v error -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 ${1}
@lmlsna
lmlsna / iptables-whitelist-cloudflare.sh
Created November 27, 2017 18:30
A Bash one-liner to whitelist Cloudflare's IPs in iptables.
#!/bin/bash
iptables -A INPUT -p tcp -m multiport --dports 80,443 -s $(for ip in $(curl -s https://www.cloudflare.com/ips-v4); do echo -n "$ip,"; done|head -c-1) -j ACCEPT
ip6tables -A INPUT -p tcp -m multiport --dports 80,443 -s $(for ip in $(curl -s https://www.cloudflare.com/ips-v6); do echo -n "$ip,"; done|head -c-1) -j ACCEPT
@lmlsna
lmlsna / keybase.md
Created December 2, 2017 05:59
keybase.md

Keybase proof

I hereby claim:

  • I am lmlsna on github.
  • I am mlsna (https://keybase.io/mlsna) on keybase.
  • I have a public key ASAUo_z8mC69AnoQfAqJ1vnKKTSs4kfZjqoWi6iYP2KofQo

To claim this, I am signing this object:

@lmlsna
lmlsna / setup-unpriv-lxc-gui.sh
Created December 14, 2017 10:04
Setup unprivileged LXC containers with GUI
#!/bin/bash
# Sets up the directory tree and necessary files for unpriv LXC GUI containers.
# Expects the default LXC setup (lxcbr0) to be functioning
# Need to install desktop environment inside each container.
UNAME="ubuntu"
UHOME="/home/$UNAME"
mkdir -p "$UHOME/.config/lxc" # /etc/lxc
touch "$UHOME/.config/lxc/lxc.conf" # /etc/lxc/lxc.conf
@lmlsna
lmlsna / php.ini
Created January 17, 2018 09:08
PHP7 Error reporting values
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
track_errors = On
html_errors = On
error_prepend_string = "<span style='color: #ff0000'>"
error_append_string = "</span>"
@lmlsna
lmlsna / str_to_int.sh
Created February 1, 2018 01:35
Convert string to int in bash.
#!/bin/bash
# This is a simple bash function to change strings (of arbitrary length) into integers (of arbitrary maximums).
# I use this to convert my "user@hostname" to a number between 1-255 and use that as a persistent color for the
# termnal prompt to remind me what box/user I am on.
#
# It just hashes whatever string you give it, converts the hex to a decimal and then mods around whatever your max int is.
# Usage: str_to_int [string_to_convert] [max_int_size]
# Example: str_to_int "user@hostname" 255
#