Created
May 16, 2012 13:41
-
-
Save serf/2710415 to your computer and use it in GitHub Desktop.
AutoHotkey Script: Launch PuTTY with Selected Text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Start SSH session using the currently selected text as the target hostname. | |
$#S:: | |
; Putty copies selected text to the clipboard so you don't need to copy it | |
; doing Ctrl-Insert throws away what you already have in the Clipboard. | |
; Can't use Ctrl-C in putty, because it sends that to your session as ^C | |
WinGet, Active_ID, ID, A | |
WinGet, Active_Process, ProcessName, ahk_id %Active_ID% | |
if ( Active_Process ="putty.exe" ) | |
{ | |
host = %Clipboard% | |
} | |
else | |
{ | |
host_tmp := SelectedViaClipboard() | |
host := ValidateHostname(host_tmp) | |
} | |
title_msg := "Please enter the hostname" | |
prompt_msg := "Didn't get host from selected text:" | |
if (!host) | |
{ | |
InputBox, new_host, %title_msg%, %prompt_msg%,, 220, 111, , , , , %host_tmp% | |
if (ErrorLevel) | |
{ | |
return | |
} | |
else | |
{ | |
host := ValidateHostname(new_host) | |
} | |
} | |
if (host) | |
{ | |
; MsgBox, Doing ssh %host% | |
Run "C:\Program Files\PuTTY\putty.exe" "-ssh" "%host%" | |
; NB: You could add your username as "user@%host%" | |
} | |
else | |
{ | |
MsgBox, %prompt_msg% [%host_tmp%] | |
} | |
return | |
;; utility functions | |
;; Get selected text without clobbering Clipboard | |
SelectedViaClipboard() | |
{ | |
old_clipboard = %ClipboardAll% ; save current Clipboard | |
Clipboard := "" ; clears Clipboard | |
Send, ^{Insert} | |
selection = %Clipboard% ; save the content of the clipboard | |
Clipboard = %old_clipboard% ; restore old content of the clipboard | |
return selection | |
} | |
;; Capture a hostname out of some selected text | |
ValidateHostname(str) | |
{ | |
if (RegExMatch(str, "^([ \'\[@]*)([\w\-\.]+)([ \'\]:/]*)$", match)) | |
return match2 | |
return "" | |
} |
digging up a dead thread, but it came to me in a related search - i just did exactly this @JeanCarlosChavarriaHughes it will copy any double click word to a temp file and any double click inside the temp file will paste to putty.
https://github.com/saylor68/puttyCLIP/tree/master
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this code.
Is any way to send a command to a already active putty session?
I need users to manually open a putty session and then use autohotkey to send commands