System Scripts

2019-02-07 21:55 UTC
  • Xyne

About

All scripts that are somehow system-related.

If there is no description for a script below, check the script itself for comments.

Rsync-based Bash Backup Function

backup_function.sh contains a function named backup that you can use in your backup scripts. It will maintain a working copy of a directory at another location and it will also create snapshot archives of modifed and deleted files so that you can retrieve old data if necessary.

See the comments in the file for usage information along with an example of how to purge snapshot archives older than a month.

fstab-uuid

fstab-uuid parses fstab and crypttab files, replaces device paths with UUIDs, and prints the entries to STDOUT. It is meant as a helper script for migrating from paths to UUIDs. It should not be used to automatically replace these files.

It also includes a scan mode for non-fstab formulate files. In this mode it will parse a file line-by-line and print out any lines that contain paths that could be replaced by UUIDs (along with the matching UUIDs). The matching is naïve and again only meant as an aid for manually editing the file.

Subnet Launcher Function

launch_subnet.sh contains a function named launch_subnet that will automate several aspect of subnet management such as opening the required ports and starting the DHCP server. The file should be sourced from another bash script that sets the required variables before calling the function.

The file also contains a function named print_launch_subnet_usage that will list and describe the variables expected by the launch_subnet function.

For more information read my notes about configuring a subnet with DHCP and DNS. There you will also find example scripts and configuration files for

  • dhcpd
  • named
  • hostapd

Edit (2017-11-01)

I ended up rewriting the script to make it more versatile (e.g. bridging nats on the local subnet to support VMs and tethered devices with USB interfaces). I will put it back up on the site shortly along with some other ad-hoc networking tools as soon as I have time to organize them and write a decent README. Feel free to email me a reminder if I take too long.

List Apps by MIME-type

list_apps_by_mimetype.py is a Python script that uses the Mimeo module to… *drumroll* list applications by MIME-type. It was written as a quick example in response to a question on the forum.

merge-passwd

Interactively merge serveral passwd-like files (e.g. /etc/passwd, /etc/group). This is mainly intended for merging .pacnew files but should be useful in other cases.

Help Message

usage: merge-passwd [-h] [-o <path>] [-c] [-g] <path> [<path> ...]

Merge changes in password files.

positional arguments:
  <path>                The files to merge. Multiple may be given. If only one
                        is given, a matching .pacnew file must exist.

optional arguments:
  -h, --help            show this help message and exit
  -o <path>, --output <path>
                        The output file. This may be one of the input files,
                        but it is recommended to use a different file and
                        check the output before overwriting system files.
  -c, --confirm         Confirm before writing output file.
  -g, --group           Treat files as group files.

netctl-checklist

A simple dialogue that lets you simultaneously enable or disable multiple networks via netctl.

Screenshot

screenshot

timestats

timestats a command multiple times and calculates simple runtime statistics. The results can be plotted in a violin plot.

Dependencies

  • python3
  • python-numpy
  • python-matplotlib (optional, for plotting)

Help Message

usage: timestats [-h] [-r RUNS] [-p] <cmd|arg> [<cmd|arg> ...]

Run a command multiple times and print some basic statistics about the
runtimes.

positional arguments:
  <cmd|arg>             The command and arguments to run.

optional arguments:
  -h, --help            show this help message and exit
  -r RUNS, --runs RUNS  The number of times to run the command. Default: 10
  -p, --plot            Plot the results.

Hint: use "--" to force all following arguments to be interpretted as the
command and arguments to run.

Urxvt Quake-like Terminal

urxvtq creates a quake-like togglable urxvt terminal. The script only requires xdotool and urxvtdc. By default the terminal uses tabs. To remove these, simply remove -pe 'default,tabbed`` from theurxvtdc` command.

urxvtdc is a simple convenience script that will start up the urxvt daemon if necessary when invoking a client.

xrandroutputchangewait

Yeah, the name’s ugly, but hey, it’s descriptive.

xrandroutputchangewait is a minimalist command-line utility for use in other scripts. When run, it will wait for Xrandr output changes and then exit. You can use it in scripts to automatically configure displays and other things when monitors are connected or disconnected.

Compilation

gcc -Wall -lX11 -lXrandr -o xrandr-outputchangewait{,.c}

Example 1

Run commands when an HDMI monitor is connected or disconnected:

#!/bin/bash

# Change this to the appropriate path on your system.
STATUSPATH_='/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-HDMI-A-1/status'

while xrandroutputchangewait
do
  content_="$(< "$STATUSPATH_")"
  case "$content_" in
    connected)
      # Run xrandr or whatever here.
    ;;
    disconnected)
      # Run xrandr or whatever here.
    ;;
    *)
      echo 'error: unexpected status' >&2
    ;;
  esac
done

Example 2

Run specific commands for different HDMI monitors based on their unique EDIDs. This is useful if, for example, you have a laptop that connects to different monitors at home and at work. One way would be to configure each setup and then get the exact command using unxrandr (included in the arandr package).

edidchecksum_="$(sha256sum '/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-HDMI-A-1/edid')"
edidchecksum_="${edidchecksum_%% *}"

case "$edidchecksum_" in
  aba161abb9e52b1dcc4c66b367356d66a87610fa49764b72a97ddb0f9be10733)
    # Do stuff here if monitor A is connected.
    # xrandr --output HDMI1 --pos 0x0 ...
  ;;
  ed329858041295cbf1aca3be39874df75551f0ad1f52568550fbbff542282d13)
    # Do other stuff here if monitor B is connected.
    # xrandr --output HDMI1 --pos 1650x0 ...
  ;;
...
esac
Contact
echo xyne.archlinux.org | sed 's/\./@/'
Validation
XHTML 1.0 Strict CSS level 3 Atom 1.0