Untitled

 avatar
user_3431143
plain_text
a year ago
3.1 kB
1
Indexable
Never
import json
import requests
import os
import time
from datetime import datetime
from scapy.all import ARP, Ether, srp

# Set webhook URLs
device_webhook_url = 'https://webhook.site/898bae18-a5de-4100-8acb-1db15a02081e'
log_webhook_url = 'https://webhook.site/2ac7238e-81ab-40d4-b2f4-6be4a7afc066'

# Set log file name
log_file = 'network_scanner_log.txt'

# Set network IP range
network_range = '192.168.0.0/24'

# Set device status dictionary
device_status = {}

# Function to scan network and return list of devices
def scan_network():
    # Send ARP broadcast request and collect responses
    arp = ARP(pdst=network_range)
    ether = Ether(dst='ff:ff:ff:ff:ff:ff')
    packet = ether/arp
    result = srp(packet, timeout=3, verbose=0)[0]

    # Extract information for each response
    devices = []
    for sent, received in result:
        devices.append({'ip_address': received.psrc, 'mac_address': received.hwsrc})
    return devices

# Function to check if a device is online
def is_device_online(ip):
    # Send ARP request to device and wait for response
    arp = ARP(pdst=ip)
    ether = Ether(dst='ff:ff:ff:ff:ff:ff')
    packet = ether/arp
    result = srp(packet, timeout=3, verbose=0)[0]

    # Return True if response is received
    return bool(result)

# Function to send device data to webhook
def send_device_webhook(data):
    response = requests.post(device_webhook_url, json=data)
    return response.status_code

# Function to send log message to webhook
def send_log_webhook(message):
    response = requests.post(log_webhook_url, json={'message': message})
    return response.status_code

# Function to load device data from JSON file
def load_devices():
    if os.path.exists('devices.json'):
        with open('devices.json', 'r') as f:
            devices = json.load(f)
        return devices
    else:
        return []

# Function to save device data to JSON file
def save_devices(devices):
    with open('devices.json', 'w') as f:
        json.dump(devices, f, indent=4)

# Function to check devices for changes in online/offline status
def check_devices(devices):
    # Check status of each device
    for device in devices:
        online = is_device_online(device['ip_address'])
        if device['ip_address'] in device_status:
            if online != device_status[device['ip_address']]:
                # Device status has changed
                device_status[device['ip_address']] = online
                message = f"{device['ip_address']} is now {'online' if online else 'offline'}."
                send_log_webhook(message)
                device['status'] = 'online' if online else 'offline'
                send_device_webhook(device)
        else:
            # Device not in status dictionary, add it
            device_status[device['ip_address']] = online
            device['status'] = 'online' if online else 'offline'
            send_device_webhook(device)

    # Check for new devices
    new_devices = []
    for device in scan_network():
        if device not in devices:
            # New device found
            new_devices