From: Rasjid W. <ra...@op...> - 2003-05-02 10:39:21
|
For the record, David's solution is exactly what I created the prompt_callback parameter for, and is certainly the kind of solution I had in mind. My appologies to Christian, as I think I simply missed that part his email when reading it, and thank you to David for a solution. Cheers, Rasjid. On Friday 02 May 2003 19:16, David Schachter wrote: > In <http://sourceforge.net/mailarchive/forum.php?thread_id=1560324&forum_id=705>, Christian Laubscher asked: > >Another question (I know that this is bad style, but I'd like to do it > > anyway): can I hand ssh the password as a variable? How? > > I did this by passing a different function into Ssh.login(). The function > is defined like this: def login(self, logintext='Last login:', > prompt_callback=_prompt) > > where _prompt is a helper function to handle the login dialog. I provided > my own function, myPrompt(), using the _prompt() function as a starting > point. Here's an example program, run on Microsoft Windows 2000, based on > Christian's example: import pyssh > import sys > > if len(sys.argv) != 4: > print """Usage: example2 <hostname> <username> <password> > > Example: example2 fred.acme.com zaphod beeblebrox""" > sys.exit(1) > > hostG = sys.argv[1] > userG = sys.argv[2] > passwordG = sys.argv[3] > > > def myPrompt(prompt): > """Print the message as the prompt for input. Return the text entered.""" > > abort = 0 > > lprompt = prompt.lower() > if lprompt.find("password:") >= 0 or lprompt.find("passphrase:") >= 0: > # print "Found password request in '%s'." % prompt > response = passwordG > > else: > print "User input required for ssh connection. (Type Ctrl-C to abort > connection.)" try: > response = raw_input(prompt) > > except KeyboardInterrupt: > response = "" > abort = 1 > > return response, abort > > > mycon = pyssh.Ssh(userG, hostG, "22") > mycon.set_debuglevel(1) > mycon.set_sshpath("c:/progra~1/putty/plink.exe") > mycon.login("Last login:", myPrompt) > > for command in ("ls", "ps", "ps auxw | grep httpd | grep -v grep ; echo > $?", "exit"): print "\n%%", command > print mycon.sendcmd(command) > mycon.close() -- Rasjid Wilcox Canberra, Australia UTC + 10 http://www.openminddev.net |