Skip to content

Instantly share code, notes, and snippets.

@serf
Created May 16, 2012 13:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save serf/2710415 to your computer and use it in GitHub Desktop.
Save serf/2710415 to your computer and use it in GitHub Desktop.
AutoHotkey Script: Launch PuTTY with Selected Text
;; 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 ""
}
@JeanCarlosChavarriaHughes

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

@saylor68
Copy link

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