-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathldig
More file actions
executable file
·37 lines (37 loc) · 1.08 KB
/
ldig
File metadata and controls
executable file
·37 lines (37 loc) · 1.08 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
#!/bin/bash
# ldig - (local dig)
# look up a domain in a hosts file, with dig (+short) syntax
hosts=${hosts-./hosts}
if [ -z "$1" ]; then
echo "Usage: $0 [-x someip OR somedomain.com]"
exit 1
elif [ "$1" == "-x" ]; then
if [ -z "$2" ]; then
echo "Usage: $0 [-x someip OR somedomain.com]"
else
shift
for ip in $@; do
if test -r $hosts; then
grep -i "^$ip\s" "$hosts" | awk '{print $2}'
elif test -r /etc/hosts; then
echo using file /etc/hosts >&2
grep -i "^$ip\s" /etc/hosts | awk '{print $2}'
else
echo cannot find local hosts file, using system dig >&2
dig +short -x "$ip"
fi
done
fi
else
for dom in $@; do
if test -r $hosts; then
grep -i "^[^\#].*\s[www\.]*$dom\(\s\|\$\)" "$hosts" | awk '{print $1}' # regex, beautiful regex :)
elif test -r /etc/hosts; then
echo using file /etc/hosts >&2
grep -i "^[^\#].*\s[www\.]*$dom\(\s\|\$\)" /etc/hosts | awk '{print $1}'
else
echo "cannot find local hosts file, using system dig" >&2
dig +short "$dom"
fi
done
fi