aceberg / WatchYourPorts

Open ports inventory for local servers. Exports data to InfluxDB2/Grafana
https://hub.docker.com/r/aceberg/watchyourports
MIT License
90 stars 3 forks source link

Simple python script that pulls all items from wyl and adds it to wyp #2

Open SteveClement opened 3 weeks ago

SteveClement commented 3 weeks ago

Below is a simple script to quickly populate WatchYourPorts.

⚠️ If an entry exists in wyp, with scan results, it will be overwritten without warning.

#!/usr/bin/env python3
import os
import requests

try:
    from dotenv import load_dotenv
    # Load configuration from .env file
    load_dotenv()
except ImportError:
    print("Note: 'python-dotenv' is not installed. Installing it allows the application to load "
          "environment variables from a .env file, which is helpful for configuration management.")

WYL_URL = os.getenv("WYL_URL", "http://localhost:8840")
WYP_URL = os.getenv("WYP_URL", "http://localhost:8853")

def add_address(name, ip):
    url = f"{WYP_URL}/addr_add/"
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Accept-Language': 'en,en-GB;q=0.9,en-LU;q=0.8,fr-FR;q=0.7,fr;q=0.6,en-US;q=0.5',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Content-Type': 'application/x-www-form-urlencoded',
        'DNT': '1',
        'Origin': WYP_URL,
        'Referer': WYP_URL,
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
    }

    data = {
        'name': name,
        'ip': ip
    }

    response = requests.post(url, headers=headers, data=data, timeout=5)

    if response.status_code == 200:
        print(f"Successfully added address: {name} with IP: {ip}")
    else:
        print(f"Failed to add address: {response.status_code}, {response.text}")

# URL to fetch data from
url = WYL_URL + "/api/all"

# Send a GET request to the URL with a timeout of 5 seconds
response = requests.get(url, timeout=5)
# print(response.json())

# Parse the JSON response
data = response.json()

# Iterate over each element in the JSON data and print the Name and IP
for item in data:
    name = item.get("Name").rstrip('.')
    if len(name) == 0:
        name = "No Name defined" # noqa
    ip = item.get("IP")
    add_address(name, ip)
aceberg commented 1 week ago

Can you, please, make this script into a pull request? Just put it in the configs folder.
Then I'll add it to the README.

SteveClement commented 6 days ago

Sure, I will try to get it done over the weekend.