用于在多个ESXi主机和虚拟机上启动ntp服务的python代码



我一直在编写python脚本,以便在多个ESXi主机和虚拟机上启动ntp服务,需要知道这种方法是否正确。

  1. 脚本从配置文件中读取ESXi主机/虚拟机的IP地址、用户名和密码。

  2. 然后,它对它们进行ping,并在确定ntp是ESXi机器还是linux机器(redhat/centros/SuSE)后检查其当前状态

  3. 然后,它将备份/etc/ntp.conf文件,在所有机器上修改/etc/ntp.onf文件,以便从通用ntp服务器上花费时间,并重新启动ntp服务

  4. 对于ESXi主机,它将打开防火墙端口123以允许ntp数据包。

  5. 我使用pexpect和pxssh模块来实现这一点,因为我不被允许安装paramiko或任何其他模块。

  6. 用户名可以是root用户或受限用户。

  7. 我遇到的一个问题是,我拥有root用户对所有ESXi的访问权限,但受到限制访问具有SuSE linux的vm我必须使用pexpect/pxssh,但我有学习正则表达式和完成任务的困难。

  8. 有人能告诉我这是否是正确的方法吗我已经粘贴了代码其检查ntp状态。我有一些更蹩脚的代码,但为了防止混乱,我不会把它粘贴在这里。

配置文件的格式:[主机]serverip1=10.10.10.10,用户名,密码1,密码2serverip2=10.10.10.11,用户名,密码1,密码2如果username不是root,我还需要使用root的密码来执行管理命令。

代码:

def checkntpstatus(hostlist, logger):
"""This function checks the ntp status
"""
for host in hostlist:
try:
ssh = pxssh.pxssh()
ssh.login(host.ipaddress, host.username, host.password1)
if host.ostype == 'esxlinux':
ssh.sendline('/etc/init.d/ntpd status')
ssh.prompt()
logger.info(host.ipaddress + " : ")
logger.info(ssh.before)
elif host.ostype == 'suse' and host.username == 'root':
ssh.sendline('service ntp status')
ssh.prompt()
logger.info(host.ipaddress + " : ")
logger.info(ssh.before)
elif host.ostype == 'suse' and host.username == 'restricted':
ssh.sendline('sudo service ntp status')
response = ssh.expect(r'(?i)password:')
logger.info("Using root's password for : " + 
host.ipaddress)
ssh.sendline(host.password2)
ssh.prompt()
logger.info(host.ipaddress + " : ")
logger.info(ssh.before)
except pxssh.ExceptionPxssh as exception:
logger.info("Failed to ssh or" +
" match login prompt " +
host.ipaddress + " " + str(exception))
finally:
ssh.logout()

def findostype(hostlist, logger):
"""ping the host and findout the operating system type
"""
for host in hostlist:
if not pinghost(host.ipaddress, logger):
logger.info("probing host : " + host.ipaddress)
try:
ssh = pxssh.pxssh()
ssh.login(host.ipaddress, host.username, host.password1)
ssh.sendline("vmware -v")
ssh.prompt()
regexp1 = re.compile(r'command not found')
regexp2 = re.compile(r'(?i)suse')
regexp3 = re.compile(r'(?i)redhat')
regexp4 = re.compile(r'(?i)centos')
if regexp1.search(ssh.before) == None:
host.ostype = 'esxlinux'
else:
ssh.sendline('python -c "import platform; print 
platform.dist()"')
ssh.prompt()
if regexp2.search(ssh.before):
host.ostype = 'suse'
elif regexp3.search(ssh.before):
host.ostype = 'redhat'
elif regexp4.search(ssh.before):
host.ostype = 'centos'
except pxssh.ExceptionPxssh as exception:
logger.info("Failed to ssh or" + 
" match login prompt " + 
host.ipaddress + " " + str(exception))
finally:
ssh.logout()
else:
logger.info("Failed to ping host : " + host.ipaddress) 

作为SSH的替代选项(因为并非总是为所有人启用),您可以利用vSphere API以编程方式配置和启动/停止NTP。VMware最近为Python绑定开源了vSphere SDK,称为pyvmomi(http://www.virtuallyghetto.com/2013/12/early-xmas-gift-from-vmware-pyvmomi.html)下面是一个快速示例,显示如何为ESXi主机启用NTP服务:https://gist.github.com/lamw/9065097

该示例假设您指向vCenter Server,但可以修改为直接指定到ESXi主机(假设它不是免费版)

下面是一个快速的样本运行:

python start_ntp_sample.py-s vcenter55-1-u root-p xxxx-xvesxi55-1.primp-industries.com正在上启动ntpd服务vesxi55-1.primp-industries.com

最新更新