8-02 16 views
1.背景
最近在研究ESXi的API,另外因本人是一个pythoner,也在负责公司运维平台的开发工作,看着系统那边的小伙伴在一台一台的手工去开启ESXi的SSH服务(其实在批量安装系统时可以开启,当然如果会powercli也能做到),着实于心不忍,就写了这个脚本。也是想后期可以将这些功能整合到运维平台上去。当然这个不单单是只能开启SSH服务。
2.准备工作
2.1环境准备
1 2 |
Python:3.x dependents: pyVmomi,pyVim |
2.2代码
esxi_ssh_enable.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Author : Eric Winn # @Email : eng.eric.winn@gmail.com # @Time : 8/1/2018 19:04 # @Version : 1.0 # @File : esxi_ssh_enable.py # @Software : PyCharm import argparse import time from pyVim import connect import ssl import atexit from pyVmomi import vim def help_parser(): """ Builds a standard argument parser with arguments for talking to vCenter -s service_host_name_or_ip -o optional_port_number -u required_user -p optional_password """ parser = argparse.ArgumentParser( description='Standard Arguments for talking to vCenter or ESXi') parser.add_argument('-s', '--host', required=True, action='store', help='vSphere service to connect to') parser.add_argument('-o', '--port', type=int, default=443, action='store', help='Port to connect on') parser.add_argument('-u', '--user', required=True, action='store', help='User name to use when connecting to host') parser.add_argument('-p', '--password', required=True, action='store', help='Password to use when connecting to host') return parser def makeConnect(parser): context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) context.verify_mode = ssl.CERT_NONE # connect to vCenter or ESXi si = connect.SmartConnect( host=parser.host, user=parser.user, pwd=parser.password, port=parser.port, sslContext=context) atexit.register(connect.Disconnect, si) content = si.RetrieveContent() return content def get_obj(content, vimtype): """ Return an object by view """ container = content.viewManager.CreateContainerView( content.rootFolder, vimtype, True) obj = container.view container.Destroy() return obj # Start service def set_services(host_system): service_system = host_system.configManager.serviceSystem # find SSH service ssh_service = [x for x in service_system.serviceInfo.service if x.key == 'TSM-SSH'][0] if not ssh_service.running: print('Starting SSH ...') service_system.Start(ssh_service.key) # start and shutdown with host,default is off service_system.UpdateServicePolicy(id=ssh_service.key, policy='on') print('successfully...') else: print('Already Started ...') return ssh_service if __name__ == '__main__': parser = help_parser() parser = parser.parse_args() content = makeConnect(parser) host_system_list = get_obj(content, [vim.HostSystem]) for host_system in host_system_list: print('========> {}'.format(host_system.name)) set_services(host_system) |
3.执行
1 |
python3 esxi_ssh_enable.py -s vcenter_or_esxi_host -u user -p password |
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!