Merge branch 'main' of ssh://gitssh.mainserverprivate.org:8222/vgallegoiz/Status
This commit is contained in:
1
.env
Normal file
1
.env
Normal file
@@ -0,0 +1 @@
|
|||||||
|
DISCORD_WEBHOOK_URL='https://discord.com/api/webhooks/1229025346385608824/kIt2dp2znu0jR85QbOPmoMAteHaB9BCJDTMfn0cHE9mwpxHkbUNc9By2Y7n6gpufoyco'
|
||||||
65
status.py
65
status.py
@@ -1,6 +1,16 @@
|
|||||||
import argparse
|
import argparse
|
||||||
from pythonping import ping
|
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
|
||||||
@@ -12,11 +22,7 @@ def parseArguments():
|
|||||||
|
|
||||||
try:
|
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()
|
||||||
if args.web:
|
|
||||||
return args.web
|
|
||||||
elif args.host:
|
|
||||||
return args.host
|
|
||||||
except argparse.ArgumentError as e:
|
except argparse.ArgumentError as e:
|
||||||
# Si ocurre un error al analizar los argumentos, mostrar un mensaje de error
|
# Si ocurre un error al analizar los argumentos, mostrar un mensaje de error
|
||||||
print("Error:", e)
|
print("Error:", e)
|
||||||
@@ -24,15 +30,48 @@ def parseArguments():
|
|||||||
parser.print_help()
|
parser.print_help()
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
def check_ping(host):
|
def toIP(hosts):
|
||||||
try:
|
ips = set()
|
||||||
response = ping(host, count=4, timeout=2) # verbose=True -> Para ver la respuesta del ping
|
for host in hosts:
|
||||||
return True if response.success() else False
|
try:
|
||||||
except Exception as e:
|
ip = ipaddress.IPv4Address(host)
|
||||||
print(f"Error making ping to {host}: {e}")
|
ips.add(ip)
|
||||||
return False
|
except ipaddress.AddressValueError:
|
||||||
|
print(f"La entrada '{host}' no es una dirección IP válida.")
|
||||||
|
return ips
|
||||||
|
|
||||||
|
def check_ping(hosts):
|
||||||
|
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}")
|
||||||
|
send_discord_message(f"Ping fallido para el host: {host}")
|
||||||
|
elif response.success():
|
||||||
|
logging.info(f"Ping correct: {host}")
|
||||||
|
except Exception as e:
|
||||||
|
logging.info(f"Bad ping: {host}")
|
||||||
|
|
||||||
|
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()
|
||||||
print(check_ping(args))
|
ips_and_hosts = set()
|
||||||
|
if args.host:
|
||||||
|
ips_and_hosts.update(toIP(read_document(args.host)))
|
||||||
|
if args.web:
|
||||||
|
print("Hay webs")
|
||||||
|
# Parsear correctamente
|
||||||
|
# ips_and_hosts.update()
|
||||||
|
check_ping(ips_and_hosts)
|
||||||
Reference in New Issue
Block a user