<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
## Based on:
## console.py
## Author:  James Thiele
## Date:    27 April 2004
## Version: 1.0

import pexpect
import sys
import getpass
import sys
import os
import cmd
import readline

# 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

class Console(cmd.Cmd):

    def __init__(self):
        cmd.Cmd.__init__(self)
        self.prompt = "$ "
	self.default('help')

    ## Command definitions ##
    def do_help(self, args):
    	self.default('help')

    def do_hist(self, args):
        """Print a list of commands that have been entered"""
        print self._hist

    def do_exit(self, args):
        """Exits from the console"""
        return -1

    ## Command definitions to support Cmd object functionality ##
    def do_EOF(self, args):
        """Exit on system end of file character"""
        return self.do_exit(args)

    ## Override methods in Cmd object ##
    def preloop(self):
        """Initialization before prompting user for commands.
           Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
        """
        cmd.Cmd.preloop(self)   ## sets up command completion
        self._hist    = []      ## No history yet
        self._locals  = {}      ## Initialize execution namespace for user
        self._globals = {}

    def postloop(self):
        """Take care of any unfinished business.
           Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
        """
        cmd.Cmd.postloop(self)   ## Clean up command completion
        print "Exiting..."

    def precmd(self, line):
        """ This method is called after the line has been input but before
            it has been interpreted. If you want to modifdy the input line
            before execution (for example, variable substitution) do it here.
        """
        self._hist += [ line.strip() ]
        return line

    def postcmd(self, stop, line):
        """If you want to stop the console, return something that evaluates to true.
           If you want to do some post command processing, do it here.
        """
        return stop

    def emptyline(self):    
        """Do nothing on empty input line"""
        pass

    def default(self, line):       
        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
#        try:
#            exec(line) in self._locals
#	except NameError:
	try:
		child = ssh_command (username, hostname, password, line)
		child.expect(pexpect.EOF)
		print child.before
	except:
		raise
#        except Exception, e:
#            print e.__class__, ":", e

if __name__ == '__main__':
	try:
		hostname = sys.argv[1]
	except:
		try:
			hostname = raw_input('hostname: ')
		except:
			print
			sys.exit(0)

	try:
		username = sys.argv[2]
	except:
		try:
			username = raw_input('username: ')
		except:
			print
			sys.exit(0)

	try:
		password = sys.argv[3]
	except:
		try:
			password = getpass.getpass('password: ')
		except:
			print
			sys.exit(0)

        console = Console()
        console . cmdloop() 
</pre></body></html>