improve hostname cleanup

This commit is contained in:
nervuri 2023-09-10 14:37:57 +00:00
parent fa6221e91a
commit f3f4f72d9c
Signed by: nervuri
GPG Key ID: C4769EEA7BA61672
1 changed files with 23 additions and 8 deletions

View File

@ -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]