This repository was archived by the owner on Feb 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathip
More file actions
265 lines (230 loc) · 8.14 KB
/
ip
File metadata and controls
265 lines (230 loc) · 8.14 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/bash
set -e
# Tool made by Lokesh Kumar
# --- Define Colors ---
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[93m'
CYAN='\033[96m'
MAGENTA='\033[95m'
BLUE='\033[94m'
RESET='\033[0m'
# --- Define Paths ---
SAVE_DIR="$HOME/IP_Scanner_Results"
INSTALL_DIR="$HOME/.ipscanner"
CONFIG_DIR="$HOME/.config/ipscanner"
CONFIG_FILE="$CONFIG_DIR/config"
ACCESS_TOKEN=""
# --- Quick dependency checks (informative only) ---
# These checks do not install anything; they only provide helpful error messages.
if ! command -v jq >/dev/null 2>&1; then
echo -e "${RED}Error: 'jq' is not installed.${RESET}"
echo -e "${YELLOW}Install it (Termux: pkg install jq, Debian/Ubuntu: sudo apt install jq)${RESET}"
exit 1
fi
if ! command -v python3 >/dev/null 2>&1; then
echo -e "${RED}Error: 'python3' is not installed.${RESET}"
echo -e "${YELLOW}Install it (Termux: pkg install python, Debian/Ubuntu: sudo apt install python3)${RESET}"
exit 1
fi
# check python requests module
if ! python3 -c "import requests" >/dev/null 2>&1; then
echo -e "${RED}Error: Python module 'requests' is not installed.${RESET}"
echo -e "${YELLOW}Install it with: pip3 install requests${RESET}"
exit 1
fi
# --- [SECURITY FIX] Load API Key ---
if [ -f "$CONFIG_FILE" ]; then
ACCESS_TOKEN=$(grep -E '^ACCESS_TOKEN=' "$CONFIG_FILE" | head -n1 | cut -d'=' -f2- | tr -d '"' | xargs)
fi
if [ -z "$ACCESS_TOKEN" ] || [ "$ACCESS_TOKEN" == "YOUR_KEY_HERE" ]; then
echo -e "${RED}Error: ipinfo.io API Token not found!${RESET}"
echo -e "${YELLOW}Please create the ${CONFIG_FILE} file and add:${RESET}"
echo -e "${CYAN}ACCESS_TOKEN=YOUR_KEY${RESET}"
exit 1
fi
# --- Function to display banner ---
display_banner() {
clear
echo -e "${CYAN}"
if command -v figlet >/dev/null 2>&1; then
figlet -f slant "IP Scanner Pro" 2>/dev/null || true
else
echo "IP Scanner Pro"
fi
echo -e "${GREEN}By Lokesh Kumar${RESET}"
echo -e "${MAGENTA}---------------------------------${RESET}"
echo -e "${YELLOW}Advanced IP Scanning Tool${RESET}"
echo -e "${MAGENTA}---------------------------------${RESET}"
}
# --- Function to fetch and display IP details ---
fetch_ip_details() {
local ip_address=$1
local result
echo -e "${YELLOW}Fetching IP details...${RESET}"
result=$(python3 - <<END
import requests, json, sys
access_token = "$ACCESS_TOKEN"
ip_address = "$ip_address"
url = f"https://ipinfo.io/{ip_address}/json?token={access_token}"
try:
response = requests.get(url, timeout=5)
response.raise_for_status()
print(json.dumps(response.json()))
except requests.exceptions.HTTPError as e:
status = getattr(e.response, "status_code", None)
if status == 404:
print(json.dumps({"error": "IP address not found or private."}))
elif status in (401, 403):
print(json.dumps({"error": "Invalid API Token. Check $CONFIG_FILE"}))
else:
print(json.dumps({"error": f"API Error: {status}"}))
except requests.RequestException:
print(json.dumps({"error": "Failed to connect. Check internet."}))
sys.exit(1)
END
)
if echo "$result" | jq -e '.error' >/dev/null 2>&1; then
error_msg=$(echo "$result" | jq -r '.error')
echo -e "${RED}Failed to fetch details: $error_msg${RESET}"
return
fi
formatted_output=$(echo "$result" | jq -r '
"IP Address: \(.ip // "N/A")",
"Hostname: \(.hostname // "N/A")",
"City: \(.city // "N/A")",
"Region: \(.region // "N/A")",
"Country: \(.country // "N/A")",
"Location: \(.loc // "N/A")",
"Organization: \(.org // "N/A")",
"Postal Code: \(.postal // "N/A")",
"Timezone: \(.timezone // "N/A")"
')
echo -e "${GREEN}--- IP Address Details ---${RESET}"
echo "$formatted_output" | while IFS= read -r line; do
echo -e "${CYAN}$line${RESET}"
done
echo -e "${GREEN}----------------------------${RESET}"
mkdir -p "$SAVE_DIR"
filename="$SAVE_DIR/${ip_address}_Report.txt"
{
echo "--- IP Address Details ---"
echo "$formatted_output"
echo "----------------------------"
echo
echo "--- Raw JSON Data ---"
echo "$result" | jq .
} > "$filename"
echo -e "${GREEN}Details saved to: ${filename}${RESET}"
}
# --- Function to update the tool ---
update_tool() {
echo -e "${BLUE}Updating tool from GitHub...${RESET}"
if [ -d "$INSTALL_DIR/.git" ]; then
cd "$INSTALL_DIR" || return
git pull origin main
echo -e "${GREEN}Update complete.${RESET}"
echo -e "${YELLOW}Applying updates...${RESET}"
if [[ $(uname -o 2>/dev/null) == "Android" ]]; then
BIN_DIR="$PREFIX/bin"
cp "ip" "$BIN_DIR/ip"
chmod +x "$BIN_DIR/ip"
else
BIN_DIR="/usr/local/bin"
if command -v sudo &>/dev/null; then
sudo cp "ip" "$BIN_DIR/ip"
sudo chmod +x "$BIN_DIR/ip"
else
cp "ip" "$BIN_DIR/ip"
chmod +x "$BIN_DIR/ip"
fi
fi
echo -e "${GREEN}Tool updated. Please restart the script.${RESET}"
else
echo -e "${RED}Installation directory $INSTALL_DIR not found.${RESET}"
echo -e "${YELLOW}Please reinstall using the installer script.${RESET}"
fi
sleep 3
}
# --- Function to remove the tool ---
remove_tool() {
echo -e "${RED}Completely removing the tool...${RESET}"
SUDO_CMD="sudo"
if [[ $(uname -o 2>/dev/null) == "Android" ]]; then
BIN_DIR="$PREFIX/bin"
SUDO_CMD=""
elif [[ -f /etc/debian_version ]] || [[ -f /etc/arch-release ]] || [[ -f /etc/fedora-release ]] || [[ $(uname -s) == "Darwin" ]]; then
BIN_DIR="/usr/local/bin"
fi
if [ -f "$BIN_DIR/ip" ]; then
$SUDO_CMD rm -f "$BIN_DIR/ip"
echo "Executable removed."
fi
if [ -d "$INSTALL_DIR" ]; then
rm -rf "$INSTALL_DIR"
echo "Repository removed."
fi
if [ -d "$CONFIG_DIR" ]; then
rm -rf "$CONFIG_DIR"
echo "Config file removed."
fi
if [ -d "$SAVE_DIR" ]; then
rm -rf "$SAVE_DIR"
echo "Scan results removed."
fi
echo -e "${GREEN}Tool removed successfully!${RESET}"
}
# --- Main Menu ---
main_menu() {
while true; do
display_banner
ipv4=$(curl -s -4 ifconfig.me || echo "N/A")
ipv6=$(curl -s -6 ifconfig.me || echo "N/A")
echo -e "${RED}Your IPv4: ${CYAN}$ipv4${RESET}"
echo -e "${RED}Your IPv6: ${CYAN}$ipv6${RESET}"
echo -e "${MAGENTA}\nMain Menu:${RESET}"
echo -e "${CYAN}1. Scan IPv4 / IPv6${RESET}"
echo -e "${CYAN}2. Update Tool${RESET}"
echo -e "${CYAN}3. Remove Tool${RESET}"
echo -e "${CYAN}4. Exit${RESET}"
read -r -p "${YELLOW}Enter your choice: ${RESET}" choice
case $choice in
1)
read -r -p "${YELLOW}Enter the IP address (v4 or v6): ${RESET}" ip_address
# Validate input safely (ShellCheck clean)
# Reject these unsafe characters: ; | & $ ( ) `
# Using printf avoids locale/echo differences and the single-quoted regex prevents accidental expansion.
if printf '%s' "$ip_address" | grep -qE '[;|&$()`]' ; then
echo -e "${RED}Invalid characters in IP address.${RESET}"
sleep 2
continue
fi
if [[ -z "$ip_address" ]]; then
echo -e "${RED}IP address cannot be empty.${RESET}"
sleep 1
continue
fi
fetch_ip_details "$ip_address"
;;
2)
update_tool
;;
3)
remove_tool
exit 0
;;
4)
echo -e "${RED}Exiting...${RESET}"
exit 0
;;
*)
echo -e "${RED}Invalid choice, please try again.${RESET}"
sleep 1
;;
esac
echo -e "\n${YELLOW}Press Enter to continue...${RESET}"
read -r
done
}
# Run the script
main_menu