utils.py
1.17 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
#!/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'eduardo'
import netifaces
import netaddr
import socket
from pprint import pformat
def get_local_network(myiface='eth0'):
"""
Função que encontra a rede local.
Fonte: http://stackoverflow.com/questions/3755863/trying-to-use-my-subnet-address-in-python-code
:param myiface: Interface local a ser utilizada na busca
:return: IPNetwork instance
"""
ifaces = netifaces.interfaces()
# => ['lo', 'eth0', 'eth1']
addrs = netifaces.ifaddresses(myiface)
# {2: [{'addr': '192.168.1.150',
# 'broadcast': '192.168.1.255',
# 'netmask': '255.255.255.0'}],
# 10: [{'addr': 'fe80::21a:4bff:fe54:a246%eth0',
# 'netmask': 'ffff:ffff:ffff:ffff::'}],
# 17: [{'addr': '00:1a:4b:54:a2:46', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]}
# Get ipv4 stuff
ipinfo = addrs[socket.AF_INET][0]
address = ipinfo['addr']
netmask = ipinfo['netmask']
# Create ip object and get
cidr = netaddr.IPNetwork('%s/%s' % (address, netmask))
# => IPNetwork('192.168.1.150/24')
#network = cidr.network
# => IPAddress('192.168.1.0')
return cidr