title: python code to start ntp service on multiple ESXi hosts and VMs
body: <p>I have been writing python script to start ntp service on multiple ESXi hosts and VMs and need to know if this approach is right.</p>
  
  <ol>
  <li><p>Script reads the ESXi hosts/Virtual machines ipaddress, username and password from config file.</p></li>
  <li><p>It then pings them and checks the current status of ntp after determining whether it is an ESXi machine or a linux machine(redhat/centos/SuSE)</p></li>
  <li><p>It will then take backup of /etc/ntp.conf file, modify /etc/ntp.conf file on all machines to take time from a common ntp server and restart ntp service</p></li>
  <li><p>For ESXi hosts it will open the firewall port 123 to allow ntp packets.</p></li>
  <li><p>I am using pexpect and pxssh module to accomplish this as I am not allowed to 
  install paramiko or any other module.</p></li>
  <li><p>username could be root or restricted user.</p></li>
  <li><p>One of the problems I have is that I have root's access to all ESXis but restricted 
  access to vm which have SuSE linux for that I have to use pexpect/pxssh but I am having
  a hard time learning the regular expressions and completing the task.</p></li>
  <li><p><strong>Could anyone please let me know if this is the right approach.</strong> I have pasted the code
  which check ntp status. I have some more lame code but am not pasting it here to prevent cluttering.</p></li>
  </ol>
  
  <p>Format of config file:
  [host]
  serverip1 = 10.10.10.10,username,password1,password2
  serverip2 = 10.10.10.11,username,password1,password2 
  if username is not root I need to take the root's password as well to perform administrative commands.</p>
  
  <p>Code:</p>
  
  <pre><code> 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) 
  </code></pre>