PDF to JPG using ImageMagick’s convert

…while dealing with alpha/transparency:

convert -verbose -density 150 -trim <input>[PAGE-RANGE] -quality 100 -sharpen 0x1.0 -background white -alpha remove <output>

pdf2jpg.sh, usage: pdf2jpg.sh <input> [page-range], page range starting at 0.

#!/bin/bash
INPUT=$1
PAGES=$2
ME=`basename "$0"`
if [[ ! -f "${INPUT}" ]]
then
	echo "Input not found"
	echo "Usage: ${ME} <pdf> [[pages]]"
	exit 1
fi
if [[ $(file --mime-type -b "${INPUT}") != "application/pdf" ]]
then
	echo "Input not a PDF"
	exit 1
fi
BASENAME="`basename "${INPUT}" .pdf`"
OUTPUT=$(mktemp -q -u "${BASENAME}.XXXXXXXXX")
convert -verbose -density 150 -trim "${INPUT}${PAGES}" -quality 100 -sharpen 0x1.0 -background white -alpha remove "${OUTPUT}-%03d.jpg"

Reconnect VPN on connection loss using NetworkManager’s nmcli

Find the <UUID> of your VPN connection using:

nmcli connection show

Using nmcli you can (re-)connect to your VPN by:

nmcli connection up uuid 

Checking every 10 seconds, if VPN is still up, and reconnect otherwise:

#!/bin/bash +x
UUID="<UUID>"
while (true)
do
        VPNCON=$(nmcli connection show --active | grep -i vpn | grep -i "${UUID}" | cut -f3 -d " ")
        if [[ $VPNCON != "${UUID}" ]] # Double check
        then
                nmcli connection up uuid "${UUID}"
        fi
        sleep 10
done

Block all but OpenVPN traffic aka kill switch

I set up a Xubuntu VM the other day and wanted to make sure, that there’s no traffic besides VPN traffic possible. <insert zomg torrent plz donald duck pic>

# Reset firewall rules
sudo ufw reset
sudo ufw enable

# Load default policies
sudo ufw default deny incoming
sudo ufw default deny outgoing

# Allow traffic on OpenVPN interface
sudo ufw allow in on tun0
sudo ufw allow out on tun0

# Allow traffic to local network
sudo ufw allow in on enp0s3 from 10.0.2.0/24
sudo ufw allow out on enp0s3 to 10.0.2.0/24

# Allow traffic on OpenVPN port via local network
sudo ufw allow in on enp0s3 from any port 1194
sudo ufw allow out on enp0s3 to any port 1194

# DNS: May result in DNS leak
# sudo ufw allow in from any to any port 53
# sudo ufw allow out from any to any port 53

Use ssh tunnel as SOCKS proxy

ssh <user>@<host> -D<local_port>

<local_port> = local SOCKS proxy port to connect to, eg. 1080

See what’s going on and keep connection alive:

ssh <user>@<host> -v -2 -o TCPKeepAlive=yes -o ServerAliveInterval=10 -o ConnectTimeout=180 -D<local_port> -C sleep 9999999

You may want to while (true); do …; done the sh*t out of this…