Skip to content
Snippets Groups Projects
scan_utilities.py 1.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • import json
    import logging
    
    
    SSH_PORT = '22'
    HTTP_PORT = '80'
    HTTPS_PORT = '443'
    
    
    def pretty_json(j):
        return json.dumps(j,sort_keys=True,indent=4)
            
    def import_dep_info(file_path, endpoint_keys):
        
        with open(file_path) as f:
            data = json.load(f)    
        
        endpoints = {}
    
    Gioacchino Vino's avatar
    Gioacchino Vino committed
        for key,value in data['outputs'].items():
    
            if "_ip" in key:
                if isinstance(value, str):
    
                    logging.info(f"Endpoint: {value}:{SSH_PORT}")
                    endpoints[value] = {SSH_PORT}
    
        
        if endpoint_keys != "None":
            list_endpoints = endpoint_keys.split(',')
            for key in data['outputs'].keys():
                if key in list_endpoints:
                    endpoint = str(data['outputs'][key])
                    prefix,url = endpoint.split("://")
                    if ":" in url:
                        host,port = url.split(":")
                    else:
                        host = url
                        if prefix == "https":
    
                            port = HTTPS_PORT
    
                        elif prefix == 'http':
    
                            port = HTTP_PORT
    
                        else:
                            raise Exception(f"Impossible to parse the endpoint port. Endpoint: {endpoint}")
                    logging.info(f"Endpoint: {host}:{port}")
                    if host not in endpoints:
                        endpoints[host] = {port}
                    else:
                        endpoints[host].add(port)
        
        for host,ports in endpoints.items():
            endpoints[host] = sorted(list(ports))
        return endpoints