forked from DouglasFreshHabian/Bash_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLookup.sh
More file actions
161 lines (128 loc) · 5.94 KB
/
Copy pathLookup.sh
File metadata and controls
161 lines (128 loc) · 5.94 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
#!/bin/bash
# A simple Bash script that accepts a log file as input and extracts the ip address, sorts
# them, getting rid of any duplicates and runs geoiplookup and whois on each address printing
# the results to the screen and saving the results to a file...
# Define color codes
RESET='\033[0m'
BOLD='\033[1m'
YELLOW='\033[33m'
CYAN='\033[36m'
RED='\033[31m'
MAGENTA='\033[35m'
# Check if the input file is provided
if [ -z "$1" ]; then
echo -e "${RED}Usage: $0 <log_file>${RESET}"
exit 1
fi
# Check if the file exists
if [ ! -f "$1" ]; then
echo -e "${RED}File not found!${RESET}"
exit 1
fi
# Store the input file
log_file="$1"
# Extract the filename without extension and path
file_name=$(basename "$log_file" | sed 's/\(.*\)\..*/\1/')
# Create a timestamp
timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
# Create the output file name
output_file="${file_name}_results_${timestamp}.txt"
# Print the initial message to indicate what file is being processed
echo -e "${CYAN}Searching $log_file For Any IP Addresses...${RESET}"
# Check if geoiplookup is installed
if ! command -v geoiplookup &> /dev/null; then
echo -e "${RED}Error: geoiplookup is not installed.${RESET}"
echo -e "${YELLOW}To install geoiplookup, run the following command:${RESET}"
echo -e "${YELLOW}sudo apt install geoip-bin${RESET}"
exit 1
fi
# Extract IP addresses from the log file using regex, remove duplicates, and sort
ip_addresses=$(grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' "$log_file" | sort -u)
# Check if we found any IP addresses
if [ -z "$ip_addresses" ]; then
echo -e "${RED}No IP addresses found in the log file.${RESET}"
exit 1
fi
# Count the number of IP addresses found
ip_count=$(echo "$ip_addresses" | wc -l)
# Create or clear the Results file
> "$output_file"
# Print the list of IP addresses found and their count
echo -e "${CYAN}==========================================${RESET}"
echo -e "${BOLD}${GREEN}List Of IP Addresses Found (${ip_count}):${RESET}"
echo "$ip_addresses"
echo -e "${CYAN}==========================================${RESET}"
# Append the list to Results.txt
echo -e "${CYAN}==========================================${RESET}" >> "$output_file"
echo -e "${BOLD}${GREEN}List Of IP Addresses Found (${ip_count}):${RESET}" >> "$output_file"
echo "$ip_addresses" >> "$output_file"
echo -e "${CYAN}==========================================${RESET}" >> "$output_file"
# Check for internet connectivity
echo -e "${CYAN}Checking For Internet...${RESET}"
ping -c 2 1.1.1.1 &> /dev/null
# If the ping fails, print error and exit
if [ $? -ne 0 ]; then
echo -e "${RED}You Are Not Connected To The Internet${RESET}"
echo -e "${RED}You Are Not Connected To The Internet${RESET}" >> "$output_file"
exit 1
else
echo -e "${GREEN}You Are Connected To The Internet${RESET}"
fi
# Define the filter patterns for grep
filter_patterns="(OrgAbuseRef|OrgAbuseEmail|#|RTechRef|OrgTechRef|RefRTechEmail|RTechPhone|RTechName|RTechHandle|OrgAbusePhone|OrgAbuseName|OrgAbuseHandle|Ref|RTechEmail|Comment|OrgRoutingRef|OrgRoutingEmail|OrgRoutingPhone|OrgRoutingName|OrgRoutingHandle|OrgNOCRef|OrgNOCEmail|OrgNOCPhone|OrgNOCName|OrgNOCHandle|OrgTechEmail|OrgTechPhone|OrgTechName|OrgTechHandle|Comment)"
# Variable to store IP addresses that return no info
no_info_ips=""
# Process each IP address found in the log file
while IFS= read -r ip_address; do
# Run geoiplookup and whois for the IP address
geoip_result=$(geoiplookup "$ip_address" 2>/dev/null)
whois_result=$(whois "$ip_address" 2>/dev/null)
# Check if either geoiplookup or whois gives the result "IP Address not found"
if [[ "$geoip_result" == *"IP Address not found"* || "$whois_result" == *"IP Address not found"* ]]; then
no_info_ips="$no_info_ips\n${RED}IP Address $ip_address Returns No Info. Perhaps It Is An Internal IP Address?${RESET}"
continue
fi
# Skip if no results from geoiplookup or whois
if [ -z "$geoip_result" ] && [ -z "$whois_result" ]; then
continue
fi
# Output to the screen with colors
echo -e "${CYAN}==========================================${RESET}"
echo -e "${BOLD}${GREEN}Results for IP: $ip_address${RESET}"
# Output geoiplookup result (filtered and colorized)
if [ -n "$geoip_result" ]; then
echo -e "${MAGENTA}GeoIP Lookup:${RESET}"
echo "$geoip_result" | grep -vwE "$filter_patterns" | sed "s/^/${GREEN}/"
fi
# Output whois result (filtered and colorized)
if [ -n "$whois_result" ]; then
echo -e "${MAGENTA}Whois Lookup:${RESET}"
echo "$whois_result" | grep -vwE "$filter_patterns" | sed "s/^/${GREEN}/"
fi
# Append results to Results.txt (filtered)
echo -e "${CYAN}==========================================${RESET}" >> "$output_file"
echo -e "${BOLD}${GREEN}Results for IP: $ip_address${RESET}" >> "$output_file"
if [ -n "$geoip_result" ]; then
echo -e "${MAGENTA}GeoIP Lookup:${RESET}" >> "$output_file"
echo "$geoip_result" | grep -vwE "$filter_patterns" >> "$output_file"
fi
if [ -n "$whois_result" ]; then
echo -e "${MAGENTA}Whois Lookup:${RESET}" >> "$output_file"
echo "$whois_result" | grep -vwE "$filter_patterns" >> "$output_file"
fi
echo -e "${CYAN}==========================================${RESET}" >> "$output_file"
echo >> "$output_file"
done <<< "$ip_addresses"
# Print the "no info" IP addresses at the end of the results
if [ -n "$no_info_ips" ]; then
echo -e "${CYAN}\n==========================================${RESET}"
echo -e "${BOLD}${RED}IP Addresses That Returned No Info:${RESET}"
echo -e "$no_info_ips"
echo -e "${CYAN}==========================================${RESET}"
echo -e "${CYAN}\n==========================================${RESET}" >> "$output_file"
echo -e "${BOLD}${RED}IP Addresses That Returned No Info:${RESET}" >> "$output_file"
echo -e "$no_info_ips" >> "$output_file"
echo -e "${CYAN}==========================================${RESET}" >> "$output_file"
fi
# Notify user where the results have been saved
echo -e "${YELLOW}Results saved to $output_file${RESET}"