gpt4 book ai didi

java - 使用java访问vcenter时如何解决证书错误?

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:23 25 4
gpt4 key购买 nike

我正在使用 vmware API。通过使用此 API,我能够获取 ESX 设备的信息。现在我想使用此 API 获取有关 vcenter 的信息,但出现异常“Java.rmi.remoteException:VI SDK 调用异常:javax.net.ssl.SSLHandShakeException:java.security。”

代码如下:

public void realesx(){
System.out.println("Running ESX Realtime for host ..."+host);
JSONObject esxcmdout = new JSONObject();
String url = "https://" + host + "/sdk/vimService";
try {
ServiceInstance si = new ServiceInstance(new URL(url), user, pass,true);
System.out.println("host :"+host+"---"+si.getAboutInfo().getFullName());

System.out.println(" Version is .. " +si.getAboutInfo().version);
System.out.println(" os type is .. " +si.getAboutInfo().osType);
System.out.println("Vendor is .. " + si.getAboutInfo().vendor);
System.out.println("name is" + si.getAboutInfo().name);
try{
esxcmdout.put("vmWayerVersion", si.getAboutInfo().version);
esxcmdout.put("vmWayerOSType", si.getAboutInfo().osType);
esxcmdout.put("vmWayerVendor", si.getAboutInfo().vendor);
esxcmdout.put("vmWayerName", si.getpublic void realesx(){
System.out.println("Running ESX Realtime for host ..."+host);
JSONObject esxcmdout = new JSONObject();
String url = "https://" + host + "/sdk/vimService";
try {
ServiceInstance si = new ServiceInstance(new URL(url), user, pass,true);
System.out.println("host :"+host+"---"+si.getAboutInfo().getFullName());

System.out.println(" Version is .. " +si.getAboutInfo().version);
System.out.println(" os type is .. " +si.getAboutInfo().osType);
System.out.println("Vendor is .. " + si.getAboutInfo().vendor);
System.out.println("name is" + si.getAboutInfo().name);
try{
esxcmdout.put("vmWayerVersion", si.getAboutInfo().version);
esxcmdout.put("vmWayerOSType", si.getAboutInfo().osType);
esxcmdout.put("vmWayerVendor", si.getAboutInfo().vendor);
esxcmdout.put("vmWayerName", si.getAboutInfo().name);
}
catch (Exception e){
e.printStackTrace();
}

ManagedEntity[] managedEntities = new InventoryNavigator(
si.getRootFolder()).searchManagedEntities("VirtualMachine");
ManagedEntity[] hostmanagedEntities = new InventoryNavigator(
si.getRootFolder()).searchManagedEntities("HostSystem");

for (ManagedEntity hostmanagedEntity : hostmanagedEntities) {
HostSystem hostsys = (HostSystem) hostmanagedEntity;
String ESXhostname = hostsys.getName();
//System.out.println("main system version is .. " + hostsys.getConfig());
HostListSummary hls = hostsys.getSummary();
HostHardwareSummary hosthwi = hls.getHardware();
HostListSummaryQuickStats hqs = hls.getQuickStats();
Datastore[] HDS = hostsys.getDatastores();
StringBuilder DS = new StringBuilder();
for (int i=0;i <HDS.length;i++){
DatastoreSummary dsm =HDS[i].getSummary();

DS.append(dsm.name+":"+dsm.capacity+":"+dsm.freeSpace+"-");
}

int MEM=hqs.overallMemoryUsage;
int UPT=hqs.getUptime();
Integer CPU=hqs.getOverallCpuUsage();

String esxkey = "ESXRealInfo";
String esxvalue = "ESXhostname-" + ESXhostname
+ ";CPU Usage-" + CPU + ";MEM Usage-"
+ MEM + ";UPTIME-" + UPT+"; Datastores -"+DS;
try {
esxcmdout.put(esxkey, esxvalue);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

for (int i = 0; i < managedEntities.length; i++) {
VirtualMachine vm = (VirtualMachine) managedEntities[i];
String vmName = vm.getName();

String vmIP = vm.getGuest().getIpAddress();
VirtualMachineConfigInfo config = vm.getConfig();
VirtualHardware hw = config.getHardware();
String vmVersion = config.version;
System.out.println("######### vm version is ###### ... "+ vmVersion);
int vmCPU = hw.getNumCPU();
int vmMem = hw.getMemoryMB();
String vmkey = "vm" + i;
String vmvalues = "Name-" + vmName + ";IP-" + vmIP + ";vmCPU-"
+ vmCPU + ";vmMem-" + vmMem + ";vmVersion-" + vmVersion;
System.out.println("string to write is... "+vmvalues);
try {
esxcmdout.put(vmkey, vmvalues);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
si.getServerConnection().logout();
}

catch (InvalidProperty e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RuntimeFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
runMT.httpput(runtype, host, vmwtype, esxcmdout);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}AboutInfo().name);
}
catch (Exception e){
e.printStackTrace();
}

ManagedEntity[] managedEntities = new InventoryNavigator(
si.getRootFolder()).searchManagedEntities("VirtualMachine");
ManagedEntity[] hostmanagedEntities = new InventoryNavigator(
si.getRootFolder()).searchManagedEntities("HostSystem");

for (ManagedEntity hostmanagedEntity : hostmanagedEntities) {
HostSystem hostsys = (HostSystem) hostmanagedEntity;

String ESXhostname = hostsys.getName();
//System.out.println("main system version is .. " + hostsys.getConfig());

HostListSummary hls = hostsys.getSummary();
HostHardwareSummary hosthwi = hls.getHardware();
HostListSummaryQuickStats hqs = hls.getQuickStats();
Datastore[] HDS = hostsys.getDatastores();
StringBuilder DS = new StringBuilder();
for (int i=0;i <HDS.length;i++){
DatastoreSummary dsm =HDS[i].getSummary();

DS.append(dsm.name+":"+dsm.capacity+":"+dsm.freeSpace+"-");
}

int MEM=hqs.overallMemoryUsage;
int UPT=hqs.getUptime();
Integer CPU=hqs.getOverallCpuUsage();

String esxkey = "ESXRealInfo";
String esxvalue = "ESXhostname-" + ESXhostname
+ ";CPU Usage-" + CPU + ";MEM Usage-"
+ MEM + ";UPTIME-" + UPT+"; Datastores -"+DS;
try {
esxcmdout.put(esxkey, esxvalue);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

for (int i = 0; i < managedEntities.length; i++) {
VirtualMachine vm = (VirtualMachine) managedEntities[i];
String vmName = vm.getName();
String vmIP = vm.getGuest().getIpAddress();
VirtualMachineConfigInfo config = vm.getConfig();
VirtualHardware hw = config.getHardware();
String vmVersion = config.version;
System.out.println("######### vm version is ###### ... "+ vmVersion);
int vmCPU = hw.getNumCPU();
int vmMem = hw.getMemoryMB();
//sasSystem.out.println(vmName + vmIP + vmCPU + vmMem);
String vmkey = "vm" + i;
String vmvalues = "Name-" + vmName + ";IP-" + vmIP + ";vmCPU-"
+ vmCPU + ";vmMem-" + vmMem + ";vmVersion-" + vmVersion;
System.out.println("string to write is... "+vmvalues);
try {
esxcmdout.put(vmkey, vmvalues);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
si.getServerConnection().logout();
}

catch (InvalidProperty e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RuntimeFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
runMT.httpput(runtype, host, vmwtype, esxcmdout);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

此代码可以很好地收集 ESX 设备的信息。如何解决 vcenter 证书错误?有没有其他方法可以做到这一点?

最佳答案

您必须导入证书,所以通过这个 vSphere_Documentation了解进口证书的页面。

编辑:

试试下面给出的代码,看看这个 answer

KeyStore keyStore = KeyStore.getInstance("JKS");
String fileName = "D:\\certs_path\\cacerts"; // cerrtification file path
System.setProperty("javax.net.ssl.trustStore", fileName);

关于java - 使用java访问vcenter时如何解决证书错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21153758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com