Ejemplo n.º 1
0
def main():
    arg_parser = getargs.get_arg_parser()
    arg_parser.add_argument("port1",
                            metavar="PORT_ID_1",
                            type=str,
                            nargs="?",
                            help="Logical port 1")
    arg_parser.add_argument("port2",
                            metavar="PORT_ID_2",
                            type=str,
                            nargs="?",
                            help="Logical port 2")
    args = arg_parser.parse_args()

    # Verify exactly 0 or 2 ports were given
    ports = [x for x in [args.port1, args.port2] if x is not None]
    if len(ports) not in (0, 2):
        arg_parser.error("Give exactly two logical port IDs, or none to "
                         "get a list of logical ports.")

    stub_config = auth.get_session_auth_stub_config(args.user, args.password,
                                                    args.nsx_host,
                                                    args.tcp_port)

    pp = PrettyPrinter()

    # Instantiate all the services we'll need.
    lp_svc = LogicalPorts(stub_config)
    forwardingpath_svc = ForwardingPath(stub_config)

    # Find all logical ports
    all_lps = lp_svc.list().results
    lp_ids = [lp.id for lp in all_lps]
    fail = False
    for port in ports:
        if port not in lp_ids:
            print("Logical port %s not found" % port)
            fail = True
    if fail:
        sys.exit(1)
    print("Checking forwarding path between %s and %s" %
          (args.port1, args.port2))
    if len(ports) == 0:
        # Print all the ports
        for lp in all_lps:
            # Show the port's name and id, the logical switch it's on,
            # and what it's attached to, if anything.
            print("Logical port %s (id %s)" % (lp.display_name, lp.id))
            print(" On switch %s" % lp.logical_switch_id)
            attachment = ("None" if lp.attachment is None else "%s %s" %
                          (lp.attachment.attachment_type, lp.attachment.id))
            print(" Attached to: %s" % attachment)

    else:
        # Print forwarding path
        path = forwardingpath_svc.get(ports[0], ports[1])
        pp.pprint(path)
Ejemplo n.º 2
0
def main():
    arg_parser = getargs.get_arg_parser()
    arg_parser.add_argument("-s",
                            "--remote_ssh_server",
                            type=str,
                            required=True,
                            help="remote ssh server")
    arg_parser.add_argument("-r",
                            "--remote_ssh_user",
                            type=str,
                            required=True,
                            help="remote ssh username")
    arg_parser.add_argument("-c",
                            "--remote_ssh_password",
                            type=str,
                            required=True,
                            help="remote ssh password")
    arg_parser.add_argument("-f",
                            "--remote_ssh_fingerprint",
                            type=str,
                            required=True,
                            help="remote ssh SHA256 fingerprint")
    args = arg_parser.parse_args()
    stub_config = auth.get_session_auth_stub_config(args.user, args.password,
                                                    args.nsx_host,
                                                    args.tcp_port)

    pp = PrettyPrinter()

    # Instantiate all the services we'll need.
    sb_svc = SupportBundles(stub_config)
    cl_node_svc = Nodes(stub_config)

    # Get the UUID of the manager node we're talking to. We'll
    # request a support bundle from it.
    mgr_node = cl_node_svc.get("self")
    mgr_uuid = mgr_node.id
    print(mgr_uuid)

    protocol = SupportBundleFileTransferProtocol(
        name="SCP",
        authentication_scheme=SupportBundleFileTransferAuthenticationScheme(
            scheme_name="PASSWORD",
            username=args.remote_ssh_user,
            password=args.remote_ssh_password),
        ssh_fingerprint=args.remote_ssh_fingerprint)
    rfs = SupportBundleRemoteFileServer(directory_path="/tmp",
                                        server=args.remote_ssh_server,
                                        protocol=protocol)

    sb_request = SupportBundleRequest(log_age_limit=1,
                                      nodes=[mgr_uuid],
                                      remote_file_server=rfs)
    resp = sb_svc.collect(sb_request)
    pp.pprint(resp)
Ejemplo n.º 3
0
def main():
    arg_parser = getargs.get_arg_parser()
    arg_parser.add_argument("-s", "--remote_ssh_server", type=str,
                            required=True, help="remote ssh server")
    arg_parser.add_argument("-w", "--remote_ssh_user", type=str,
                            required=True, help="remote ssh username")
    arg_parser.add_argument("-c", "--remote_ssh_password", type=str,
                            required=True, help="remote ssh password")
    arg_parser.add_argument("-f", "--remote_ssh_fingerprint", type=str,
                            required=True,
                            help="remote ssh SHA256 fingerprint")
    args = arg_parser.parse_args()
    api_client = auth.create_nsx_api_client(args.user, args.password,
                                            args.nsx_host, args.tcp_port,
                                            auth_type=auth.SESSION_AUTH)
    pp = PrettyPrinter()

    # Get the UUID of the manager node we're talking to. We'll
    # request a support bundle from it.
    mgr_node = api_client.cluster.Nodes.get("self")
    mgr_uuid = mgr_node.id
    print(mgr_uuid)

    protocol = SupportBundleFileTransferProtocol(
        name="SCP",
        authentication_scheme=SupportBundleFileTransferAuthenticationScheme(
            scheme_name="PASSWORD",
            username=args.remote_ssh_user,
            password=args.remote_ssh_password
        ),
        ssh_fingerprint=args.remote_ssh_fingerprint
    )
    rfs = SupportBundleRemoteFileServer(
        directory_path="/tmp",
        server=args.remote_ssh_server,
        protocol=protocol
    )

    sb_request = SupportBundleRequest(
        log_age_limit=1,
        nodes=[mgr_uuid],
        remote_file_server=rfs
    )
    try:
        resp = api_client.administration.SupportBundles.collect(sb_request)
        pp.pprint(resp)
    except Error as ex:
        api_error = ex.data.convert_to(ApiError)
        print("An error occurred: %s" % api_error.error_message)
Ejemplo n.º 4
0
def main():
    arg_parser = getargs.get_arg_parser()
    arg_parser.add_argument("-s",
                            "--remote_enforcement_point",
                            type=str,
                            required=True,
                            help="remote enforcement point (nsx-t manager "
                            "ip address or hostname)")
    arg_parser.add_argument("-l",
                            "--remote_login_user",
                            type=str,
                            required=True,
                            help="remote login username")
    arg_parser.add_argument("-c",
                            "--remote_login_password",
                            type=str,
                            required=True,
                            help="remote login password")
    arg_parser.add_argument("-f",
                            "--remote_ssl_thumbprint",
                            type=str,
                            required=True,
                            help="remote ssl SHA256 thumbprint")
    args = arg_parser.parse_args()

    api_client = auth.create_nsx_policy_api_client(args.user,
                                                   args.password,
                                                   args.nsx_host,
                                                   args.tcp_port,
                                                   auth_type=auth.SESSION_AUTH)

    # Create a pretty printer to make the output look nice.
    pp = PrettyPrinter()

    DEFAULT_DZ_ID = "default"
    # Read the default deployment zone
    default_dz = api_client.infra.DeploymentZones.get(DEFAULT_DZ_ID)

    # Print the current enforcement points
    print("Initial enforcement points: ")
    pp.pprint(default_dz.enforcement_points)

    # Register an enforcement point
    conn_info = NSXTConnectionInfo(
        enforcement_point_address=args.remote_enforcement_point,
        thumbprint=args.remote_ssl_thumbprint,
        username=args.remote_login_user,
        password=args.remote_login_password,
    )
    ep = EnforcementPoint(
        description="Example Enforcement Point",
        connection_info=conn_info,
    )

    try:
        api_client.infra.EnforcementPoints.patch(DEFAULT_DZ_ID, "example", ep)
    except Error as ex:
        api_error = ex.data.convert_to(ApiError)
        print("An error occurred: %s" % api_error.error_message)

    eps = api_client.infra.EnforcementPoints.list(DEFAULT_DZ_ID)
    pp.pprint(eps)

    print("You can now examine the enforcement points in the")
    print("NSX policy service if you wish. Press enter to continue.")
    sys.stdin.readline()

    # Delete the enforcement point
    api_client.infra.EnforcementPoints.delete(DEFAULT_DZ_ID, "example")
    print("After deleting enforcement point")
Ejemplo n.º 5
0
def main():
    arg_parser = getargs.get_arg_parser()
    arg_parser.add_argument("-s",
                            "--remote_ssh_server",
                            type=str,
                            required=True,
                            help="remote ssh server")
    arg_parser.add_argument("-w",
                            "--remote_ssh_user",
                            type=str,
                            required=True,
                            help="remote ssh username")
    arg_parser.add_argument("-c",
                            "--remote_ssh_password",
                            type=str,
                            required=True,
                            help="remote ssh password")
    arg_parser.add_argument("-f",
                            "--remote_ssh_fingerprint",
                            type=str,
                            required=True,
                            help="remote ssh SHA256 fingerprint")
    arg_parser.add_argument("-g",
                            "--remote_file_path",
                            type=str,
                            required=True,
                            help="full path name of remote file to copy")
    args = arg_parser.parse_args()
    api_client = auth.create_nsx_api_client(args.user,
                                            args.password,
                                            args.nsx_host,
                                            args.tcp_port,
                                            auth_type=auth.SESSION_AUTH)
    pp = PrettyPrinter()

    local_file_name = os.path.basename(args.remote_file_path)

    # Copy a file from a remote server to the NSX node
    protocol = ScpProtocol(name="scp",
                           authentication_scheme=PasswordAuthenticationScheme(
                               scheme_name="password",
                               username=args.remote_ssh_user,
                               password=args.remote_ssh_password),
                           ssh_fingerprint=args.remote_ssh_fingerprint)
    remote_properties = CopyFromRemoteFileProperties(
        port=22,
        server=args.remote_ssh_server,
        uri=args.remote_file_path,
        protocol=protocol)
    try:
        api_client.node.FileStore.copyfromremotefile(local_file_name,
                                                     remote_properties)
        print("Copied %s from %s to nsx node %s" %
              (local_file_name, args.remote_ssh_server, args.nsx_host))
    except Error as ex:
        api_error = ex.data.convert_to(ApiError)
        print("An error occurred: %s" % api_error.error_message)

    # Then copy that file back from the NSX node
    remote_properties = CopyToRemoteFileProperties(
        port=22,
        server=args.remote_ssh_server,
        uri=args.remote_file_path,
        protocol=protocol)
    try:
        api_client.node.FileStore.copytoremotefile(local_file_name,
                                                   remote_properties)
        print("Copied %s from nsx_node %s to %s" %
              (local_file_name, args.nsx_host, args.remote_ssh_server))
    except Error as ex:
        api_error = ex.data.convert_to(ApiError)
        print("An error occurred: %s" % api_error.error_message)