commit e8c41d182c1833dd6a392afbd11d5c38500d1bec
parent a8a839bf132d328df1286a05265ada51e423ae13
Author: Matthew Carlson <matt@mattcarlson.org>
Date: Sun, 2 Jun 2024 20:27:05 -0400
added a script to automatically configure dns and added a script to generate random strings/phrases
Diffstat:
2 files changed, 171 insertions(+), 0 deletions(-)
diff --git a/.local/bin/dns.sh b/.local/bin/dns.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+#
+# dns
+
+# system copy of /etc/resolv.conf
+RESOLV_SYS='/etc/resolv.conf'
+# nm's copy of resolv.conf
+RESOLV_NM='/var/run/NetworkManager/resolv.conf'
+
+# check to see whether ethernet interface is up
+eth_is_up() {
+ # show active interfaces via nmcli and grep for the one we want
+ nmcli -t -f DEVICE connection show --active | grep "${1}" >/dev/null
+
+ #shellcheck disable=SC2046
+ return $(get_interface_status "${?}")
+}
+
+get_interface_status() {
+ status="${1}"
+ # interface is up
+ if [ "${status}" = '0' ]; then
+ return 0
+ # interface is down
+ else
+ return 1
+ fi
+}
+
+# get wlan interface name
+get_wlan_interface() {
+ # loop over interfaces in /sys/class/net
+ for interface in '/sys/class/net/'*; do
+ # wireless interfaces will have a subfolder called 'wireless'
+ # if carrier = 1, then the interface is up
+ if [ -d "${interface}/wireless/" ] && [ "$(cat "${interface}/carrier")" = '1' ]; then
+ printf '%s\n' "${interface##*/}"
+ fi
+ done
+
+ return 0
+}
+
+# check to see whether wifi is up
+wifi_is_up() {
+ # get ssid of wlan via iw
+ ssid="$(iw dev "${1}" info 2>/dev/null | grep -i '\<ssid\>' | awk '{ print $2 }')"
+
+ # if no ssid, then the wifi is down and /etc/resolv.conf may be blank
+ if [ -z "${ssid}" ]; then
+ return 1
+ else
+ return 0
+ fi
+}
+
+main() {
+ # this script requires iwd and nm
+ if ! command -v iw >/dev/null && ! command -v nmcli >/dev/null; then
+ printf '%s\n' 'iw and NetworkManager are required to run this script.'
+ return 1
+ fi
+
+ # get name of eth interface
+ eth_interface="$(basename '/sys/class/net/eth'*)"
+ # get name of wlan interface
+ wlan_interface="$(get_wlan_interface)"
+
+ # automatically get nameservers if wifi goes down but ethernet remains
+ while :; do
+ # if eth interface is up, then check for nameservers
+ if eth_is_up "${eth_interface}"; then
+ grep 'nameserver' "${RESOLV_SYS}" >/dev/null
+ has_dns="${?}"
+
+ # if there are nameservers, then the wifi interface must be up (or the ethernet interface was manually enabled)
+ # do nothing
+ if [ "${has_dns}" = '0' ]; then
+ :
+ # if there are no nameservers, then replace /etc/resolv.conf with nm's resolv.conf
+ else
+ cp "${RESOLV_NM}" "${RESOLV_SYS}"
+ fi
+ # if eth interface is down, then it could've been disabled manually
+ # if wifi is still up, then add the nameserver(s) from dhcpcd's resolv.conf
+ else
+ # if wifi is up, then check for nameserver(s)
+ if wifi_is_up "${wlan_interface}"; then
+ grep 'nameserver' "${RESOLV_SYS}" >/dev/null
+ has_dns="${?}"
+
+ # if there is a nameserver or if there are nameservers, then do nothing
+ if [ "${has_dns}" = '0' ]; then
+ :
+ # if there aren't any nameservers, then copy dhcpcd's resolv.conf to /etc/resolv.conf
+ else
+ # dhcpcd's resolv.conf contents for the wlan interface should be located here
+ resolv_dhcpcd="/var/run/dhcpcd/hook-state/resolv.conf/${wlan_interface}.dhcp"
+ # if dhcpcd does have a resolv.conf, then proceed
+ if [ -e "${resolv_dhcpcd}" ]; then
+ cp "${resolv_dhcpcd}" "${RESOLV_SYS}"
+ else
+ :
+ fi
+ fi
+ else
+ :
+ fi
+ fi
+ sleep 10
+ done
+
+ return 0
+}
+
+main "${@}"
diff --git a/.local/bin/gen_string.sh b/.local/bin/gen_string.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+#
+# script
+
+DICT='/usr/share/dict/british-english'
+LENGTH_ERROR='String length must be a number between 1 and 64.'
+
+is_integer() {
+ case "${1}" in
+ (*[!0123456789]*) return 1 ;;
+ ('') return 1 ;;
+ (*) return 0 ;;
+ esac
+}
+
+main() {
+ while :; do
+ # get string type
+ printf '%s\n' 'Type of string (0 = random, 1 = phrase)?: '
+ read -r string_type
+ # incorrect input
+ { [ ! "${string_type}" = '0' ] && [ ! "${string_type}" = '1' ] ; } && printf '%s\n' 'String type must be a number between 0 and 1.' && continue
+ break
+ done
+
+ # get string length
+ while :; do
+ printf '%s\n' "If you're generating a phrase, it's recommended to use a middling length, because it's very time consuming or even impossible to generate a phrase if the length is very low or very high."
+ printf '%s\n' 'Length of string (1-64)?: '
+ read -r length
+ # test whether input is number
+ ! is_integer "${length}" && printf '%s\n' "${LENGTH_ERROR}" && continue
+ # test whether input is within range
+ { [ "${length}" -lt 1 ] || [ "${length}" -gt 64 ] ; } && printf '%s\n' "${LENGTH_ERROR}" && continue
+ break
+ done
+
+ # generate string
+ string=''
+ # generate random string via /dev/urandom and tr
+ if [ "${string_type}" = '0' ]; then
+ string="$(</dev/urandom tr -dc 'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' | head -c "${length}"; printf '\n')"
+ printf '%s\n' "${string}"
+ # generate random phrase by pulling words from dictionary
+ elif [ "${string_type}" = '1' ]; then
+ while :; do
+ string="$(grep -v "'s\|[[:upper:]]" "${DICT}" | shuf -n 4 | tr -d '\n')"
+ [ ${#string} -eq "${length}" ] && printf '%s\n' "${string}" && break
+ done
+ fi
+
+ return 0
+}
+
+main "${@}"