#!/usr/bin/python
# Description:  Lists all guest ldoms with their hostnames configured in the operating system
# Author:       Manuel Zach 
# Contact:	http://blog.zach.st
# Version:      08/08 2011

import subprocess
import re
import telnetlib

def exec_cl(command_line):
    command = subprocess.Popen(command_line, shell=True, stdout=subprocess.PIPE)
    cl_value = command.communicate()[0]
    if len(cl_value)> 1 and cl_value[-1]=="\n":
        cl_value=cl_value[:-1]
    return cl_value

ldm_list_raw = exec_cl("ldm list -p | grep state=active")
consoles = re.findall(r'cons=([0-9]+)', ldm_list_raw)

print "Console: \tHostname of guest ldom"
print "-------- \t----------------------"
for console in consoles:
    tn = telnetlib.Telnet("127.0.0.1",console)

    tn.write("\n\n\n")
    mo = tn.expect([r' (\S*) console login',r'} (ok)'],2)
    tn.close()
    if mo[1]:
        ldom_hostname = mo[1].group(1)
        if ldom_hostname == 'ok':
            print "%s: \t\tOBP" % console
        else:
            print "%s: \t\t%s" % (console,ldom_hostname)


