I am trying to write a script in PHP that resets the root password on an ESXi (“myhost”, moId=”host-15458″) registered to a VCSA version 8.0.2 (“myvCenter”, API-Id=8.0.2.200). I have the old password in $oldpass and the new password in $newpass. The host for my tests is a brand new deployment of a nested ESXi 8.0.2b.
I’ve been on it for several months, already…
I’m not so sure where the problem comes from after trying so many things, including an AI… that kept apologizing.
I’m lost for ideas, anything working in PHP or a web server (Apache) is fine, SOAP or cURL or anything else, PowerCLI and Windows-based stuff are not acceptable. I have the VMWare API for PHP but I don’t understand how to include it and how it works, that might be a solution, although it’s an “old” version for 6.7, it’s just I don’t know how to use it.
I tried with a SOAPClient object, the login is successful, but it seems to timeout immediately, so the Session object is unusable, even for an immediate logout:
/var/www/html/interfaces/RootPasswordChange $ php SOAP.php
login successful
stdClass Object
(
[key] => 52c007b6-208a-8a5b-1e76-955ac6134e59
[userName] => VSPHERE.LOCAL(redacted)
[fullName] => reporter reporter
[loginTime] => 2024-06-28T10:38:18.945751Z
[lastActiveTime] => 2024-06-28T10:38:18.945751Z
[locale] => en
[messageLocale] => en
[extensionSession] =>
[ipAddress] => (redacted)
[userAgent] => PHP-SOAP/7.4.3-4ubuntu2.23
[callCount] => 0
)
attempting logout()
SoapFault exception: [ServerFaultCode] The session is not authenticated. in /var/www/html/interfaces/RootPasswordChange/SOAP.php:52
Stack trace:
#0 /var/www/html/interfaces/RootPasswordChange/SOAP.php(52): SoapClient->__call()
#1 /var/www/html/interfaces/RootPasswordChange/SOAP.php(75): ESXI_SOAP->__construct()
#2 /var/www/html/interfaces/RootPasswordChange/SOAP.php(92): ESXI_Test()
This one was based on a script found on Stack Overflow that exhibits the same symptoms.
I tried through cURL calls, login and logout work, I can fetch the list of hosts and their moId, however I can’t even fetch the API information:
function getApiInfo($curl, $vcs, $session_id) {
curl_setopt_array( $curl ,
[ CURLOPT_URL => "https://" . $vcs . "/api"
, CURLOPT_CUSTOMREQUEST => "GET"
, CURLOPT_RETURNTRANSFER => true
, CURLOPT_HTTPHEADER => [ API_VAR_SESSION . ': ' . $session_id ]
, CURLOPT_SSL_VERIFYPEER => false
, CURLOPT_NOPROXY => "*"
]
) ;
$res = curl_exec( $curl ) ;
print_r( $res ) ;
$inf = curl_getinfo( $curl ) ;
$ret = $inf[ 'http_code' ] ;
return $ret === 200 ? json_decode($response, true) : null;
}
outputs this:
HTTP/2 405
cache-control: must-revalidate,no-cache,no-store
content-type: text/html;charset=iso-8859-1
content-length: 473
x-envoy-upstream-service-time: 0
date: Fri, 28 Jun 2024 10:51:20 GMT
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 405 HTTP method GET is not supported by this URL</title>
</head>
<body><h2>HTTP ERROR 405 HTTP method GET is not supported by this URL</h2>
<table>
<tr><th>URI:</th><td>/api</td></tr>
<tr><th>STATUS:</th><td>405</td></tr>
<tr><th>MESSAGE:</th><td>HTTP method GET is not supported by this URL</td></tr>
<tr><th>SERVLET:</th><td>servlet 4</td></tr>
</table>
</body>
</html>
The Claude 3.5 AI suggested this:
function getHostDetails( $curl , $vcs , $moId , $session_id ) {
$url = 'https://' . $vcs . '/api/vcenter/host/' . $moId ;
echo "$urln";
curl_setopt_array( $curl ,
[ CURLOPT_URL => $url
, CURLOPT_CUSTOMREQUEST => 'GET'
, CURLOPT_RETURNTRANSFER => true
, CURLOPT_SSL_VERIFYPEER => false
, CURLOPT_HEADER => [ API_VAR_SESSION => $session_id ]
]
) ;
$res = curl_exec( $curl ) ;
var_dump( $res ) ;
$ret = curl_getinfo($curl);
return $ret[ 'http_code' ] === 200 ? json_decode($res , true) : null ;
}
but I get:
{"error_type":"NOT_FOUND","messages":[{"args":[],"default_message":"Not found.","id":"com.vmware.vapi.rest.httpNotFound"}]}"
https://myvcenter/api/vcenter/host/host-15458
string(241) "HTTP/2 404
date: Fri, 28 Jun 2024 11:42:45 GMT
content-type: application/json
x-envoy-upstream-service-time: 1