-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomains2ips.sh
More file actions
executable file
·161 lines (134 loc) · 3.55 KB
/
domains2ips.sh
File metadata and controls
executable file
·161 lines (134 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env bash
# Read domains from file and write the resolved addresses to a file for IPv4 / IPv6.
SCRIPT_DIR=$(dirname "$(realpath "$0")")
SCRIPTNAME=$(basename "$0" | cut -d'.' -f1)
DOMAIN_PATTERN='^[a-z0-9-]+(\.[a-z0-9-]+)+$'
# If LOG_FILE is set in parent script
if [ ! -v LOG_FILE ]; then
LOG_FILE="$SCRIPT_DIR/$SCRIPTNAME".log
echo "INFO" "LOG_FILE is set to $LOG_FILE"
fi
function file_ends_with_newline() {
[[ $(tail -c1 "$1" | wc -l) -gt 0 ]]
}
usage() {
echo "Usage: $0"
echo "-i, --input <file> Read file with domains, separated by newline"
echo "-h, --help Print usage"
echo "-o, --output <file> Output file with addresses, separated by newline"
echo "-6 Optional parameter: Resolve domains to IPv6 (default: IPv4)."
echo "-v, --verbose Verbose output"
}
log() {
local LEVEL="$1"
shift
local MESSAGE="$*"
local TIMESTAMP
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
echo "$TIMESTAMP [$LEVEL] $MESSAGE" >> "$LOG_FILE"
}
declare -a domains
record=A
ip_pattern='^[0-9.]+$'
# Check if no parameters were passed
if [ $# -eq 0 ]; then
echo "Error: No arguments provided."
usage
exit 1
fi
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-i | --input)
if [[ -z "$2" || "$2" == --* ]]; then
echo "Error: -f, --file requires a value."
exit 1
elif [ ! -f "$2" ]; then
echo "ERROR" "Input file $2 does not exist" | tee log
exit 1
fi
in_file="$2"
shift 2
;;
-6)
record=AAAA
ip_pattern='^[0-9a-fA-F:]+$'
shift 1
;;
-o | --output)
out_file="$2"
shift 2
;;
-v | --verbose)
verbose=true
shift 1
;;
-h | --help)
usage
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use -h, --help for usage."
exit 1
;;
esac
done
if [[ -z $out_file ]]; then
out_file=$(basename "$in_file" | cut -d'.' -f1).out
fi
log "INFO" "Output file: $out_file"
log "INFO" "Input file: $in_file"
if $verbose; then
echo "DEBUG" "LOGFILE of $SCRIPTNAME: $LOG_FILE"
log "DEBUG" "Record: $record"
fi
log "INFO" "--- Clear output file ---"
echo "" > "$out_file"
# If the file does not end with a newline, then the last line will be ignored in the while loop.
if ! file_ends_with_newline "$in_file"
then
log "INFO" "Append newline in $in_file"
echo "" >> "$in_file"
fi
log "INFO" "--- Read domains from $in_file ---"
while IFS= read -r line; do
((i++))
if [ -z "$line" ]; then
log "INFO" "Line $i is empty and will be ignored"
elif [[ ! "$line" =~ $DOMAIN_PATTERN ]]; then
log "INFO" "Entry $line in line $i is invalid and will be ignored"
else
log "INFO" "Add $line in array"
domains+=("$line")
fi
done < "$in_file"
log "INFO" "--- Resolve domains ---"
for domain in "${domains[@]}";
do
if $verbose; then
log "DEBUG" "Domain: $domain"
fi
resolved=false
ips=$(dig +short $record "$domain" | grep -Eo "$ip_pattern")
if $verbose; then
log "DEBUG" "Resolved IP addresses: $ips"
fi
log "INFO" "--- Add resolved addresses of $domain in $out_file ---"
if [ -n "$ips" ]; then
while IFS= read -r ip; do
if $verbose; then
log "DEBUG" "$domain -> $ip"
fi
echo "$ip" >> "$out_file"
done <<< "$ips"
resolved=true
fi
if [ "$resolved" = false ]; then
log "INFO" "Resolving $record for $domain returned no addresses"
fi
done
# Remove empty line
sed -i '/^$/d' "$out_file"
# Sort and remove duplicates
sort -u -o "$out_file" "$out_file"