Category Archives: Ubuntu

How to remotely list Vmware VMs using python and turn off SSL certificate verification

Since we are automating a number of things in our environment there was a need to list vms remotely using a script. We tried using PHP or the vmware vi perl toolkit but we couldn’t make any of them work.

The problem with that PHP script we tried to use was that the SSL certificate on the vcenter host doesn’t match the hostname and even though we tried everything it was impossible to turn the SSL certificate verification off. We tried to use this script:

https://gist.github.com/scr34m/3490246

The problem with the VI perl toolkit was that the toolkit is kind of old and requires libraries that are not available any longer. It also requires perl version 5.8 so we had to install an older perl version parallel with the current one. After a day spent trying to get it working we gave it up.

The last option was to use the  VMware vSphere API Python Bindings and the pyvmomi-community-samples.  We did the following:

Determine the python version:

Python 2.7.12 (default, Nov 19 2016, 06:48:10)
 [GCC 5.4.0 20160609] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import sys
 >>> print (sys.version)
 2.7.12 (default, Nov 19 2016, 06:48:10)
 [GCC 5.4.0 20160609]
 >>>

Since Python 2.7 doesn’t have PIP ( Pip Installs Packages ) installed by default we had to get that downloaded and installed first.

Download get-pip.py from here
python get-pip.py

This installs PIP. Now we need to download and install the vmware python extensions.

pip install pyvmomi

We also need to install git if it is not already installed

apt-get install git

Now install the community samples part. The libraries will be installed into the directory you issue this command from.

git clone https://github.com/vmware/pyvmomi-community-samples.git

Locate and run getallvms.py to list all vms from your vcenter. It is very important that you run this script from the community samples location otherwise it will fail with the following error:

root@zoltan-VirtualBox-kk:/app/images/IPM/python# python getallvms.py
 Traceback (most recent call last):
 File "getallvms.py", line 27, in <module>
 import tools.cli as cli
 ImportError: No module named cli

Run the command using the following syntax:

python getallvms.py -s [vcentername]  -u [vmwreuser] -p [vmwarepassword]

If your hostname doesn’t match the certificate you will be getting the error below. If it matches you should be seeing the list of vms now.

Traceback (most recent call last):
 File "getallvms.py", line 104, in <module>
 main()
 File "getallvms.py", line 78, in main
 port=int(args.port),
 File "build/bdist.linux-x86_64/egg/pyVim/connect.py", line 836, in SmartConnect
 File "build/bdist.linux-x86_64/egg/pyVim/connect.py", line 718, in __FindSupportedVersion
 File "build/bdist.linux-x86_64/egg/pyVim/connect.py", line 638, in __GetServiceVersionDescription
 File "build/bdist.linux-x86_64/egg/pyVim/connect.py", line 604, in __GetElementTree
 File "/usr/lib/python2.7/httplib.py", line 1057, in request
 self._send_request(method, url, body, headers)
 File "/usr/lib/python2.7/httplib.py", line 1097, in _send_request
 self.endheaders(body)
 File "/usr/lib/python2.7/httplib.py", line 1053, in endheaders
 self._send_output(message_body)
 File "/usr/lib/python2.7/httplib.py", line 897, in _send_output
 self.send(msg)
 File "/usr/lib/python2.7/httplib.py", line 859, in send
 self.connect()
 File "/usr/lib/python2.7/httplib.py", line 1278, in connect
 server_hostname=server_hostname)
 File "/usr/lib/python2.7/ssl.py", line 353, in wrap_socket
 _context=self)
 File "/usr/lib/python2.7/ssl.py", line 601, in __init__
 self.do_handshake()
 File "/usr/lib/python2.7/ssl.py", line 830, in do_handshake
 self._sslobj.do_handshake()
 ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

There is no way to switch certificate verification off using a command line parameter so the code has to be changed from:

#!/usr/bin/env python
# VMware vSphere Python SDK
# Copyright (c) 2008-2013 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Python program for listing the vms on an ESX / vCenter host
"""

import atexit

from pyVim import connect
from pyVmomi import vmodl
from pyVmomi import vim

import tools.cli as cli


def print_vm_info(virtual_machine):
    """
    Print information for a particular virtual machine or recurse into a
    folder with depth protection
    """
    summary = virtual_machine.summary
    print("Name       : ", summary.config.name)
    print("Template   : ", summary.config.template)
    print("Path       : ", summary.config.vmPathName)
    print("Guest      : ", summary.config.guestFullName)
    print("Instance UUID : ", summary.config.instanceUuid)
    print("Bios UUID     : ", summary.config.uuid)
    annotation = summary.config.annotation
    if annotation:
        print("Annotation : ", annotation)
    print("State      : ", summary.runtime.powerState)
    if summary.guest is not None:
        ip_address = summary.guest.ipAddress
        tools_version = summary.guest.toolsStatus
        if tools_version is not None:
            print("VMware-tools: ", tools_version)
        else:
            print("Vmware-tools: None")
        if ip_address:
            print("IP         : ", ip_address)
        else:
            print("IP         : None")
    if summary.runtime.question is not None:
        print("Question  : ", summary.runtime.question.text)
    print("")


def main():
    """
    Simple command-line program for listing the virtual machines on a system.
    """

    args = cli.get_args()

    try:
        service_instance = connect.SmartConnect(host=args.host,
                                                user=args.user,
                                                pwd=args.password,
                                                port=int(args.port))

        atexit.register(connect.Disconnect, service_instance)

        content = service_instance.RetrieveContent()

        container = content.rootFolder  # starting point to look into
        viewType = [vim.VirtualMachine]  # object types to look for
        recursive = True  # whether we should look into it recursively
        containerView = content.viewManager.CreateContainerView(
            container, viewType, recursive)

        children = containerView.view
        for child in children:
            print_vm_info(child)

    except vmodl.MethodFault as error:
        print("Caught vmodl fault : " + error.msg)
        return -1

    return 0

# Start program
if __name__ == "__main__":
    main()

…to the code below. Changes are highlighted with bold ( might not be easy to see at first )

#!/usr/bin/env python
# VMware vSphere Python SDK
# Copyright (c) 2008-2013 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Python program for listing the vms on an ESX / vCenter host
"""

import atexit
import ssl

from pyVim import connect
from pyVmomi import vmodl
from pyVmomi import vim

import tools.cli as cli


def print_vm_info(virtual_machine):
    """
    Print information for a particular virtual machine or recurse into a
    folder with depth protection
    """
    summary = virtual_machine.summary
    print("Name       : ", summary.config.name)
    print("Template   : ", summary.config.template)
    print("Path       : ", summary.config.vmPathName)
    print("Guest      : ", summary.config.guestFullName)
    print("Instance UUID : ", summary.config.instanceUuid)
    print("Bios UUID     : ", summary.config.uuid)
    annotation = summary.config.annotation
    if annotation:
        print("Annotation : ", annotation)
    print("State      : ", summary.runtime.powerState)
    if summary.guest is not None:
        ip_address = summary.guest.ipAddress
        tools_version = summary.guest.toolsStatus
        if tools_version is not None:
            print("VMware-tools: ", tools_version)
        else:
            print("Vmware-tools: None")
        if ip_address:
            print("IP         : ", ip_address)
        else:
            print("IP         : None")
    if summary.runtime.question is not None:
        print("Question  : ", summary.runtime.question.text)
    print("")


def main():
    """
    Simple command-line program for listing the virtual machines on a system.
    """

    args = cli.get_args()

    try:

        context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        context.verify_mode = ssl.CERT_NONE

        service_instance = connect.SmartConnect(host=args.host,
                                                user=args.user,
                                                pwd=args.password,
                                                port=int(args.port),
                                                sslContext=context
)

        atexit.register(connect.Disconnect, service_instance)

        content = service_instance.RetrieveContent()

        container = content.rootFolder  # starting point to look into
        viewType = [vim.VirtualMachine]  # object types to look for
        recursive = True  # whether we should look into it recursively
        containerView = content.viewManager.CreateContainerView(
            container, viewType, recursive)

        children = containerView.view
        for child in children:
            print_vm_info(child)

    except vmodl.MethodFault as error:
        print("Caught vmodl fault : " + error.msg)
        return -1

    return 0

# Start program
if __name__ == "__main__":
    main()

This solution was based on code displayed in this discussion:

https://github.com/vmware/pyvmomi/issues/235

Slow to receive emails? – Disable Postfix Greylisting

Once you install iRedMail on your own VPS you might encounter issues by mails not being received on time or not being received at all. In my case I just had to disable the Postfix Policyd greylisting. I have seen many reject error messages in /var/www/mail.log :

Jun 11 18:13:55 ProcessingEngine postfix/smtpd[13562]: connect from gateway01.websitewelcome.com[69.41.242.19]
Jun 11 18:13:56 ProcessingEngine postfix-policyd: rcpt=16, greylist=abuse, host=69.41.242.19 (gateway01.websitewelcome.com), from=ORIGIN@EMAILREMOVED.COM, to=DESTINATION@EMAILREMOVED.COM, size=0
Jun 11 18:13:56 ProcessingEngine postfix/smtpd[13562]: NOQUEUE: reject: RCPT from gateway01.websitewelcome.com[69.41.242.19]: 450 4.7.1 : Re cipient address rejected: Policy Rejection- Please try later.; from= to= proto=SMTP helo=
Jun 11 18:13:56 ProcessingEngine postfix/smtpd[13562]: disconnect from gateway01.websitewelcome.com[69.41.242.19]

After a bit of a research I have figured that the postfix-policyd configuration file contains the entry where you can disable greylisting. Once I have done that I have been able to send and receive mails instantly.

The config file on my ubuntu 10.4.2 LTS server is located at:

/etc/postfix-policyd.conf

Just set the GREYLISTING paramter to 0

#####################################################################
#
# enable greylisting                                  default: on
#
#   whether greylisting should be enabled or disabled.
#
#                                                     1=on  0=off
GREYLISTING=0

How to change a wordpress site’s URL

I recently had to copy over to solutioning.eu this blog from blog.solutioning.eu. I read many articles and it was really confusing. Since the database was already on the same server and the username/ password hasn’t changed once I copied all the files the site was running but there were no styles whatsoever so it looked a bit dodgy. Also the wp-admin link wasn’t working. This is due to the fact that wordpress stores the site’s URL in its database ( not like joomla or prestashop which have this defined in a configuration file ).

I could change the URL by simply changing the following in the wordpress database using PhpMyAdmin:
I opened the wp_options table and changed the siteurl and home fields to www.solutioning.eu from blog.solutioning.eu

The website was working again right after this, no hassles, not other configurations…

Gigabyte h55m-ud2h struggles, problems, solutions and that

Installing the Motherboard

I have recently purchased Gigabyte h55m-ud2h motherboard as part of an upgrade kit. First of all the motherboard came with only 2 cables ( IDE, SATA ) so if I wouldn’t have cables like this lying around I wouldn’t have been able to connect my 3 SATA disks. I do not know if this is because it was part of the upgrade kit or the motherboard comes with these 2 cables by default but it is nowhere near sufficent.

The motherboard is very compact, needless to say that I couldn’t properly fit it to my 5 year old large chassis. The holes on the chassis simply were not on the right place for this motherboard… I managed to fit most of the screws though apart from those at the far end of the motherboard. So I had to be extra careful when I installed the memory modules not to break the motherboard. Apart from this problem and that I didn’t find my screwdriver kit for a while 🙂 it was very easy to install.

The Motherboard has the following built in stuff:

Built in Audio

Proper drivers for Windows 7 64bit
Detected by Ubuntu 9.04, 9.10 OK

Built in VGA H55 ( jaysus!!! )

This has been advertised as some superfast video. Well folks .. to put it mildly it is dreadful. Terrible graphics performance, and very ugly rendering. You definitely will need an additional video card if you plan to play games on it. Even World of Warcraft is terribly flickery so I had to set the graphics option way back.

The most annoying thing is that I wasn’t able to video card I have integrated and in fact I haven’t been able to determine it until I installed the drivers that came with the motherbord installation cd.

The videocard shows up in the windows device manager as: Intel (R) Graphics Media Accelerator HD

This actually not saying much, no model number or anything so it will be a bit of a w*nk to get the latest drivers downloaded 🙂

Video Driver Windows 7 64 Bit

The video driver is OK for windows

Video Driver Ubuntu 9.04, 9.10

Update: The driver works perfectly on 10.04 LTS and it installs automatically

Now this is a problem. When I started up Ubuntu 9.04 it didn’t recognize the video and to add a bit more drama to the situation the screen was shifted left on the monitor. I spend numerous hours trying to fix this problem but I never got to fix it on this Ubuntu version.

I tried to remove the previous Nvidia drivers – No Luck
I tried to download new intel drivers – Was already the latest
Upgraded to Kernel 2.6.30 as explained here – No Luck

Finally I decided to stop trying as I read on some forums that Ubuntu 9.10 is running fine this videocard so I kicked off an upgrade.. which to be honest with you was one of the smoothest operating system upgrade I ever had. Needless to say that it wasn’t working correctly on 9.10 neither. In fact the operating system did hang right after login.

Upgraded the kernel to the latest which is 2.6.32 at the time of writing but this again didn’t solve anything it was hanging after log in like kernel 2.6.31.

The lucky thing was that the kernel 2.6.30 was still installed and was working even though the screen was shifted. I logged in to it and removed any trace of any Nvidia product installed over Ubuntu.

After a restart all 3 installed kernels started to work but the sceen was shifted for all of them.. that was better than not being able to log in at all. I have defined the third party auto upgrade option found here then reinstalled some Nvidia stuff ( I was all over the place so I haven’t a clue which ones ) and voila it started to work on kernel 2.6.31, 32… the 2D part 3D part still doesn’t work. As I am not using Ubuntu for gaming at all I decided not to bother with fixing the 3D any longer.

Built in LAN

Detected by Windows 7 OK
Detected by Ubuntu 9.04, 9.10 OK

The bios menu is generally good apart from the fact that I do not see any option to disable the built in VGA… well I hope when I get myself a proper graphics card it will not interfere with it.

The other thing is if you want to boot from an USB key it detects it as a hard drive so when you go to the boot menu and select from the numerous USB options (USB Disk, USB FDA, etc ) none of them will be your USB key. To fix this just go to the Bios and select the USB key from the Hard Drive Priority option ( needs more exact explanation ).

To summarize my experience with h55m-ud2h well after spending a few days on fixing the above errors it works all right. If you have a bunch of cables already you can go ahead and buy it… but do not forget to get a proper graphics card.

Gigabyte H55M-UD2H official page

Ubuntu 9.04 recommended applications

I just installed Ubuntu 9.04 paralell with WinXP.

I found the following software useful to get similar functionalities as you would on windows:

  1. Vuze – Torrent Client to get it: sudo apt-get install vuze
  2. XMMS – Mp3 player. It is a little tricky to get as it has been removed from the Ubuntu software catalog. You can get it though from www.xmms.org
  3. Firestarter – Easy to use firewall comes with a bunch of pre-defined configurations for port. to get it: sudo apt-get install firestarter
  4. aMSN – MSN Client to get it: sudo apt-get install amsn
  5. Skype – Official skype client for Ubuntu to get it: sudo apt-get install skype
  6. Wine – MS Windows emulator for those windows software that you can not find an equivalent for Ubuntu ( WoW for example ). To get it: sudo apt-get install wine If your windows application is not working with the stable version ( 1.0.1 at the time of writing ) You can get the latest development beta from here http://www.winehq.org/download/deb
  7. VLC Media Player – to play videos and music this does download the codecs automatically.
  8. DVD/CD Burner K3b – To burn DVDs and CDs to install sudo apt-get install k3b
  9. Krusaderif you are looking for a total commander equivalent for Ubuntu either you can use Midnight Commander ( mc ) or Krusader. I am not entirely happy with the functionalities but it does the job. To get Krusader type: sudo apt-get install krusader