Files
DingerPingerPython/IPDingv2.py
2025-11-03 17:54:59 +00:00

90 lines
3.4 KiB
Python

#!/usr/bin/env python3
import sys
import subprocess
from pathlib import Path
import platform
IP_LIST = Path("iplist.txt")
SUCCESS_FILE = Path("successful.txt")
ERROR_FILE = Path("error.txt")
PROCESSED_SUCCESS_CSV = Path("processed.csv")
PROCESSED_ERROR_CSV = Path("processederror.csv")
def ping(host: str, timeout_ms: int = 1000) -> bool:
"""
Return True if host responds to a single ping, False otherwise.
Cross-platform flags:
- Windows: ping -n 1 -w <ms>
- Unix: ping -c 1 -W <sec>
"""
if platform.system().lower().startswith("win"):
cmd = ["ping", "-n", "1", "-w", str(timeout_ms), host]
else:
# On most Unix, -W is timeout (seconds) for each reply; convert ms to sec ceiling
sec = max(1, (timeout_ms + 999) // 1000)
cmd = ["ping", "-c", "1", "-W", str(sec), host]
try:
result = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=False)
return result.returncode == 0
except Exception:
return False
def convert_txt_to_csv(txt_path: Path, csv_path: Path, header: str):
"""
Converts lines from a .txt file to a single-column CSV where spaces in a line
are replaced with commas to mimic the batch 'set "line=!line: =,!"' behavior.
"""
# Read all lines (strip trailing newlines)
lines = [line.rstrip("\r\n") for line in txt_path.read_text(encoding="utf-8", errors="ignore").splitlines()]
with csv_path.open("w", encoding="utf-8", newline="") as f:
# We'll produce a simple CSV: each transformed line as one row.
# The header is a single column.
f.write(header + "\n")
for line in lines:
# Replace spaces with commas—matching batch behavior
transformed = line.replace(" ", ",")
f.write(transformed + "\n")
def main():
if not IP_LIST.exists():
print("IP list file not found.")
sys.exit(1)
# Truncate / create output files
SUCCESS_FILE.write_text("", encoding="utf-8")
ERROR_FILE.write_text("", encoding="utf-8")
# Process each line in iplist.txt
for raw in IP_LIST.read_text(encoding="utf-8", errors="ignore").splitlines():
ip = raw.strip()
# Skip empties and comments
if not ip or ip.startswith("#") or ip.startswith(";"):
continue
ok = ping(ip, timeout_ms=1000)
if ok:
with SUCCESS_FILE.open("a", encoding="utf-8") as s:
s.write(ip + "\n")
else:
with ERROR_FILE.open("a", encoding="utf-8") as e:
e.write(ip + "\n")
# Convert both lists to CSVs, replacing spaces with commas (even though lines are likely single IPs)
# The batch file we saw created a "processederror.csv" with "Error" as header.
# We'll also produce "processed.csv" for successful.txt with "Successful" header.
if SUCCESS_FILE.exists():
convert_txt_to_csv(SUCCESS_FILE, PROCESSED_SUCCESS_CSV, header="Successful")
print(f'Converted {SUCCESS_FILE} -> {PROCESSED_SUCCESS_CSV}')
if ERROR_FILE.exists():
convert_txt_to_csv(ERROR_FILE, PROCESSED_ERROR_CSV, header="Error")
print(f'Converted {ERROR_FILE} -> {PROCESSED_ERROR_CSV}')
print("Done. Outputs:")
print(f" {SUCCESS_FILE.resolve()}")
print(f" {ERROR_FILE.resolve()}")
print(f" {PROCESSED_SUCCESS_CSV.resolve()}")
print(f" {PROCESSED_ERROR_CSV.resolve()}")
if __name__ == "__main__":
main()