From f3f4f72d9ce398e55d424cd0d4f524a6730e66ed Mon Sep 17 00:00:00 2001 From: nervuri Date: Sun, 10 Sep 2023 14:37:57 +0000 Subject: [PATCH] improve hostname cleanup --- get-hosts.sh | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/get-hosts.sh b/get-hosts.sh index 2c6a409..9bfd0e0 100755 --- a/get-hosts.sh +++ b/get-hosts.sh @@ -32,6 +32,28 @@ fi hosts="$hosts1 $hosts2" +# Prepare temporary file. +tempfile=$(mktemp) +# Delete temporary file on exit. +finish() { + rm -f "$tempfile" +} +trap finish EXIT + +echo "$hosts" | while read -r line; do + # Remove redundant ":1965" port from each line that has it. + line="${line%:1965}" + # Remove redundant "." from the end of each hostname that has it. + line="${line%.}" + # Hostname to lowercase. + line=$(echo "$line" | tr '[:upper:]' '[:lower:]') + # Add cleaned up hostnames to temporary file. + echo "$line" >> "$tempfile" +done + +# Get hosts back from temporary file. +hosts=$(cat "$tempfile") + # Remove empty lines; convert punycode to unicode; sort entries; remove duplicates. hosts=$(echo "$hosts" | awk NF | idn --allow-unassigned --idna-to-unicode | sort -fu) @@ -51,16 +73,9 @@ if [ -z "$hosts" ]; then exit 1 fi -# Save to temporary file. -tempfile=$(mktemp) +# Save to temporary file again. echo "$hosts" > "$tempfile" -# Delete temporary file on exit. -finish() { - rm -f "$tempfile" -} -trap finish EXIT - # Check connection to host. # $1: "" or "tor" # $2: host[:port]