Skip to main content

CLI Tricks: reboot/shutdown mollyguard function

Over on Twitter I was having a discussion about creating a mollyguard to protect yourself from accidentally rebooting the wrong machine by requiring you to type the hostname of the machine you wanted to reboot. If you need such, you can add these functions to your ~/.bashrc or ~/.profile to create such a reboot function. Note: The function shown on Twitter had the logic reversed so make sure you get the "=" and "!=" correct. If $1 equals the hostname, you want to proceed with the shutdown; if they are not equal you want a warning.

reboot() { [ "x$1" = "x$(hostname)" ] && sudo /sbin/shutdown -r now || echo "Failed to specify $(hostname)" ; }
halt() { [ "x$1" = "x$(hostname)" ] && sudo /sbin/shutdown -p now || echo "Failed to specify $(hostname)" ; }

If you run on OpenBSD you can specify doas instead of sudo.

CLI Tricks: Find the Nth word of a text file

While I'm not sure when others would use this I wanted to find the Nth word of a text file so I used this to find the, say, 318th word of the document:

tr -cs "a-zA-Z'" '\012' < document.txt | sed -n '318{p;q;}'

Yes, it can get tripped up by non-Latin characters, hyphenated words, or other edge-cases, but for my 7-bit ASCII input text, it did the job.

CLI Tricks: diff files in git and then add them

Occasionally I want to diff one or more files in git and, if everything looks good, then add them. To do so, I often take advantage of bash's ^ notation to do a replacement:

git diff -- file1.txt file2.txt
^diff^add
git commit -m "Looks good to me"

The second command replaces diff with add turning the command into git add -- file1.txt file2.txt without having to retype the entire command.

CLI Tricks: Compare directory sizes when hard-linking

I have directories with lots of image files (or mail files), some of which I hard-link into other directories to save space or make sure they're in sync. How much space am I saving by hard-linking?

(du -sl ; printf '-\n' ; du -s) | awk '{print $1}' | paste -s - | bc

With a little math, most of it can happen in one awk statement:

(du -sl ; du -s) | awk '{t+=(-NR*2+3)*$1} END{print t}'

Aggressive pf(4) configuration for SSH protection

If some unknown person came up to your house and started trying to open each window, jiggling each door handle, and punching in random codes on your garage-door opener, you could safely assume that they sought trouble and that you should stop them. Similarly, if some unknown computer on the internet started probing at ports on your server where you offer no services, it should set off alarm-bells and you should prevent this miscreant from interacting with your server. Especially SSH access. You know what services your machine provides. If anybody attempts to connect to some other service on your machine, in all likelihood they don't have your best interests in mind.

In this post, we configure pf on OpenBSD to catch inappropriate connection-attempts and block the offender.

Read more…