# بسم الله الرحمن الرحيم #!/usr/bin/env python3 """ IP Reporter for Jetson Nano Sends IP address to API on startup """ import socket import requests import time import logging from subprocess import check_output # Configuration API_URL = "https://echo-ip-e46d95cfc953.hosted.ghaymah.systems" # Replace with your API endpoint DEVICE_ID = "jetson-nano-001" # Change to unique ID for your device RETRY_DELAY = 30 # seconds between retries MAX_RETRIES = 100 # Setup logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler('/var/log/ip_reporter.log'), logging.StreamHandler() ] ) def get_ip_address(): """Get the IP address of the device""" try: # Get IP using hostname command ip_output = check_output(['hostname', '-I']).decode('utf-8').strip() ips = ip_output.split() if ips: # Return the first IP address (usually the main one) return ip_output else: return None except Exception as e: logging.error(f"Error getting IP address: {e}") return None def is_internet_available(): """Check if internet connection is available""" try: # Try to connect to a reliable server socket.create_connection(("8.8.8.8", 53), timeout=5) return True except OSError: return False def send_to_api(ip_address): """Send IP address to the API""" payload = { 'device_id': DEVICE_ID, 'ip_address': ip_address, 'timestamp': time.time() } try: response = requests.post(API_URL, json=payload, timeout=10) if response.status_code == 200: logging.info(f"Successfully sent IP {ip_address} to API") return True else: logging.error(f"API returned status code: {response.status_code}") return False except requests.exceptions.RequestException as e: logging.error(f"Error sending to API: {e}") return False def main(): """Main function""" logging.info("IP Reporter started") retries = 0 ip_sent = False while not ip_sent and retries < MAX_RETRIES: # Wait for internet connection if not is_internet_available(): logging.info("Internet not available, waiting...") time.sleep(RETRY_DELAY) retries += 1 continue # Get IP address ip_address = get_ip_address() if ip_address: logging.info(f"Found IP address: {ip_address}") # Send to API if send_to_api(ip_address): ip_sent = True logging.info("IP address successfully reported to API") break else: logging.warning("Failed to send IP to API, retrying...") else: logging.warning("Could not get IP address, retrying...") time.sleep(RETRY_DELAY) retries += 1 if not ip_sent: logging.error("Failed to send IP address after maximum retries") logging.info("IP Reporter finished") if __name__ == "__main__": main()