Skip to content

Instantly share code, notes, and snippets.

@Jamie-
Last active January 7, 2019 14:34
Show Gist options
  • Save Jamie-/c4559c0bfe67d08ab785779cbd35bbbb to your computer and use it in GitHub Desktop.
Save Jamie-/c4559c0bfe67d08ab785779cbd35bbbb to your computer and use it in GitHub Desktop.
Pull version info from an ESXi machine without needing to login.
import re
import argparse
import requests
import urllib3
def get_version_info(ip):
req = '<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><operationID>00000001-00000001</operationID></soap:Header><soap:Body><RetrieveServiceContent xmlns="urn:internalvim25"><_this xsi:type="ManagedObjectReference" type="ServiceInstance">ServiceInstance</_this></RetrieveServiceContent></soap:Body></soap:Envelope>'
resp = requests.post('https://{}/sdk'.format(ip), data=req, verify=False)
if resp.status_code != 200:
raise Exception('Did not get 200 OK back from target, instead got {}'.format(resp.status_code))
check_pattern = re.compile('')
if not check_pattern.match(resp.text):
raise Exception('Did not get expected response from target, unable to parse version info.')
return {
'name': re.search('(?<=<name>).*(?=</name>)', resp.text).group(0),
'full_name': re.search('(?<=<fullName>).*(?=</fullName>)', resp.text).group(0),
'vendor': re.search('(?<=<vendor>).*(?=</vendor>)', resp.text).group(0),
'version': re.search('(?<=<version>).*(?=</version>)', resp.text).group(0),
'build': re.search('(?<=<build>).*(?=</build>)', resp.text).group(0),
'os_type': re.search('(?<=<osType>).*(?=</osType>)', resp.text).group(0),
'product_line': re.search('(?<=<productLineId>).*(?=</productLineId>)', resp.text).group(0),
'api_type': re.search('(?<=<apiType>).*(?=</apiType>)', resp.text).group(0),
'api_version': re.search('(?<=<apiVersion>).*(?=</apiVersion>)', resp.text).group(0)
}
def main():
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
parser = argparse.ArgumentParser('Get verion info from a VMware machine.')
parser.add_argument('ip', help='IP address to probe.')
args = parser.parse_args()
info = get_version_info(args.ip)
for key in info.keys():
print('{}:\t{}'.format(key, info[key]))
if __name__ == '__main__':
main()
@Jamie-
Copy link
Author

Jamie- commented Jan 7, 2019

Can be used as follows:

1

$ python3 esxi_info.py <ip addr>

2

import esxi_info
data = esxi_info.get_version_info(ip)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment