#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket,struct, os
from django.conf import settings
from main.models import Log

def addressInNetwork(ip, net): 
   ipaddr = int(''.join([ '%02x' % int(x) for x in ip.split('.') ]), 16)
   netstr, bits = net.split('/')
   netaddr = int(''.join([ '%02x' % int(x) for x in netstr.split('.') ]), 16)
   mask = (0xffffffff << (32 - int(bits))) & 0xffffffff
   return (ipaddr & mask) == (netaddr & mask)

def wificmd(action, ip):
    cmd = "{0} {1} {2} {3}".format(settings.SUDO_PATH, settings.FW_SCRIPT, action, ip)
    Log.objects.create(msg="Выполняем: " + cmd, type='debug') 
    os.system(cmd)
