-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi3_get_external_ip.py
More file actions
executable file
·77 lines (64 loc) · 2.14 KB
/
i3_get_external_ip.py
File metadata and controls
executable file
·77 lines (64 loc) · 2.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
#!/usr/bin/env python3
#
# GNU GENERAL PUBLIC LICENSE v3
#
# Copyright (C) <2016> JOSE MARCOS <jm4rcos@gmail.com>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.
#
# See <http://www.gnu.org/licenses/> for more details of the GNU General Public
# License.
#
# comments/requirements:
# no commnets/requirements
import urllib.request, urllib.error, urllib.parse
import itertools
def check_url_for_ipaddr(url):
# fuction to return ip address informed by url
ip = urllib.request.urlopen(url, timeout=5).read().strip()
return ip
# list with sites to check my local ip address
url_list = \
[
'http://plain-text-ip.com/' ,
'https://api.ipify.org/' ,
'http://ipecho.net/plain'
]
# list to hold ip addresses
ipa_result = []
# url error count
e = 0
# check each url in url list, save ip addresses in a list and count errors
for l in url_list:
try:
ipa = check_url_for_ipaddr(l)
ipa_result.append(ipa)
# counting check errors
except urllib.error.HTTPError as err:
e += 1
except urllib.error.URLError as err:
e += 1
# Print ip address list result, for debuging pourpose
#
#print(ipa_result)
# evaluate ip addresses in ip address list, errors and ip mismatch
try:
if len(ipa_result) == 0: raise ValueError()
m = 0
# compare items in ip address list to find discrepancies (check if ip add
# are equal, they should be!) and count them
for a, b in itertools.combinations(ipa_result,2):
if a != b: m += 1
# print them all!
print (ipa_result[0].decode(), 'E[{}]'.format(e), 'M[{}]'.format(m))
except:
print ("<span font_style='italic'>no ext ip.addr</span>")
print ("<span font_style='italic'>no ext ip.addr</span>")
print ('#595959')