Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4802

Change password on ESXi with REST API

$
0
0

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, here's the code:

class ESXI_SOAP extends SoapClient { private $ServiceInstance; private $session; private function getServiceInstance() {  return $this->RetrieveServiceContent( [ "_this" => new SoapVar( "ServiceInstance", XSD_STRING , "ServiceInstance" ) ] )->returnval ;}private function getSession($ServiceInstance,$user,$pass) {  return $this->Login(   [ '_this'    => $ServiceInstance->sessionManager   , 'userName' => $user   , 'password' => $pass   ] )->returnval ;} public function __construct( $url , $user , $pass ) { try {   parent::__construct( $url . '/vimService.wsdl' ,    [ 'trace' => true    , 'exceptions' => true    , "location" => $url    , 'stream_context' => stream_context_create(     [ 'ssl' =>      [ 'verify_peer'       => false      , 'verify_peer_name'  => false      , 'allow_self_signed' => true      ]     ]    )    ]   ) ; // parent->__construct()   $this->ServiceInstance = $this->RetrieveServiceContent(    [ "_this" => new SoapVar( "ServiceInstance", XSD_STRING , "ServiceInstance" )    ] )->returnval ;   $this->session = $this->getSession( $this->ServiceInstance , $user , $pass ) ;   echo "login successful\n" ;   print_r( $this->session ) ;//   $this->__setCookie("vmware-api-session-id",$this->session->key);  } catch( Exception $e ) {   printf("%sn",$e->__toString() ) ;   return false ;  }  /*** Logout ***/  try{   echo "attempting logout()\n";   $result = $this->Logout(    [ "_this" => $this->ServiceInstance    , "key" => $this->session->key    ]   ) ;  } catch (Exception $e) { printf("%sn",$e->__toString()); return false; }  echo "logout successful\n";}

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.phplogin successfulstdClass 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:52Stack 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 405cache-control: must-revalidate,no-cache,no-storecontent-type: text/html;charset=iso-8859-1content-length: 473x-envoy-upstream-service-time: 0date: 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>

Viewing all articles
Browse latest Browse all 4802

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>