-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_detail.py
More file actions
78 lines (63 loc) · 2.23 KB
/
ip_detail.py
File metadata and controls
78 lines (63 loc) · 2.23 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
#!/usr/bin/env python3
import subprocess
def fetch_private_ip():
#
# command: ip addr | grep inet | grep -v inet6
#
try:
p1 = subprocess.Popen(["ip", "addr"],
stderr = subprocess.STDOUT,
stdout = subprocess.PIPE)
p2 = subprocess.Popen(["grep", "-w", "inet"],
stdin = p1.stdout,
stderr = subprocess.STDOUT,
stdout = subprocess.PIPE)
#p3 = subprocess.Popen(["grep", "-v", "inet6"],
# stdin = p2.stdout,
# stdout = subprocess.PIPE)
output = p2.stdout.read()
except:
output = "failed to execute command.\r\n"
return output.decode('utf-8')
def fetch_public_ip():
cmd = ['python',
'/opt/pytools/check_webservice.py',
'-a', 'myip.dnsdynamic.org',
'-p', '80',
'-r', '/']
p1 = subprocess.Popen(cmd,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
out, err = p1.communicate()
if not out:
return err
else:
return out
def fetch_public_ip2():
cmd = ['dig',
'@resolver1.opendns.com',
'myip.opendns.com',
'A',
'+short']
p1 = subprocess.Popen(cmd,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
out, err = p1.communicate()
if not out:
return err
else:
return out.decode('utf-8')
if __name__ == "__main__":
print("\n")
print("---------------------------------------------------------------------------")
print("Private IP detail:")
print("---------------------------------------------------------------------------")
for line in fetch_private_ip().splitlines():
print(line.strip())
print("\n")
print("---------------------------------------------------------------------------")
print("Public IP detail:")
print("---------------------------------------------------------------------------")
#print fetch_public_ip()
print(fetch_public_ip2())
# -- end --