-
Notifications
You must be signed in to change notification settings - Fork 14
/
netinfo.py
39 lines (30 loc) · 835 Bytes
/
netinfo.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
#!/usr/bin/python3
import sys
import socket
import psutil
def get_ip_addresses(family):
for interface, snics in psutil.net_if_addrs().items():
for snic in snics:
if snic.family == family:
yield (interface, snic.address)
def interface():
ipv4s = list(get_ip_addresses(socket.AF_INET))
nic = ipv4s[1]
returnStr = nic[0]
print(returnStr)
return
def address():
ipv4s = list(get_ip_addresses(socket.AF_INET))
nic = ipv4s[1]
returnStr = nic[1]
print(returnStr)
return
if len(sys.argv) != 2:
print("Please specify interface or address")
else:
if str(sys.argv[1]) == "address":
address()
elif str(sys.argv[1]) == "interface":
interface()
else:
print("Invalid Parameter, only address and interface are accepted")