Example #1
0
 def path_find_existing(self, dst, as_ip=False):
     if dst in [host.name for host in Host.find_all()]:
         host = Host.find_one(name=dst)
         dst = host.closest_endpoint
     else:
         try:
             dst = Endpoint.find_one(ip_port=dst)
         except:
             print("Please specify a valid endpoint in the IP:PORT form")
             return
     if dst is None:
         print("The endpoint provided doesn't exist in this workspace")
         return
     if Path.direct(dst):
         print("The destination should be reachable from the host")
         return
     try:
         chain = Path.get(dst)
     except NoPathError:
         print("No path could be found to the destination")
         return
     if chain[0] is None:
         chain[0] = "local"
     if as_ip:
         print(" > ".join(str(link.closest_endpoint) \
                 if isinstance(link, Host) else str(link) for link in chain))
     else:
         print(" > ".join(str(link) for link in chain))
Example #2
0
    def host_del(self, host):
        """Remove a :class:`Host` from the workspace

        Args:
            name (str): The `Host` 's username
        """

        if host not in [host.name for host in Host.find_all()]:
            print("Not a known Host name.")
            return False
        host = Host.find_one(name=host)
        self.unstore(host.delete())
        return True
Example #3
0
 def path_add(self, src, dst):
     if src.lower() != "local":
         if src not in [host.name for host in Host.find_all()]:
             print("Not a known Host name.")
             return
         src = Host.find_one(name=src)
         if src is None:
             print("The source Host provided doesn't exist in this workspace")
             return
     else:
         src = None
     try:
         dst = Endpoint.find_one(ip_port=dst)
     except:
         print("Please specify valid destination endpoint in the IP:PORT form")
     if dst is None:
         print("The destination endpoint provided doesn't exist in this workspace")
         return
     path = Path(src, dst)
     path.save()
     print("Path saved")
Example #4
0
 def get_objects(self, local=False, hosts=False, connections=False, endpoints=False, \
         users=False, creds=False, tunnels=False, paths=False, scope=None, tags=None):
     ret = []
     if local:
         ret.append("local")
     if hosts:
         ret = ret + Host.find_all(scope=scope)
     if connections:
         ret = ret + Connection.find_all(scope=scope)
     if endpoints:
         ret = ret + Endpoint.find_all(scope=scope)
     if users:
         ret = ret + User.find_all(scope=scope)
     if creds:
         ret = ret + Creds.find_all(scope=scope)
     if tunnels:
         ret = ret + list(self.tunnels.values())
     if paths:
         ret = ret + Path.find_all()
     if tags:
         ret = ret + Tag.find_all()
     return ret
Example #5
0
 def path_del(self, src, dst):
     if str(src).lower() != "local":
         if src not in [host.name for host in Host.find_all()]:
             print("Not a known Host name.")
             return False
         src = Host.find_one(name=src)
         if src is None:
             print("The source Host provided doesn't exist in this workspace")
             return False
     else:
         src = None
     try:
         dst = Endpoint.find_one(ip_port=dst)
     except:
         print("Please specify valid destination endpoint in the IP:PORT form")
     if dst is None:
         print("The destination endpoint provided doesn't exist in this workspace")
         return False
     path = Path(src, dst)
     if path.id is None:
         print("The specified Path doesn't exist in this workspace.")
         return False
     self.unstore(path.delete())
     return True
Example #6
0
    def probe(self, targets, gateway="auto", verbose=False, find_new=False):
        for endpoint in targets:
            print("Probing \033[1;34m"+str(endpoint)+"\033[0m > ", end="", flush=True)
            if verbose:
                print("")

            conn = Connection(endpoint, None, None)
            working = False
            if not find_new and endpoint.reachable and str(gateway) == "auto":
                if verbose:
                    print("\nEndpoint is supposed to be reachable, trying...")
                working = conn.probe(verbose=verbose)
                host = Host.find_one(prev_hop_to=endpoint)
            if not working and str(gateway) != "auto":
                if verbose:
                    print("\nA gateway was given, trying...")
                if gateway == "local":
                    gateway_conn = None
                    host = None
                else:
                    host = Host.find_one(name=gateway)
                    gateway_conn = Connection.find_one(endpoint=host.closest_endpoint)
                try:
                    working = conn.probe(gateway=gateway_conn, verbose=verbose)
                except ConnectionClosedError as exc:
                    print("\nError: "+str(exc))
                    return
            if not working and not find_new:
                try:
                    Path.get(endpoint)
                except NoPathError:
                    pass
                else:
                    if verbose:
                        print("\nThere is an existing path to the Endpoint, trying...")
                    working = conn.probe(verbose=verbose)
                    host = Host.find_one(prev_hop_to=endpoint)
                    if not working and host is not None:
                        self.path_del(host, endpoint)
            if not working:
                if verbose:
                    print("\nTrying to reach directly from local...")
                host = None
                working = conn.probe(gateway=None, verbose=verbose)
            if not working:
                if verbose:
                    print("\nTrying from every Host from closest to furthest...")
                hosts = Host.find_all(scope=True)
                hosts.sort(key=lambda h: h.distance)
                working = False
                for host in hosts:
                    gateway_endpoint = host.closest_endpoint
                    loop_gateway = Connection.find_one(endpoint=gateway_endpoint)
                    working = conn.probe(gateway=loop_gateway, verbose=verbose)
                    if working:
                        break

            if working:
                path = Path(host, endpoint)
                path.save()
                if host is None:
                    print("\033[1;32mOK\033[0m: reached directly from \033[1;34mlocal\033[0m.")
                else:
                    print("\033[1;32mOK\033[0m: reached using \033[1;34m"+str(host)+"\033[0m as gateway")
            else:
                print("\033[1;31mKO\033[0m: could not reach the endpoint.")
            if verbose:
                print("########################\n")