(Translated by https://www.hiragana.jp/)
Simple python script that pulls all items from wyl and adds it to wyp · Issue #2 · aceberg/WatchYourPorts · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SteveClement opened this issue Sep 2, 2024 · 2 comments

Comments

@SteveClement
Copy link

SteveClement commented Sep 2, 2024

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
Copy link
Owner

aceberg commented Sep 17, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants