Here's the script i am having trouble on checking the internet connection of the ip address from other network.
import platform
import subprocess
import requests
import smtplib
from email.mime.text import MIMEText
import meraki
import time
from datetime import datetime, timedelta
# Your API key and configurations
API_KEY = ""
NETWORK_ID = "" # Network ID of the device
DEVICE_SERIAL = "" # Serial number of the device
# Gmail configurations
SMTP_SERVER = ''
SMTP_PORT = 587
SMTP_USER = ''
SMTP_PASSWORD = ''
NOTIFY_EMAIL = 'i'
# Initialize Meraki Dashboard API
dashboard = meraki.DashboardAPI(API_KEY)
def get_network_name(network_id😞
"""Get the name of the network for the given network ID."""
network = dashboard.networks.getNetwork(network_id)
return network.get('name', 'Unknown Network')
def get_device_details(network_id, serial😞
"""Get device details for the given serial number and network ID."""
devices = dashboard.networks.getNetworkDevices(network_id)
for device in devices:
if device['serial'] == serial:
return device
print(f"Device with serial {serial} not found in network ID: {network_id}")
return None
"""Check if a request to the given URL is successful."""
try:
print(f"Testing connectivity to {url}...")
response = requests.get(url, timeout=10)
if response.status_code == 200:
print(f"Successful request to {url}. Status code: {response.status_code}")
return True
else:
print(f"Request to {url} returned status code: {response.status_code}. Connectivity might be unreliable.")
return False
except requests.RequestException as e:
print(f"Request to {url} failed with exception: {e}. Connectivity might be down.")
return False
def check_ping(ip_address😞
"""Check if the IP address responds to a ping request."""
try:
print(f"Pinging {ip_address}...")
# Determine the correct ping command based on the platform
param = '-n' if platform.system().lower() == 'windows' else '-c'
result = subprocess.run(['ping', param, '4', ip_address], capture_output=True, text=True)
if result.returncode == 0:
print(f"Ping successful to {ip_address}.\n{result.stdout}")
return True
else:
print(f"Ping failed to {ip_address}.\n{result.stderr}")
return False
except Exception as e:
print(f"Ping failed with exception: {e}")
return False
def get_wan_status(device😞
"""Check WAN1 and WAN2 status and internet connectivity sequentially."""
wan1_ip = device.get('wan1Ip', None)
wan2_ip = device.get('wan2Ip', None)
# Initialize WAN statuses
wan1_status = "Not Connected"
wan2_status = "Not Connected"
# Check WAN1 IP Address
if wan1_ip:
print(f"WAN1 IP Address available: {wan1_ip}")
# Check internet connectivity for WAN1
if check_internet():
wan1_status = "Active"
else:
wan1_status = "Failed"
else:
print("WAN1 IP Address not available.")
# Check WAN2 IP Address
if wan2_ip:
print(f"WAN2 IP Address available: {wan2_ip}")
# Check internet connectivity for WAN2
if check_internet() and check_ping(wan2_ip😞
wan2_status = "Active"
else:
wan2_status = "Failed"
else:
print("WAN2 IP Address not available.")
wan2_status = "Not Connected"
return wan1_status, wan2_status
def send_email(subject, body😞
"""Send an email notification using Gmail SMTP server."""
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = SMTP_USER
msg['To'] = NOTIFY_EMAIL
try:
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SMTP_USER, SMTP_PASSWORD)
server.send_message(msg)
print(f"Email sent to {NOTIFY_EMAIL}.")
except Exception as e:
print(f"Failed to send email: {e}")
def main():
# Initialize previous statuses and last update time
last_wan1_status = None
last_wan2_status = None
last_update_time = datetime.now()
while True:
print("Checking WAN Status...")
device = get_device_details(NETWORK_ID, DEVICE_SERIAL)
if device:
network_name = get_network_name(NETWORK_ID)
# Check WAN status
wan1_status, wan2_status = get_wan_status(device)
# Print WAN status
print(f"Network: {network_name}")
print(f"WAN1 Status: {wan1_status}")
print(f"WAN2 Status: {wan2_status}")
# Check if status has changed
if (wan1_status != last_wan1_status) or (wan2_status != last_wan2_status😞
# Prepare email content
subject = f"WAN Status Update for {network_name}"
body = (f"Network: {network_name}\n"
f"WAN1 Status: {wan1_status}\n"
f"WAN2 Status: {wan2_status}")
send_email(subject, body)
# Update last known status and time
last_wan1_status = wan1_status
last_wan2_status = wan2_status
last_update_time = datetime.now()
else:
# Check if 24 hours have passed since the last update
if datetime.now() - last_update_time >= timedelta(hours=24😞
# Prepare reminder email content
subject = f"Reminder: No WAN Status Change for {network_name}"
body = (f"Network: {network_name}\n"
f"WAN1 Status: {last_wan1_status}\n"
f"WAN2 Status: {last_wan2_status}\n"
f"Note: No status change detected for 24 hours.")
send_email(subject, body)
# Update last update time to avoid repeated reminders
last_update_time = datetime.now()
else:
print("Unable to retrieve device details.")
# Indicate the next check cycle
print("Checking WAN Status...\n")
# Wait for a short period before checking again
time.sleep(30) # Check every 10 seconds
if __name__ == "__main__":
main()