import pexpect, os, fnmatch

#
# Utils
# author  Maurizio Nagni
# version 1.0 - 11 March 2006
#
#
#

class SSH:
    #def __init__(self):
        
	
    def scp(self, command, password, tmout=45):
        try:
                child = pexpect.spawn("scp " + command, timeout=tmout)
                child.expect('.*word:')
                child.sendline(password)
                child.expect(pexpect.EOF)
		return child.before.strip()
        except pexpect.TIMEOUT:
                msg = "scp %s \n TIMEOUT ERROR, unable to connect." % (command)
		raise Exception(msg)
        except pexpect.EOF: 
                msg = "scp %s \n BAD HOST NAME ERROR, unable to connect." % (command)
		raise Exception(msg)

    def ssh(self, command, password, whatExpect = pexpect.EOF, tmout=45):
        try:
                child = pexpect.spawn("ssh " + command, timeout=tmout)
                child.expect('.*word:')
                child.sendline(password)
                child.expect(whatExpect)
		return True
        except pexpect.TIMEOUT:
                msg = "ssh %s \n TIMEOUT ERROR, unable to connect." % (command)
		raise Exception(msg)
        except pexpect.EOF: 
                msg = "ssh %s \n BAD HOST NAME ERROR, unable to connect." % (command)
		raise Exception(msg)

