gpt4 book ai didi

python - 通过 PyVim/PyVmomi API 在主机上启用 SSH

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:11 26 4
gpt4 key购买 nike

有没有办法在 ESXi 主机上启用 SSH?我环顾四周,但一无所获。我正在编写一个脚本,需要能够在主机上启用和禁用 SSH。

最佳答案

是的,有多种方法可以在 ESXi 主机上启用 SSH。

使用 Pyvmomi API

from pyVim.connect import SmartConnect, Disconnect
import ssl
import atexit
from pyVmomi import vim


def connect():
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE

si = SmartConnect(host='0.0.0.0', user='user', pwd='pass', port=443, sslContext=context)
atexit.register(Disconnect, si)
content = si.RetrieveContent()
return content

def get_obj(content, vimtype, name):
"""
Return an object by name, if name is None the
first found object is returned
"""
obj = None
container = content.viewManager.CreateContainerView(
content.rootFolder, vimtype, True)
for c in container.view:
if name:
if c.name == name:
obj = c
break
else:
obj = c
break

container.Destroy()
return obj

content = connect()

host_system = get_obj(content, [vim.HostSystem], 'NAME_OF_HOSTSYSTEM')
service_system = host_system.configManager.serviceSystem
ssh_service = [x for x in service_system.serviceInfo.service if x.key == 'TSM-SSH'][0]
if not ssh_service.running:
service_system.Start(ssh_service.key)

# Fun, Profit
service_system.Stop(ssh_service.key) # Stop SSH service.

并使用 Ansible 的 vmware_host_service_manager模块

- name: Start SSH service setting for an ESXi Host in given Cluster
vmware_host_service_manager:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
esxi_hostname: esxi_name
service_name: TSM-SSH
state: present

关于python - 通过 PyVim/PyVmomi API 在主机上启用 SSH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44729507/

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