AutoHotkey: Launch a putty session out of an active sftp-plugin connection within TC

From TotalcmdWiki
Revision as of 18:59, 10 September 2008 by TWatcher (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
; AHK script to lauch a putty session out of active sftp-plugin connection within TC
;   using "server" and "user" params (client certificate not supported here)
; version 09.09.2008
; author: TWatcher
; this works only with sftp-plugin provided by Mr. Ghisler
; precondition: putty.exe has to be found within PC's path
; parameter: "-logToFile" will generate some log statements in file TcStrtPty.log, located in same dir as wincmd.ini
; preparation:
;  (1) save this AHK script to a file, e.g. TcStrtPty.ahk
;  (2) compile this AHK script to an exe, execute: "Ahk2Exe.exe /in TcStrtPty.ahk"
;      result should be TcStrtPty.exe
;  (3) Drag TcStrtPty.exe with the mouse to TC's Button bar.
; usage:
;  (4) within TC, within an active SFTP Session (!) just click on "TcStrtPty.exe-Button" in TC's Button bar.


#IfWinExist, ahk_class TTOTAL_CMD

version = 09.09.2008
startprg = putty.exe
SearchStrg = \\\Secure FTP\

if not WinActive( "ahk_class TTOTAL_CMD" )
{
  MsgBox,16,, TC is not active, doing nothing
  return
}

; Read current TC dir from the panel left of the command line
ControlGetText, TCPath, TMyPanel2

DoLogToFile =

if 0 > 0
{
  par1 = %1%
  if ( par1 = "-logToFile" )
  {
    DoLogToFile = True
  }
}

iniPath = %COMMANDER_INI%
StringLen, pos_prev, iniPath
pos_prev += 1
StringGetPos, pos, iniPath, \, R1
pos += 1
StringLeft, iniPath, iniPath, pos

if DoLogToFile = True
{
  LogFile = %iniPath%TcStrtPty.log
  FileDelete, %LogFile%
  Log(A_ScriptName . " : AHK script to lauch a putty session out of active sftp-plugin connection within TC")
  Log("version: " . version)
  Log("Logging to file is active, LogFile: " . LogFile)
}

FormatTime, TimeString
Log("Start " . TimeString)
Log("iniPath: " . iniPath )

Log("Found running TC")
Log("Active TCPath is: '" . TCPath )

; Check whether TCPath starts with SearchStrg
if ( InStr( TCPath, SearchStrg) > 0 )
{
  Log("Found SFTP within path")
  ; Replace SearchStrg
  StringReplace, TCPath, TCPath, %SearchStrg%,
  ; Replace the trailing > with a \
  StringReplace, TCPath, TCPath, >, \
  ; now replace any "\" by   "/"
  StringReplace, TCPath, TCPath, \ , / , all
  ; now find 1st "/"
  StringGetPos, pos, TCPath, /
  if pos >= 0
  {
    ; save startdir
    StringRight, startdir, TCPath, StrLen(TCPath) - pos
    ; and Remove rest
    StringLeft, Host, TCPath, pos
  }
}
Log("Host: '" . Host . "'")

iniFile = %iniPath%sftpplug.ini
;##############################
if Host
{
  Log("SFTP-Ini File is : " . iniFile )
  Gosub, SearchSection
}
else
{
  Log("Did not detect active 'Secure FTP' window in TC")
  MsgBox, 16,,Did not detect active 'Secure FTP' window in TC, can do nothing, aborting now...
}

Log("done...")

exitApp


;##############################

SearchSection:
ArrayCount = 0
SectionFound =
Loop, Read, %iniFile%
{
  ArrayCount += 1  ; Keep track of how many items are in the array.
  Array%ArrayCount% := A_LoopReadLine  ; Store this line in the next array element.
}

; Read from the array:
Loop %ArrayCount%
{
  If not SectionFound
  {
    element := Array%A_Index%
    if ( InStr( element, "[" ) = 1 )
    {
      Log("Found section: " . element)
    }
    IfInString, element, %Host%
     SectionFound := true
  }
  If SectionFound
  {
    Log("--> found Host in charge")
    Indextmp = %A_Index%
    Indextmp += 1
    ItemServer := Array%Indextmp%
    Indextmp += 1
    ItemUser :=  Array%Indextmp%

    StringReplace, ItemServer, ItemServer, server=,
    ; now replace any "\" by   "/"
    StringReplace, ItemServer, ItemServer, \ , / , all

    ; now find 1st "/"
    StringGetPos, pos, ItemServer, /
    if pos >= 0
    {
      ; Remove rest
      StringLeft, ItemServer, ItemServer, pos
    }

    StringReplace, ItemUser, ItemUser, user=,

    Log("server='" . ItemServer . "'  user='" . ItemUser . "'")
    Log("startdir='" . startdir . "'")

    if not ItemServer
    {
      Log("Empty entry for server in section '" . element . "' can do nothing, aborting now..")
      MsgBox,16,, Empty entry for server in section '%element%' can do nothing, aborting now..
      break
    }

    if ItemUser
    {
      Log("Now trying to start putty session to  " . ItemUser . "@" . ItemServer )
      Log("Run, " . startprg . " -ssh " . ItemServer . " -l " . ItemUser )
      Run, %startprg% -ssh %ItemServer% -l %ItemUser%, , UseErrorLevel  ;
      if ErrorLevel = ERROR
      {
        MsgBox,16,, The programm 'putty.exe' was not found or could not be launched`, please check your 'path' settings`!
        ;exit
      }
    }
    else
    {
      Log("Now trying to start putty session to  " . ItemServer )
      Log("Run, " . startprg . " -ssh " . ItemServer )
      Run, %startprg% -ssh %ItemServer% , , UseErrorLevel  ;
      if ErrorLevel = ERROR
      {
        MsgBox,16,, The programm 'putty.exe' was not found or could not be launched`, please check your 'path' settings`!
      }
    }
    break
  }
}
Log("After loop, now return")
return

;##############################
;functions:
;##############################
Log(LogString)
{
  global
  if DoLogToFile = True
  {
    if LogFile
      FileAppend, %LogString% `n,  %LogFile%
  }
}

Back to AutoHotkey