import sys import requests from requests.auth import HTTPBasicAuth import json # Global variables for authentication USERNAME = "apitest" PASSWORD = "apitest" def call_api(url, method='GET', data=None): try: if method == 'GET': response = requests.get( url, auth=HTTPBasicAuth(USERNAME, PASSWORD), verify=False # Note: This disables SSL certificate verification. Use with caution. ) elif method == 'POST': response = requests.post( url, auth=HTTPBasicAuth(USERNAME, PASSWORD), json=data, verify=False # Note: This disables SSL certificate verification. Use with caution. ) response.raise_for_status() json_response = response.json() if isinstance(json_response, dict) and json_response.get('message') == 'Success': return json_response.get('data') else: print(f"API call failed: {json_response.get('message', 'Unknown error')}") return None except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") return None def get_interfaces(ip_address): url = f"https://{ip_address}/api/datasources/proxy/2/interfaces" return call_api(url) def get_interface_status(ip_address, pretty_mac): url = f"https://{ip_address}/api/datasources/proxy/2/interfaces/{pretty_mac}" return call_api(url) def toggle_capture(ip_address, pretty_mac): url = f"https://{ip_address}/api/datasources/proxy/2/interfaces/{pretty_mac}/capture/toggle" return call_api(url, method='POST') def can_start_capture(interface_status): return ( interface_status['physical_state']['state'] == 'idle' and not interface_status['capture_enabled'] ) def print_interface_capture_status(ip_address, interfaces): if interfaces: print("Interface Capture Status:") for interface in interfaces: pretty_mac = interface['pretty_mac'] name = interface['interface']['name'] status = get_interface_status(ip_address, pretty_mac) if status: capture_status = can_start_capture(status) print(f"Interface: {name} (Pretty MAC: {pretty_mac})") print(f" Can start capture: {'Yes' if capture_status else 'No'}") print(f" Current state: {status['physical_state']['state']}") print(f" Capture enabled: {status['capture_enabled']}") print() else: print(f"Interface: {name} (Pretty MAC: {pretty_mac})") print(" Unable to retrieve status") print() else: print("No interfaces found or unable to retrieve interface information.") def main(): if len(sys.argv) < 2 or len(sys.argv) > 3: print("Usage: python script.py [pretty-mac]") sys.exit(1) ip_address = sys.argv[1] pretty_mac = sys.argv[2] if len(sys.argv) == 3 else None if pretty_mac: # Check status of specific interface and toggle capture status = get_interface_status(ip_address, pretty_mac) if status: print(f"Current status for interface {pretty_mac}:") print(f" State: {status['physical_state']['state']}") print(f" Capture enabled: {status['capture_enabled']}") print("\nToggling capture state...") toggle_result = toggle_capture(ip_address, pretty_mac) if toggle_result is not None: print("Capture state toggled successfully.") print("New status:") print(f" Capture enabled: {toggle_result['capture_enabled']}") else: print("Failed to toggle capture state.") else: print(f"Failed to retrieve status for interface {pretty_mac}") else: # List all interfaces and their capture status interfaces = get_interfaces(ip_address) if interfaces: print_interface_capture_status(ip_address, interfaces) else: print("Failed to retrieve interface information.") if __name__ == "__main__": main()