-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_dns_info.c.1m.py
executable file
·362 lines (330 loc) · 11.1 KB
/
ip_dns_info.c.1m.py
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python3
__author__ = "Venkata Kolagotla"
__created__ = "2023-08-24 19:16"
__last_updated__ = "2023-09-17 21:22"
__copyright__ = "Copyright 2023"
__credits__ = ["Venkata Kolagotla"]
__license__ = "Apache License, Version 2.0"
__version__ = "0.2.1"
__maintainer__ = "Venkata Kolagotla"
__email__ = "vkolagotla@pm.me"
__status__ = "Development"
__description__ = "Get external IP address and details"
__usage__ = "Place the script at ~/.config/argos/ with name extip.1m.py"
import datetime
import requests
import subprocess
from typing import Dict, Optional
import pytz
# Dictionary of all country codes and respective flag emojis
country_flags: Dict[str, str] = {
"AF": "🇦🇫", # Afghanistan
"AL": "🇦🇱", # Albania
"DZ": "🇩🇿", # Algeria
"AD": "🇦🇩", # Andorra
"AO": "🇦🇴", # Angola
"AG": "🇦🇬", # Antigua and Barbuda
"AR": "🇦🇷", # Argentina
"AM": "🇦🇲", # Armenia
"AU": "🇦🇺", # Australia
"AT": "🇦🇹", # Austria
"AZ": "🇦🇿", # Azerbaijan
"BS": "🇧🇸", # Bahamas
"BH": "🇧🇭", # Bahrain
"BD": "🇧🇩", # Bangladesh
"BB": "🇧🇧", # Barbados
"BY": "🇧🇾", # Belarus
"BE": "🇧🇪", # Belgium
"BZ": "🇧🇿", # Belize
"BJ": "🇧🇯", # Benin
"BT": "🇧🇹", # Bhutan
"BO": "🇧🇴", # Bolivia
"BA": "🇧🇦", # Bosnia and Herzegovina
"BW": "🇧🇼", # Botswana
"BR": "🇧🇷", # Brazil
"BN": "🇧🇳", # Brunei
"BG": "🇧🇬", # Bulgaria
"BF": "🇧🇫", # Burkina Faso
"BI": "🇧🇮", # Burundi
"KH": "🇰🇭", # Cambodia
"CM": "🇨🇲", # Cameroon
"CA": "🇨🇦", # Canada
"CV": "🇨🇻", # Cape Verde
"CF": "🇨🇫", # Central African Republic
"TD": "🇹🇩", # Chad
"CL": "🇨🇱", # Chile
"CN": "🇨🇳", # China
"CO": "🇨🇴", # Colombia
"KM": "🇰🇲", # Comoros
"CG": "🇨🇬", # Congo
"CD": "🇨🇩", # Democratic Republic of Congo
"CR": "🇨🇷", # Costa Rica
"HR": "🇭🇷", # Croatia
"CU": "🇨🇺", # Cuba
"CY": "🇨🇾", # Cyprus
"CZ": "🇨🇿", # Czech Republic
"CI": "🇨🇮", # Côte d'Ivoire
"DK": "🇩🇰", # Denmark
"DJ": "🇩🇯", # Djibouti
"DM": "🇩🇲", # Dominica
"DO": "🇩🇴", # Dominican Republic
"EC": "🇪🇨", # Ecuador
"EG": "🇪🇬", # Egypt
"SV": "🇸🇻", # El Salvador
"GQ": "🇬🇶", # Equatorial Guinea
"ER": "🇪🇷", # Eritrea
"EE": "🇪🇪", # Estonia
"ET": "🇪🇹", # Ethiopia
"FJ": "🇫🇯", # Fiji
"FI": "🇫🇮", # Finland
"FR": "🇫🇷", # France
"GA": "🇬🇦", # Gabon
"GM": "🇬🇲", # Gambia
"GE": "🇬🇪", # Georgia
"DE": "🇩🇪", # Germany
"GH": "🇬🇭", # Ghana
"GR": "🇬🇷", # Greece
"GD": "🇬🇩", # Grenada
"GT": "🇬🇹", # Guatemala
"GN": "🇬🇳", # Guinea
"GW": "🇬🇼", # Guinea-Bissau
"GY": "🇬🇾", # Guyana
"HT": "🇭🇹", # Haiti
"HN": "🇭🇳", # Honduras
"HU": "🇭🇺", # Hungary
"IS": "🇮🇸", # Iceland
"IN": "🇮🇳", # India
"ID": "🇮🇩", # Indonesia
"IR": "🇮🇷", # Iran
"IQ": "🇮🇶", # Iraq
"IE": "🇮🇪", # Ireland
"IL": "🇮🇱", # Israel
"IT": "🇮🇹", # Italy
"JM": "🇯🇲", # Jamaica
"JP": "🇯🇵", # Japan
"JO": "🇯🇴", # Jordan
"KZ": "🇰🇿", # Kazakhstan
"KE": "🇰🇪", # Kenya
"KI": "🇰🇮", # Kiribati
"KP": "🇰🇵", # North Korea
"KR": "🇰🇷", # South Korea
"KW": "🇰🇼", # Kuwait
"KG": "🇰🇬", # Kyrgyzstan
"LA": "🇱🇦", # Laos
"LV": "🇱🇻", # Latvia
"LB": "🇱🇧", # Lebanon
"LS": "🇱🇸", # Lesotho
"LR": "🇱🇷", # Liberia
"LY": "🇱🇾", # Libya
"LI": "🇱🇮", # Liechtenstein
"LT": "🇱🇹", # Lithuania
"LU": "🇱🇺", # Luxembourg
"MG": "🇲🇬", # Madagascar
"MW": "🇲🇼", # Malawi
"MY": "🇲🇾", # Malaysia
"MV": "🇲🇻", # Maldives
"ML": "🇲🇱", # Mali
"MT": "🇲🇹", # Malta
"MH": "🇲🇭", # Marshall Islands
"MR": "🇲🇷", # Mauritania
"MU": "🇲🇺", # Mauritius
"MX": "🇲🇽", # Mexico
"FM": "🇫🇲", # Micronesia
"MD": "🇲🇩", # Moldova
"MC": "🇲🇨", # Monaco
"MN": "🇲🇳", # Mongolia
"ME": "🇲🇪", # Montenegro
"MA": "🇲🇦", # Morocco
"MZ": "🇲🇿", # Mozambique
"MM": "🇲🇲", # Myanmar
"NA": "🇳🇦", # Namibia
"NR": "🇳🇷", # Nauru
"NP": "🇳🇵", # Nepal
"NL": "🇳🇱", # Netherlands
"NZ": "🇳🇿", # New Zealand
"NI": "🇳🇮", # Nicaragua
"NE": "🇳🇪", # Niger
"NG": "🇳🇬", # Nigeria
"NO": "🇳🇴", # Norway
"OM": "🇴🇲", # Oman
"PK": "🇵🇰", # Pakistan
"PW": "🇵🇼", # Palau
"PS": "🇵🇸", # Palestine
"PA": "🇵🇦", # Panama
"PG": "🇵🇬", # Papua New Guinea
"PY": "🇵🇾", # Paraguay
"PE": "🇵🇪", # Peru
"PH": "🇵🇭", # Philippines
"PL": "🇵🇱", # Poland
"PT": "🇵🇹", # Portugal
"QA": "🇶🇦", # Qatar
"RO": "🇷🇴", # Romania
"RU": "🇷🇺", # Russia
"RW": "🇷🇼", # Rwanda
"KN": "🇰🇳", # Saint Kitts and Nevis
"LC": "🇱🇨", # Saint Lucia
"VC": "🇻🇨", # Saint Vincent and the Grenadines
"WS": "🇼🇸", # Samoa
"SM": "🇸🇲", # San Marino
"ST": "🇸🇹", # Sao Tome and Principe
"SA": "🇸🇦", # Saudi Arabia
"SN": "🇸🇳", # Senegal
"RS": "🇷🇸", # Serbia
"SC": "🇸🇨", # Seychelles
"SL": "🇸🇱", # Sierra Leone
"SG": "🇸🇬", # Singapore
"SK": "🇸🇰", # Slovakia
"SI": "🇸🇮", # Slovenia
"SB": "🇸🇧", # Solomon Islands
"SO": "🇸🇴", # Somalia
"ZA": "🇿🇦", # South Africa
"SS": "🇸🇸", # South Sudan
"ES": "🇪🇸", # Spain
"LK": "🇱🇰", # Sri Lanka
"SD": "🇸🇩", # Sudan
"SR": "🇸🇷", # Suriname
"SZ": "🇸🇿", # Eswatini
"SE": "🇸🇪", # Sweden
"CH": "🇨🇭", # Switzerland
"SY": "🇸🇾", # Syria
"TW": "🇹🇼", # Taiwan
"TJ": "🇹🇯", # Tajikistan
"TZ": "🇹🇿", # Tanzania
"TH": "🇹🇭", # Thailand
"TL": "🇹🇱", # Timor-Leste
"TG": "🇹🇬", # Togo
"TO": "🇹🇴", # Tonga
"TT": "🇹🇹", # Trinidad and Tobago
"TN": "🇹🇳", # Tunisia
"TR": "🇹🇷", # Turkey
"TM": "🇹🇲", # Turkmenistan
"TV": "🇹🇻", # Tuvalu
"UG": "🇺🇬", # Uganda
"UA": "🇺🇦", # Ukraine
"AE": "🇦🇪", # United Arab Emirates
"GB": "🇬🇧", # United Kingdom
"US": "🇺🇸", # United States
"UY": "🇺🇾", # Uruguay
"UZ": "🇺🇿", # Uzbekistan
"VU": "🇻🇺", # Vanuatu
"VA": "🇻🇦", # Vatican City
"VE": "🇻🇪", # Venezuela
"VN": "🇻🇳", # Vietnam
"YE": "🇾🇪", # Yemen
"ZM": "🇿🇲", # Zambia
"ZW": "🇿🇼", # Zimbabwe
}
def get_public_ip() -> str:
"""Get public IP address.
Parameters
----------
None
Returns
-------
str: Public IP address as a string.
"""
response = requests.get("https://api64.ipify.org?format=json")
data = response.json()
return data["ip"]
def get_ip_details(ip: str) -> Optional[dict]:
"""Get IP details.
Parameters
----------
ip: str
IP address as a string.
Returns
-------
dict: IP details as a dictionary.
"""
response = requests.get(f"http://ip-api.com/json/{ip}")
data = response.json()
return data
def is_vpn_active() -> bool:
"""Check if VPN is active.
Parameters
----------
None
Returns
-------
bool: True if VPN is active, False otherwise.
"""
try:
output = subprocess.check_output(
["nmcli", "-t", "-f", "TYPE,DEVICE", "con"]
).decode("utf-8")
return any(line.startswith("vpn:") for line in output.splitlines())
except subprocess.CalledProcessError:
return False
def get_current_time(timezone: str) -> str:
"""Get current time based on timezone.
Parameters
----------
timezone: str
Timezone as a string.
Returns
-------
str: Current time as a string.
"""
tz = pytz.timezone(timezone)
current_time = datetime.datetime.now(tz)
return current_time.strftime("%b %d, %H:%M")
def main() -> None:
"""Main function."""
# Get public IP address
ip_address: str = get_public_ip()
# Check if the IP details are available
ip_details = get_ip_details(ip_address)
if ip_details is None:
print("Waiting for IP...")
return
country_code: str = ip_details.get("countryCode", "")
city: str = ip_details.get("city", "")
timezone: str = ip_details.get("timezone", "")
current_time: str = get_current_time(timezone)
lat: float = ip_details.get("lat", "")
lon: float = ip_details.get("lon", "")
zip_code: str = ip_details.get("zip", "")
region: str = ip_details.get("regionName", "")
country: str = ip_details.get("country", "")
isp: str = ip_details.get("isp", "")
mobile: bool = ip_details.get("mobile", False)
proxy: bool = ip_details.get("proxy", False)
# Get flag emoji from dictionary
flag_emoji: str = country_flags.get(country_code, "🌐")
# Check if VPN is active
vpn_active: bool = is_vpn_active()
if vpn_active:
vpn_status: str = "\033[32m ON🔒" # Green color for locked emoji
else:
vpn_status = "\033[31m OFF🔓" # Red color for unlocked emoji
# Reset color after the emoji
reset_color: str = "\033[0m"
# Color for country code and city
if vpn_active:
country_code_color: str = "\033[32m" # Green
city_color: str = "\033[32m" # Green
else:
country_code_color = "\033[31m" # Red
city_color = "\033[31m" # Red
# Output for top bar
print(f"{flag_emoji}({city_color}{city}{reset_color})")
print("---")
print(f"Public IP: {reset_color}\033[33m{ip_address}\033[0m")
print(f"Timezone: {reset_color}\033[33m{timezone}\033[0m")
print(f"Current Time: {reset_color}\033[33m{current_time}\033[0m")
print(f"Latitude: {reset_color}\033[33m{lat}\033[0m")
print(f"Longitude: {reset_color}\033[33m{lon}\033[0m")
print(f"Zipcode: {reset_color}\033[33m{zip_code}\033[0m")
print(f"Region: {reset_color}\033[33m{region}\033[0m")
print(f"Country: {reset_color}\033[33m{country}\033[0m")
print(f"ISP: {reset_color}\033[33m{isp}\033[0m")
print(
f"Connection Type: {reset_color}\033[33m{'Cellular' if mobile else 'Non Cellular'}\033[0m"
)
print(f"Proxy: {reset_color}\033[33m{'Yes' if proxy else 'No'}\033[0m")
print("VPN Status:", vpn_status, reset_color)
print("---")
# option to refresh the data
print("Refresh | refresh=true")
if __name__ == "__main__":
main()