public static void main(String[] args) throws Exception { if(args.length!=3) { System.out.println("Usage: java CimTicket <url> " + "<username> <password>"); return; } String urlStr = args[0]; String username = args[1]; String password = args[2]; ServiceInstance si = new ServiceInstance(new URL(urlStr), username, password, true); Folder rootFolder = si.getRootFolder(); HostSystem host = (HostSystem) new InventoryNavigator( rootFolder).searchManagedEntities("HostSystem")[0]; System.out.println(host.getName()); HostServiceTicket ticket = host.acquireCimServicesTicket(); System.out.println("\nHost Name:" + ticket.getHost()); System.out.println("sessionId=" + ticket.getSessionId()); System.out.println("sslThumpprint=" + ticket.getSslThumbprint()); System.out.println("serviceVersion=" + ticket.getServiceVersion()); System.out.println("service=" + ticket.getService()); System.out.println("port=" + ticket.getPort()); retrieveCimInfo(urlStr, ticket.getSessionId()); si.getServerConnection().logout(); }
public WBEMClient client(HostServiceTicket ticket, URL baseUrl, String namespace) { return client(ticket,baseObjectPath(baseUrl,namespace)); }
/** * Creates a web client using the HostServiceTicket object. * <p/> * NOTE: you must be connected to a vCenter for this to make sense * @param ticket granted by vCenter * @param path the initial path for the client to talk to * @return a WBEMClient * @see javax.wbem.client.WBEMClient */ public WBEMClient client(final HostServiceTicket ticket, final CIMObjectPath path) { return client( ticket.getSessionId(), // use sessionId as username ticket.getSessionId(), // use sessionId as password path ); }
/** * creates a web client from a ticket, a URL, and a namespace and classname to query * @param ticket * @param url * @param namespace * @param classname * @return a web client * @see javax.wbem.client.WBEMClient */ public WBEMClient client(final HostServiceTicket ticket, final URL url, final String namespace, final String classname) { return client(subject(ticket), url, namespace, classname); }
/** * A subject is an authentication construct. The vSphere API allows you to obtain a ticket * from the vSphere SDK and use that ticket to construct a subject based on it. * <p/> * @see javax.wbem.client.WBEMClient * @see javax.security.auth.Subject * @return the subject built with the ticket for use in WBEMClient */ public Subject subject(final HostServiceTicket ticket) { return subject(ticket.getSessionId(), ticket.getSessionId()); }
/** * Queries a host system for Cim data. * * @param hostSystem the host system to query * @param cimClass the class of Cim objects to retrieve * @param primaryIpAddress the Ip address to use * @return the list of Cim objects * @throws RemoteException * @throws CIMException */ public List<CIMObject> queryCimObjects(HostSystem hostSystem, String cimClass, String primaryIpAddress) throws ConnectException, RemoteException, CIMException { List<CIMObject> cimObjects = new ArrayList<CIMObject>(); if (!m_hostServiceTickets.containsKey(hostSystem)) { m_hostServiceTickets.put(hostSystem, hostSystem.acquireCimServicesTicket()); } HostServiceTicket hostServiceTicket = m_hostServiceTickets.get(hostSystem); if (!m_hostSystemCimUrls.containsKey(hostSystem)) { String ipAddress = primaryIpAddress; if (ipAddress == null) { ipAddress = getPrimaryHostSystemIpAddress(hostSystem); } if (ipAddress == null) { logger.warn("Cannot determine ip address for host system '{}'", hostSystem.getMOR().getVal()); return cimObjects; } m_hostSystemCimUrls.put(hostSystem, "https://" + ipAddress + ":5989"); } String cimAgentAddress = m_hostSystemCimUrls.get(hostSystem); String namespace = "root/cimv2"; UserPrincipal userPr = new UserPrincipal(hostServiceTicket.getSessionId()); PasswordCredential pwCred = new PasswordCredential(hostServiceTicket.getSessionId().toCharArray()); CIMNameSpace ns = new CIMNameSpace(cimAgentAddress, namespace); CIMClient cimClient = new CIMClient(ns, userPr, pwCred); // very important to query esx5 hosts cimClient.useMPost(false); CIMObjectPath rpCOP = new CIMObjectPath(cimClass); Enumeration<?> rpEnm = cimClient.enumerateInstances(rpCOP); while (rpEnm.hasMoreElements()) { CIMObject rp = (CIMObject) rpEnm.nextElement(); cimObjects.add(rp); } return cimObjects; }