Categorylinux

Apply delay to MKV/eac3to chapter files

Small bash script to apply a delay to MKV/eac3to chapter files (CHAPTERXX=/CHAPTERXXNAME= format):

#!/bin/bash
# usage: ./chapshift.sh "+0.792 seconds" < <input> > <output>
set -o errexit -o noclobber -o nounset -o pipefail

date_offset="$1"

apply_delay() {
    date --date="$1 $date_offset" +%T,%N | cut -c 1-12
}

while read -r origline
do
    line=`echo ${origline} | sed 's/\=/\=\ /g' | sed 's/\n//g'`
    if [[ $line =~ ^CHAPTER[0-9][0-9]=\ [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9] ]]
    then
        read -r chapter start_date <<<"$line"
        if [[ $start_date =~ 00:00:00.000.* ]]
        then
            new_start_date="00:00:00.000"
        else
            new_start_date="$(apply_delay "$start_date")"
        fi
        chapter=`echo ${chapter} | sed 's/=\ /=/g'`
        new_start_date=`echo ${new_start_date} | sed 's/\,/\./g'`
        printf "%s%s\n" "${chapter}" "$new_start_date"
    else
        printf "%s\n" "$origline"
    fi
done

Run any program through a SOCKS proxy (chain)

proxychains-ng enables you to easily route any program trough a proxy (chain) without the need to work yourself through tons of man pages and parameter lists.

git clone https://github.com/rofl0r/proxychains-ng.git
cd proxychains-ng/
./configure --prefix=/usr/
make
sudo make install

After installing you can either create a default config at /etc/proxychains.conf or pass a custom location/file using -f <filename>. A sample config is located at src/proxychains.conf, which can be installed at the default location using sudo make install-config.

strict_chain #chain proxies in the order as they appear in the list (all proxies must be online)
proxy_dns #proxy DNS requests
remote_dns_subnet 224 #subnet for internal remote DNS mapping
# Some timeouts in milliseconds (defaults)
tcp_read_time_out 15000
tcp_connect_time_out 8000
chain_len=1 # Chain length

[ProxyList]
socks5 127.0.0.1 1080 # shadowsocks
socks4 127.0.0.1 9050 # tor
http   somehost  8080 <user> <password> # http

proxychains-ng supports different chaining modes:

  • strict_chain: use all proxies in the order they appear in the config
    • all must be online
  • dynamic_chain: use all online proxies in the order they appear
    • at least one must be online
  • round_robin_chain: proxy chain of online proxies with a length of chain_len
    • Previously used servers are skipped/used as an offset for round robin
  • random_chain: as the name says… random proxy chain with a length of chain_len

Using proxychains-ng is as simple as:

proxychains4 wget <url>
proxychains4 -f <file.conf> ssh user@host

Using lockfile to create a semaphore in bash scripts

Sometimes you want to limit the number of times a script can run in parallel. You can do so by using lockfile (part of procmail). For example: want to make sure a script only has one instance? Simply add

lockfile -r 0 "<lock file name>" || exit 1

to the start of your script. Retry count, sleep time, etc are configurable. The -! flag inverts the exit status, making in possible to use lockfile as break conditions in a while loops with bash.

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