Working copy
This commit is contained in:
@@ -1,9 +1,44 @@
|
||||
# plugins/isup.py
|
||||
|
||||
import os
|
||||
import logging
|
||||
import aiohttp
|
||||
import socket
|
||||
import simplematrixbotlib as botlib
|
||||
|
||||
async def check_http(domain):
|
||||
"""
|
||||
Check if HTTP service is up for the given domain.
|
||||
|
||||
Args:
|
||||
domain (str): The target domain.
|
||||
|
||||
Returns:
|
||||
bool: True if HTTP service is up, False otherwise.
|
||||
"""
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(f"http://{domain}") as response:
|
||||
return response.status == 200
|
||||
except aiohttp.ClientError:
|
||||
return False
|
||||
|
||||
async def check_https(domain):
|
||||
"""
|
||||
Check if HTTPS service is up for the given domain.
|
||||
|
||||
Args:
|
||||
domain (str): The target domain.
|
||||
|
||||
Returns:
|
||||
bool: True if HTTPS service is up, False otherwise.
|
||||
"""
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(f"https://{domain}") as response:
|
||||
return response.status == 200
|
||||
except aiohttp.ClientError:
|
||||
return False
|
||||
|
||||
async def handle_command(room, message, bot, PREFIX):
|
||||
"""
|
||||
Function to handle the !isup command.
|
||||
@@ -20,18 +55,29 @@ async def handle_command(room, message, bot, PREFIX):
|
||||
logging.info("Received !isup command")
|
||||
args = match.args()
|
||||
if len(args) != 1:
|
||||
await bot.api.send_text_message(room.room_id, "Usage: !isup <ipv4/ipv6/domain>")
|
||||
await bot.api.send_markdown_message(room.room_id, "Usage: !isup <ipv4/ipv6/domain>")
|
||||
logging.info("Sent usage message to the room")
|
||||
return
|
||||
|
||||
target = args[0]
|
||||
|
||||
# DNS resolution
|
||||
try:
|
||||
response = os.system(f"ping -c 1 {target}")
|
||||
if response == 0:
|
||||
await bot.api.send_text_message(room.room_id, f"{target} is up")
|
||||
else:
|
||||
await bot.api.send_text_message(room.room_id, f"{target} is down")
|
||||
logging.info(f"Sent status of {target} to the room")
|
||||
except Exception as e:
|
||||
await bot.api.send_text_message(room.room_id, f"Error: {e}")
|
||||
logging.error(f"Error occurred while checking {target}: {e}")
|
||||
ip_address = socket.gethostbyname(target)
|
||||
logging.info(f"DNS resolution successful for {target}: {ip_address}")
|
||||
await bot.api.send_markdown_message(room.room_id, f"✅ DNS resolution successful for **{target}**: **{ip_address}** (A record)")
|
||||
except socket.gaierror:
|
||||
logging.info(f"DNS resolution failed for {target}")
|
||||
await bot.api.send_markdown_message(room.room_id, f"❌ DNS resolution failed for **{target}**")
|
||||
return
|
||||
|
||||
# Check HTTP/HTTPS services
|
||||
if await check_http(target):
|
||||
await bot.api.send_markdown_message(room.room_id, f"🖧 **{target}** HTTP service is up")
|
||||
logging.info(f"{target} HTTP service is up")
|
||||
elif await check_https(target):
|
||||
await bot.api.send_markdown_message(room.room_id, f"🖧 **{target}** HTTPS service is up")
|
||||
logging.info(f"{target} HTTPS service is up")
|
||||
else:
|
||||
await bot.api.send_markdown_message(room.room_id, f"😕 **{target}** HTTP/HTTPS services are down")
|
||||
logging.info(f"{target} HTTP/HTTPS services are down")
|
||||
|
Reference in New Issue
Block a user