Compare commits
18 Commits
eceb7ad1f3
...
statusFeat
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff9edd855e | ||
|
|
76069b8193 | ||
|
|
aad6fb97a6 | ||
|
|
6ab6224ff6 | ||
|
|
2b986eeabb | ||
|
|
90a7cb8581 | ||
|
|
7bccab9455 | ||
| 03b7177ecf | |||
|
|
807517be96 | ||
| 166067972e | |||
| 1aad27b937 | |||
| 2521b34df0 | |||
| 69dd4bd1f4 | |||
| 87a3ee2886 | |||
| 439a7d545e | |||
|
|
48f773f374 | ||
|
|
03dd3ba495 | ||
|
|
fc40664ddf |
1
.env
Normal file
1
.env
Normal file
@@ -0,0 +1 @@
|
|||||||
|
DISCORD_WEBHOOK_URL='https://discord.com/api/webhooks/1229025346385608824/kIt2dp2znu0jR85QbOPmoMAteHaB9BCJDTMfn0cHE9mwpxHkbUNc9By2Y7n6gpufoyco'
|
||||||
5
pyvenv.cfg
Normal file
5
pyvenv.cfg
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
home = C:\Users\vgall\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0
|
||||||
|
include-system-site-packages = false
|
||||||
|
version = 3.11.9
|
||||||
|
executable = C:\Users\vgall\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe
|
||||||
|
command = C:\Users\vgall\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe -m venv D:\Documentos\Status
|
||||||
85
status.py
85
status.py
@@ -1,22 +1,93 @@
|
|||||||
import argparse
|
import argparse
|
||||||
|
from pythonping import ping
|
||||||
|
import ipaddress
|
||||||
|
import requests
|
||||||
|
import logging
|
||||||
|
|
||||||
|
DISCORD_WEBHOOK_URL='https://discord.com/api/webhooks/1229025346385608824/kIt2dp2znu0jR85QbOPmoMAteHaB9BCJDTMfn0cHE9mwpxHkbUNc9By2Y7n6gpufoyco'
|
||||||
|
logging.basicConfig(filename='/var/log/status.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
|
|
||||||
|
def read_document(document_name):
|
||||||
|
with open(document_name, 'r') as file:
|
||||||
|
# Leer todas las líneas del archivo y guardarlas en un conjunto
|
||||||
|
return {linea.rstrip('\n') for linea in file}
|
||||||
|
|
||||||
def parseArguments():
|
def parseArguments():
|
||||||
# Crear un objeto ArgumentParser
|
# Crear un objeto ArgumentParser
|
||||||
parser = argparse.ArgumentParser(description='Ejemplo de script con flags')
|
parser = argparse.ArgumentParser(description='Ejemplo de script con flags')
|
||||||
|
|
||||||
# Agregar los argumentos con sus respectivos flags
|
# Agregar los argumentos con sus respectivos flags
|
||||||
parser.add_argument('-w', '--web', action='store', dest='web', help='Indicar una web')
|
parser.add_argument('-W', '--web', action='store', dest='web', help='Indicar un archivo con lista de Webs')
|
||||||
parser.add_argument('-h', '--host', action='store', dest='host', help='Indicar un host')
|
parser.add_argument('-H', '--host', action='store', dest='host', help='Indicar un archivo con lista de hosts')
|
||||||
|
|
||||||
|
try:
|
||||||
# Parsear los argumentos de la línea de comandos
|
# Parsear los argumentos de la línea de comandos
|
||||||
args = parser.parse_args()
|
return parser.parse_args()
|
||||||
|
except argparse.ArgumentError as e:
|
||||||
|
# Si ocurre un error al analizar los argumentos, mostrar un mensaje de error
|
||||||
|
print("Error:", e)
|
||||||
|
# Mostrar el mensaje de ayuda
|
||||||
|
parser.print_help()
|
||||||
|
exit()
|
||||||
|
|
||||||
return args
|
def toIP(hosts):
|
||||||
|
ips = set()
|
||||||
|
for host in hosts:
|
||||||
|
try:
|
||||||
|
ip = ipaddress.IPv4Address(host)
|
||||||
|
ips.add(ip)
|
||||||
|
except ipaddress.AddressValueError:
|
||||||
|
print(f"La entrada '{host}' no es una dirección IP válida.")
|
||||||
|
return ips
|
||||||
|
|
||||||
|
def check_ping(hosts, fails):
|
||||||
|
for host in hosts:
|
||||||
|
try:
|
||||||
|
response = ping(str(host), count=4, timeout=2) # verbose=True -> Para ver la respuesta del ping
|
||||||
|
if not response.success():
|
||||||
|
logging.info(f"Bad ping: {host}")
|
||||||
|
if (host not in fails):
|
||||||
|
send_discord_message(f"Ping fallido para el host: {host}")
|
||||||
|
# Añadir el host a fails
|
||||||
|
fails.add(host)
|
||||||
|
elif response.success():
|
||||||
|
if host in fails:
|
||||||
|
fails.remove(host)
|
||||||
|
logging.info(f"Ping correct: {host}")
|
||||||
|
except Exception as e:
|
||||||
|
logging.info(f"Bad ping: {host}")
|
||||||
|
print(e)
|
||||||
|
print(fails)
|
||||||
|
save_status(fails)
|
||||||
|
|
||||||
|
def save_status(fails):
|
||||||
|
with open("failed.txt", "w") as file:
|
||||||
|
for fail in fails:
|
||||||
|
file.write(str(fail) + "\n")
|
||||||
|
file.close
|
||||||
|
|
||||||
|
def send_discord_message(message):
|
||||||
|
data = {
|
||||||
|
"content": message
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
response = requests.post(DISCORD_WEBHOOK_URL, json=data)
|
||||||
|
response.raise_for_status()
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
print(f"Error sending Discord message: {e}")
|
||||||
|
logging.info(f"Error sending Discord message: {e}")
|
||||||
|
else:
|
||||||
|
logging.info("Discord message sent successfully!")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
args = parseArguments()
|
args = parseArguments()
|
||||||
|
ips_and_hosts = set()
|
||||||
|
fails = set()
|
||||||
|
if args.host:
|
||||||
|
ips_and_hosts.update(toIP(read_document(args.host)))
|
||||||
|
fails.update(toIP(read_document("failed.txt")))
|
||||||
if args.web:
|
if args.web:
|
||||||
print()# Parte web, comprobar que Código devuelve
|
print("Hay webs")
|
||||||
else:
|
# Parsear correctamente
|
||||||
print()# Parte host, hará ping
|
# ips_and_hosts.update()
|
||||||
|
check_ping(ips_and_hosts, fails)
|
||||||
Reference in New Issue
Block a user