Difference between revisions of "AutoHotkey: Launch putty or RDP sessions via a tree-driven selection menu"

From TotalcmdWiki
Jump to: navigation, search
 
Line 5: Line 5:
 
AIX admins may recognize the portion of the script which allows you to organize server partitions by frame name rather than by application description.  If anybody needs the AIX / HMC script that I use to generate the list of partitions, contact me directly. This is a semi-manually maintained portion of the script, the list is generated on AIX and then copied and pasted into the .ahk script (or FTP if you have so many servers that a cut & paste is painful).  I haven't gotten around to making the AIX script generate the entire .ahk script so that I can simply sftp it to my workstation, but one day...
 
AIX admins may recognize the portion of the script which allows you to organize server partitions by frame name rather than by application description.  If anybody needs the AIX / HMC script that I use to generate the list of partitions, contact me directly. This is a semi-manually maintained portion of the script, the list is generated on AIX and then copied and pasted into the .ahk script (or FTP if you have so many servers that a cut & paste is painful).  I haven't gotten around to making the AIX script generate the entire .ahk script so that I can simply sftp it to my workstation, but one day...
  
You can actually extend this to enable putty to launch serial connections to LPARs also, I have that portion done as well... the only downside is that I have not (as yet) make it close the terminal connection cleanly so I haven't included that functionality on this page as yet.
+
You can actually extend this to enable putty to launch serial connections to AIX LPARs and I have that portion done as well.  
 +
The only downside is that it does not close the terminal connection on the HMC cleanly so I haven't included that functionality on this page as yet.  When (if?) I eventually get the chance to fix this bug / issue I will post the updated version.
  
I had to sanitize hostnames and such from the script, it's possible that I have mangled it a bit in the process.  If so, please forgive me for the oversight.  I have tested the version reproduced below and it seems to work.
+
I had to sanitize hostnames and such from the script, it's possible that I have mangled things a bit in the process.  If so, please forgive me for the oversight.  I have tested the version reproduced below and it seems to work. Hope somebody finds this script useful, if so, please let me know that my efforts didn't go to waste!
  
 
<pre>
 
<pre>

Revision as of 23:07, 11 March 2009

The following script is useful for UNIX admins that need to administer hundreds of UNIX servers. This script makes some assumptions, one is that there is a consistent naming convention for logically separating servers. Without too much modification it will allow you to launch any Windows-based client program, but it is really useful for when you have dozens to hundreds of things to manage that use a common client program.

One of our application admins also needs to do RDP (windows remote desktop protocol) sessions to windows servers to administer some stuff, so I extended it to permit him to launch rdp to a specific server. I can't verify that the RDP portion works, because happily I don't need to deal with Windows as a server, I only need to use one as my work-provided desktop. I would imagine it could be used to also launch Windows-based database client software to a specific database or something of that nature.

AIX admins may recognize the portion of the script which allows you to organize server partitions by frame name rather than by application description. If anybody needs the AIX / HMC script that I use to generate the list of partitions, contact me directly. This is a semi-manually maintained portion of the script, the list is generated on AIX and then copied and pasted into the .ahk script (or FTP if you have so many servers that a cut & paste is painful). I haven't gotten around to making the AIX script generate the entire .ahk script so that I can simply sftp it to my workstation, but one day...

You can actually extend this to enable putty to launch serial connections to AIX LPARs and I have that portion done as well. The only downside is that it does not close the terminal connection on the HMC cleanly so I haven't included that functionality on this page as yet. When (if?) I eventually get the chance to fix this bug / issue I will post the updated version.

I had to sanitize hostnames and such from the script, it's possible that I have mangled things a bit in the process. If so, please forgive me for the oversight. I have tested the version reproduced below and it seems to work. Hope somebody finds this script useful, if so, please let me know that my efforts didn't go to waste!

FileInstall, globe_2b.jpg, globe_2b.jpg, 1

;================================================================================================================================
; First section simply checks if conf file for the utility exists and reads it's contents into variables
; If it doesn't exist it prompts users for the information and writes the values into the conf file
;================================================================================================================================

PuttyFound = false
RDPFound = false

IfExist, %A_WorkingDir%\puttytree_conf.txt
{
   PuttyFound = true
   FileReadLine, PuttyLoc, puttytree_conf.txt, 1
   FileReadLine, PrsnId, puttytree_conf.txt, 2
}

if PuttyFound != true
{
    MsgBox Please indicate location of Putty or program will not function correctly
    FileSelectFile, PuttyLoc, 1, %A_MyDocument%, Please indicate location of Putty or program will not function correctly, *.exe
    FileAppend, %PuttyLoc%`n, puttytree_conf.txt
    InputBox, vPrsnId, Please enter your AIX userid
    FileAppend, %vPrsnId%, puttytree_conf.txt
    MsgBox The conf file with the location of putty and your userid has now been created.`n`nThe name of the file is puttytree_conf.txt and 

it can be found in the same directory as this utility.`n`nThe utility will now exit - please rerun it and it should no longer ask for the 

putty location or your userid.`nThis should also be the last time you see this message.
    ExitApp
}

IfExist, %A_WorkingDir%\RDPtree_conf.txt
{
   RDPFound = true
   FileReadLine, RDPLoc, RDPtree_conf.txt, 1
   FileReadLine, RrsnId, RDPtree_conf.txt, 2
   ;MsgBox Found the conf file so value of PuttyFound is %RDPFound%
   ;MsgBox Putty location is %RDPLoc% User ID is %RrsnId%
}

if RDPFound != true
{
    MsgBox Please indicate location of AutoRDP or program will not function correctly
    FileSelectFile, RDPLoc, 1, %A_MyDocument%, Please indicate location of AutoRDP or program will not function correctly, *.exe
    FileAppend, %RDPLoc%`n, RDPtree_conf.txt
    InputBox, vRrsnId, Please enter your wintel domain userid
    FileAppend, %vRrsnId%, RDPtree_conf.txt
    MsgBox The conf file with the location of putty and your userid has now been created.`n`nThe name of the file is RDPtree_conf.txt and it 

can be found in the same directory as this utility.`n`nThe utility will now exit - please rerun it and it should no longer ask for the putty 

location or your userid.`nThis should also be the last time you see this message.
    ExitApp
}

;================================================================================================================================

; Now to begin creating the actual GUI that the user will interact with

Gui, +Resize

Gui, Add, Picture,, %A_WorkingDir%\globe_2b.jpg

Gui, Font, s10, Lucida Console

Gui, Add, TreeView, ym r42 w500 Background33669F cWhite gSvr4Putty

svrs_by_name := TV_Add("Servers by name")
svrs_by_frame := TV_Add("Servers by frame")
rdp_svrs := TV_Add("Available RDP Sessions")

dev_svrs := TV_Add("DEV Servers", svrs_by_name)
qat_svrs := TV_Add("QAT Servers", svrs_by_name)
prd_svrs := TV_Add("PRD Servers", svrs_by_name)

;                                                             ------ This is the value that will be used by putty / RDP
;                                                             |
;                                                             \/

 PRD001 := TV_Add("Production WebServer #1 Application FOO : PRD001", prd_svrs)  ; parent is prd_svrs
 PRD002 := TV_Add("Production AppServer #1 Application BAR : PRD002", prd_svrs)  ; parent is prd_svrs
 QAT001 := TV_Add("QA WebServer #1  Application FOOBAR     : QAT001", qat_svrs)  ; parent is qat_svrs
 QAT002 := TV_Add("QA AppServer #1  Application REBAR      : QAT002", qat_svrs)  ; parent is qat_svrs
 DEV001 := TV_Add("DEV WebServer #1 Application WebWhiz    : DEV001", dev_svrs)  ; parent is qat_svrs
 DEV002 := TV_Add("DEV Tomcat Application WebWhiz          : DEV002", dev_svrs)  ; parent is qat_svrs

9117_P6_570A_SN1091111 := TV_Add("9117_P6_570A_SN1091111", svrs_by_frame)
PRD001_LPAR := TV_Add("PRD001_LPAR      : PRD001", 9117_P6_570A_SN1091111)
PRD002_LPAR := TV_Add("PRD002_LPAR      : PRD002", 9117_P6_570A_SN1091111)

9117_P6_570B_SN1091112 := TV_Add("9117_P6_570B_SN1091112", svrs_by_frame)
DEV001_LPAR := TV_Add("DEV001_LPAR      : DEV001", 9117_P6_570B_SN1091112)
DEV002_LPAR := TV_Add("DEV002_LPAR      : DEV002", 9117_P6_570B_SN1091112)

9117_P6_570C_SN1091113 := TV_Add("9117_P6_570C_SN1091113", svrs_by_frame)
QAT001_LPAR := TV_Add("QAT001_LPAR      : QAT001", 9117_P6_570C_SN1091113)
QAT002_LPAR := TV_Add("QAT002_LPAR      : QAT002", 9117_P6_570C_SN1091113)

winhost001 := TV_Add("winhost001#junkyardAPP#RDP            : winhost001#junkyardAPP", rdp_svrs)


Gui, Show  ; Show the window and its TreeView.
return

Svr4Putty:

if A_GuiEvent = DoubleClick
{
   SelectedSvr := TV_GetSelection()
   ParentNumber := TV_GetParent(SelectedSvr)
   TV_GetText(TmpParentName, ParentNumber)
   StringReplace, ParentNameOfSelected, TmpParentName, #, -, All

   ;MsgBox %ParentNameOfSelected%
   
   TV_GetText(ItemText, SelectedSvr)

   ;MsgBox %ItemText%
   

   if ( InStr( ItemText, "RDP") != 0) {
       MsgBox RDP session selected
       

       StringSplit, StringRA, ItemText, `:

       ; var StringRA2 contains a leading space - next line of code removes it but changes the variable that will be used to _FinalSvrVar_
       StringReplace, FinalSvrVar, StringRA2, %A_Space%

       StringReplace, TmpRDPVar, FinalSvrVar, #RDP,

       StringReplace, RDPVar, TmpRDPVar, #, -

       MsgBox %RDPVar%

       ;Run %RDPLoc% "/v: %RDPVar%.yourcorp.com /u:%RrsnID%"

   }
   
   else {
       StringSplit, StringRA, ItemText, `:

       ; var StringRA2 contains a leading space - next line of code removes it but changes the variable that will be used to _FinalSvrVar_
       StringReplace, FinalSvrVar, StringRA2, %A_Space%

       Run %PuttyLoc% -load %FinalSvrVar%
   }
       

   ;StringSplit, StringRA, ItemText, `:
   
 
   ; var StringRA2 contains a leading space - next line of code removes it but changes the variable that will be used to _FinalSvrVar_
   ;StringReplace, FinalSvrVar, StringRA2, %A_Space%

   ;uncomment following line if you use PuTTY profiles and would like to launch a profile instead of a generic PuTTY session
   ;Run %PuttyLoc% -load %FinalSvrVar%

   ;comment out following line if you uncomment the line above to use putty with profiles instead of generic putty sessions
   ;Run %PuttyLoc% "%PrsnId%@%FinalSvrVar%.yourcorp.com"

}

return

GuiClose:  ; Exit the script when the user closes the TreeView's GUI window.
ExitApp

Back to AutoHotkey