Files
wif2ansible/wif2ansible/parsers.py
Kris Forbes 5c95469ca3
All checks were successful
Build and Release / Build Windows Exe (push) Successful in 11s
Support SVR prefix in hostname cleanup
2026-02-06 16:06:30 -05:00

84 lines
2.4 KiB
Python

import re
from typing import List
def clean_header(header: str) -> str:
if not header:
return ""
# Remove HTML tags if any (from Ruby script logic)
header = re.sub(r'<[^>]+>', '', str(header))
return header.strip().lower()
def parse_ports(port_str: str) -> List[int]:
"""
Parses a string containing ports, ranges, or 'any'.
Returns a list of integer ports.
"""
if not port_str:
return []
s = str(port_str).lower()
# Remove 'udp' if present to focus on port numbers,
# but arguably we might want to capture protocol.
# The Ruby script removed it. We'll strip it for port extraction.
s = re.sub(r'udp', '', s)
ports = set()
# Handle 'any' or 'all' - defaulting to common ports as per Ruby script
if 'any' in s or 'all' in s:
return [22, 3389, 80, 443, 3306, 5432, 8443, 60000]
# Split by common delimiters
parts = re.split(r'[,\n\s]+', s)
for part in parts:
part = part.strip()
if not part:
continue
# Range handling: 8000-8010
# The ruby script had issues with ranges, let's do it right.
range_match = re.match(r'^(\d+)[-](\d+)$', part)
if range_match:
start, end = map(int, range_match.groups())
if start <= end:
# User Request: "only add the first, last, and middle port"
ports.add(start)
ports.add(end)
if end - start > 1:
middle = start + (end - start) // 2
ports.add(middle)
continue
# Single port
if part.isdigit():
ports.add(int(part))
return sorted(list(ports))
def clean_reference(ref: str) -> str:
"""
Cleans a server reference string.
Specifically removes 'SRV###' type prefixes if present.
Example: 'SRV123 MyServer' -> 'MyServer'
"""
if not ref:
return ""
s = str(ref)
# Remove SRV or SVR followed by digits and whitespace
s = re.sub(r'S(RV|VR)\d+\s*', '', s, flags=re.IGNORECASE)
# Remove leading/trailing whitespace
return s.strip()
def parse_ip(ip_str: str) -> List[str]:
"""Finds all IPv4 addresses in a string."""
if not ip_str:
return []
s = str(ip_str)
# Simple regex for IPv4
ips = re.findall(r'\b(?:\d{1,3}\.){3}\d{1,3}\b', s)
return list(set(ips))