a picture my son drew of a wave on a beach

Spellbook of Terminal ­Commands

rbn
tagged with: #tech #bash #script #code

Organising my thoughts is a skill I may never acquire. It gets worse as I get older. Thankfully the world is populated by wonderful beings who share their discoveries. Thank you @Rek for sharing your work. This Spellbook of Terminal Commands is something I wish I had started 40 years ago. Ah well.

I will be adding/editing this list as/when I find old text notes to myself I have scattered across my distributed electronic memory. It’s a work in progress.


Keep wifi alive

debian/bash/networking/wlan
I ran out of ethernet ports for a little raspberrypi server I run under the telly. I run this script via cron to ensure it stays on the network. I can’t remember right now what this server does…

Replace the bits inside the [SQUARE BRACKETS], obviously.


#!/bin/bash

# keep wifi alive
# sudo chmod 711 wlan0keepalive.sh
# */5 * * * * /home/wlan0keepalive.sh >/dev/null 2>&1

ping -c2 [IP of upstream router or similar]

if [ $? != 0 ] 
then 
  echo "restarting wlan0"
  ifconfig wlan0 down
  sleep 30
  ifconfig wlan0 up
else
  echo "hunkydory"
fi

Certbot/Lighttpd SSL

debian/bash/certbot/lighttpd/ssl
I use a script like the one below to check whether to update my SSL certificate at 6:30 every morning.
This is the cron:

30 6 * * * /[PATH]/sslcat.sh > /dev/null

I am sure I am missing something as my SSL regularly fails. Looking at my domain report on Googles’ Certificate Transparency Project often confuses me. I had forgotten about enabling Fastmails webDAV at fm.tregeagle.com.

I have been using certbot to keep tregeagle.com updated but not sure where fm.tregeagle.com gets updated. Did I set that up.

Replace the bits inside the [SQUARE BRACKETS], obviously.


#!/usr/bin/env bash
set -o errexit

# run via cron as root

certbot certonly -n --webroot --expand -w /var/www/[SITE] -d [www.URI.com] --pre-hook "systemctl stop lighttpd.service" --posthook "systemctl start lighttpd.service"

echo "success"

Dynamic DNS

debian/bash/dns/linode/api
I update my own DNS rather than use one of those services. My DNS records are hosted on Linode and they have a well documented API that I use. My script looks like this:


#!/bin/bash
set -Eue -o pipefail

TOKEN=[My Token]
DOMAINID=[My Domain ID]
RECORDID=[The Record ID]

src=$HOME"/bin/wanip.txt"
IPCURRENT=$(cat "${src}")

curl --request PUT \
     --url https://api.linode.com/v4/domains/${DOMAINID}/records/${RECORDID} \
     --header 'accept: application/json' \
     --header "authorization: Bearer ${TOKEN}" \
     --header 'content-type: application/json' \
     --data '
{
  "name": "data",
  "ttl_sec": 300,
  "target": "${IPCURRENT}"
}
'

Public IP checker/updater thing

debian/bash/dns/dig
As you can see in the above script it relies on comparing a record of the existing IP (IPCURRENT) with the historical one (src). To keep that updated I use the following script.


#!/bin/bash

# add this to a cronjob
# depends upon having dnsutils or dig installed.
# uses opendns to resolve external address [see ipcurrent]
# for alt methods see: https://www.cyberciti.biz/faq/how-to-find-my-public-ip-a>


# Check the source file exists
src=$HOME"/bin/wanip.txt"
ipserv="[USER]@[SERVER]:/var/www/[PATH]/ip.txt"
if [ ! -f "${src}" ]
then # if not create it
  echo "null" > "${src}"
fi

# Compare the old ip address in the source file to the current WAN ip address
ipold=$(cat "${src}")
ipcurrent="$(dig +short myip.opendns.com @resolver1.opendns.com)"

if [ "${ipcurrent}" != "${ipold}" ]; then
 echo "Old IP was \"${ipold}\", new IP is ${ipcurrent}"
 echo "${ipcurrent}" > "${src}" # update the source file
 scp "${src}" "${ipserv}"
else
 echo "external ip is: ${ipcurrent}"
 echo "on last change ip addr was copied to remote server:"
 echo "${src}" "${ipserv}"
 echo "no changes required - exiting not exciting."
fi

exit 0

* Actually, writing this Spellbook post has been very unwieldy. My patience is limited. Writing one post for each script and tagging it ‘code’ is better for me. the index/TOC is handy. I like the idea of introducing the code and having a way to find the scipt I’m after. Perhaps I just need a top tagged post for intros.

Read or add a comment.
permalink

Page 1 / 2 »