import socket
import ipaddress
import nmap

class NetworkScanner:
    @staticmethod
    def get_network_range():
        """Obtiene el rango de red basado en la IP local."""
        hostname = socket.gethostname()
        local_ip = socket.gethostbyname(hostname)
        network = ipaddress.ip_network(f"{local_ip}/24", strict=False)
        print(str(network))
        return str(network)

    @staticmethod
    def scan_network(ip_range):
        """Realiza un escaneo de red y devuelve los dispositivos detectados."""
        nm = nmap.PortScanner()
        nm.scan(hosts=ip_range, arguments='-sn')
        devices = []
        for host in nm.all_hosts():
            if nm[host].state() == "up":
                mac = nm[host]['addresses'].get('mac', 'No MAC address found')
                devices.append({'ip': nm[host]['addresses']['ipv4'], 'mac': mac})
        return devices
