<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env python
import pexpect
import sys
import re

# derived from sshls.py

def ssh_command (user, host, password, command):
    """This runs a command on the remote host. This returns a
    pexpect.spawn object. This handles the case when you try
    to connect to a new host and ssh asks you if you want to
    accept the public key fingerprint and continue connecting.
    """
    ssh_newkey = 'Are you sure you want to continue connecting'
    child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
    i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
    if i == 0: # Timeout
        raise
    if i == 1: # SSH does not have the public key. Just accept it.
        child.sendline ('yes')
        child.expect ('password: ')
        i = child.expect([pexpect.TIMEOUT, 'password: '])
        if i == 0: # Timeout
	    raise
    child.sendline(password)
    return child

try:
	child = ssh_command (sys.argv[1], sys.argv[2], sys.argv[3], 'lun show -m')
	child.expect(pexpect.EOF)
	
	try:
		lunmatch = re.compile('%s/%s[ ]+xen[ ]+([0-9]+)[ ]+iSCSI'%(sys.argv[4], sys.argv[5]))
		print int(lunmatch.split( child.before )[1])
	except:
		raise
except:
	print '-1'
</pre></body></html>