How to get the AHK Visible Window Text from Putty?

2

1

Is there a way to retrieve the contents of the Putty buffer from an AutoHotKey script? For many applications, WinGetText works, but it doesn't seem to for Putty. Also, the AHK Windows Spy doesn't display any text from the buffer, so I suspect that Putty simply uses a different method of storage/display for it's buffer.

I'm wondering if either you can enable a setting in Putty such that it will expose the contents of it's buffer in a Window-standard way that AHK could then pick up as the Visible Window Text.

Alternatively, I suppose there may be some feature of AHK that I'm not aware of that can get that text, but that seems less likely.

Cooper

Posted 2012-08-21T00:00:40.657

Reputation: 365

1What are you trying to do overall? – bobmagoo – 2012-08-21T00:12:17.753

Attachmate Reflections macros can be set up to wait until it sees certain text in the screen before it does a "Send". So if I want to have AHK automatically enter my username at a prompt, I can have the AHK script wait until the "Username:" prompt displays (for, say, a db login). I'm currently using AHK "Send", in combination with sleep,100 to handle the case where a database login prompt is slow to load (I have a db login script set to run from .profile at login). – Cooper – 2012-08-24T20:51:59.807

I mean, overall, what are you trying to accomplish? Automatically connect to a database? Send something? – bobmagoo – 2012-08-25T13:05:45.173

Yes - I want to automatically connect (and login) to a database. This involves navigating a series of text menus as well. – Cooper – 2012-09-06T15:37:47.800

What database are you connecting to? From what? Surely there's an easier way to connect to a database than screen scraping. – bobmagoo – 2012-09-06T16:11:09.997

We have many customers, and each customer has a customized series of text menus that need to be navigated to select the correct database environment (multiple can be hosted on one server). At the end, once you've selected the correct environment, I'd like to enter username and password. These text menus are driven by fairly large perl scripts, which I'd like to avoid pulling apart for each customer just to streamline my logins. – Cooper – 2012-09-10T20:38:49.287

Answers

2

I read a different approach: Have PuTTY write contents to a log file. Then have AutoHotKey read from a file. This may provide the relevant info:

autoputty-scripted-telnet-need-help

Here's another approach:

gist.github.com/serf/2710415

It involves selecting text, which copies it to the clipboard, and then have AutoHotKey read from the clipboard. (It also saves a copy of the clipboard before clobbering the clipboard, and restores the backed up clipboard data).

buixuanlinh.com/blog/?p=80

shows a simple example of logging in with PuTTY.

AutoHotkey:_Launch_putty_or_RDP_sessions_via_a_tree-driven_selection_menu

I've tested zero of these solutions. So, good luck.

If you have access to the server, you may wish to look into doing stuff on the server side. For instance, you can have an SSH key that only permits the user to run one command (or the start of a command, like "sudo mount" but not "sudo anything-else"); sudo can also provide restrictions on what command is used. OpenSSH can allow a single user to have multiple keys. So, choosing which key you use, on the local/client end, could affect that happens when you connect to the server. Details, which will probably be useful for anyone wishing to explore this route, may be found from:

cyberpillar.com/dirsver/1/mainsite/techns/netfeats/users/usrbasop/userauth/authkeyf/authkeyf.htm#sshkycmd

TOOGAM

Posted 2012-08-21T00:00:40.657

Reputation: 12 651

0

I've done this sort of process before using the ImageSearch command: take a screenshot of an empty login prompt, and use ImageSearch to determine if the prompt has been reached. I've found this to be relatively reliable (though by no means perfect) - it may work for you!

For a more reliable system you'd probably need to write a script which does the ssh interaction for you, for example python with pexpect, which I have used successfully. Instructions on how to do that are outside the scope of this question, but depending on what scripting languages you are familiar (other than AutoHotKey) I'm sure there would be an option. I've no experience doing this kind of thing in perl, but it may be possible.

tenorkev

Posted 2012-08-21T00:00:40.657

Reputation: 417