gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:23:31 25 4
gpt4 key购买 nike

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

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

  2. 然后对其进行 ping 操作,并在确定其是 ESXi 计算机还是 Linux 计算机(redhat/centos/SuSE)后检查 ntp 的当前状态

  3. 然后它将备份/etc/ntp.conf文件,修改所有机器上的/etc/ntp.conf文件以从公共(public)ntp服务器获取时间并重新启动ntp服务

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

  5. 我正在使用 pexpect 和 pxssh 模块来完成此操作,因为我不允许这样做安装 paramiko 或任何其他模块。

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

  7. 我遇到的问题之一是我对所有 ESX 具有 root 访问权限,但受到限制访问具有 SuSE linux 的虚拟机,我必须使用 pexpect/pxssh 但我有学习正则表达式并完成任务很困难。

  8. 有人可以告诉我这是否是正确的方法。我已经粘贴了代码检查 ntp 状态。我有一些更蹩脚的代码,但我没有将其粘贴到这里以防止困惑。

配置文件格式:[主持人]服务器ip1 = 10.10.10.10,用户名,密码1,密码2serverip2 = 10.10.10.11,用户名,密码1,密码2如果用户名不是 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 最近开源了名为 pyvmomi ( http://www.virtuallyghetto.com/2013/12/early-xmas-gift-from-vmware-pyvmomi.html ) 的 Python 绑定(bind) vSphere SDK,这里有一个快速示例,展示了如何为 ESXi 主机启用 NTP 服务: https://gist.github.com/lamw/9065097

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

这是一个快速示例运行:

python start_ntp_sample.py -s vcenter55-1 -u root -p xxxx -x vesxi55-1.primp-industries.com Starting ntpd service on vesxi55-1.primp-industries.com

关于在多个 ESXi 主机和虚拟机上启动 ntp 服务的 python 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21844559/

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