I use GNS3 for training and PuTTY as telnet client to devices console port on localhost.
Problem is that all PuTTY windows are named 127.0.0.1
So, I need to dynamically rename each of them with devices hostname...
I use this script which works great, but PuTTY is changing back its Window Title after the window is resized or minimized :x
I don't want to use the script to change that Title every 1 second and to be forced to have in background running one AutoHotKey script (compiled version) for each PuTTY window (memory inefficiency/CPU usage)
There is a way to change PuTTY Title in Linux with a command like this, but I have no idea how to push this as coming from the telnet session and not as a user input (with ControlSend)
Also I want to extract PuTTY text from that window and to match it against a reg-ex REGEX = (%NAME%|ciscoasa|pixfirewall)[#>\(]|Password:|Username:|login:
So this script will send a enter (ControlSend {ENTER}) every second, check again the PuTTY output and send again until the Router/ASA/Pix prompt is shown (In idle state eats one CPU core - Dynamips only)
The rename problem...
;From Linux Shell
echo -e "\033]0; TITLE \007\c"
;This is what PuTTY receive from a Telnet/SSH session as a remote window title changing‎ command
NewTitle := Chr(027) "]1; TITLE " Chr(007) Chr(027) "[1;" Chr(13)
;This is what I tried, but definitely not working ;))
ControlSend,, %NewTitle%, ahk_pid %NewPID%
;And also this in not working to extract window content TEXT
If (!DllCall("AttachConsole", "uint", NewPID))
Partial PuTTY script...
Loop, %0%
{
If (A_Index == 1)
{
HOST := %A_Index%
}
Else If (A_Index == 2)
{
PORT := %A_Index%
}
Else If (A_Index == 3)
{
NAME := %A_Index%
}
Run, putty.exe -telnet %HOST% %PORT% ,,, NewPID
WinWait ahk_pid %NewPID%
{
WinSetTitle, ahk_pid %NewPID%,, %NAME% - Console Port - [%HOST%:%PORT%]
}
This is the full script I'm using:TITLE = Telnet Wrapper Script For GNS3 - by N!NiX - 27/Apr/2012
ARGS=%0%
If (ARGS == 0)
{
MsgBox ,, %TITLE%,
( LTrim
Script used to open a telnet program to a GNS3 device Console/AUX
1) It will change the Window Title according to the device hostname.
2) Also will resize that window as specified in <X_WinSize> <Y_WinSize>
Those values are in pixels, recommended 920 420.
3) After the telnet connectivity is done, it will push some "Enter" keys
until the device console from Routers (Dynamips) will show the prompt.
This is useful for me because otherwise that router eats one CPU core.
Works great with Telnet, sometimes with Netcat and partial with PuTTY.
PuTTY is changing its name back after minimizing the window!
Usage:
console_wrapper.exe "program.exe" `%h `%p `%d <X_WinSize> <Y_WinSize>
GNS3 Ex:
console_wrapper.exe telnet.exe `%h `%p `%d 920 420
console_wrapper.exe nc.exe `%h `%p `%d 920 420
console_wrapper.exe putty.exe `%h `%p `%d 920 420
CLI Ex:
console_wrapper.exe putty.exe 127.0.0.1 2002 R1 920 420
)
ExitApp
}
Else If (ARGS != 6)
{
MsgBox ,, %TITLE%, Invalid parameters number - Must be 6!
ExitApp
}
StringCaseSense Off
DetectHiddenText, Off
SetControlDelay -1
EXPECT = 0
Loop, %0%
{
If (A_Index == 1)
{
TOOL := %A_Index%
}
Else If (A_Index == 2)
{
HOST := %A_Index%
}
Else If (A_Index == 3)
{
PORT := %A_Index%
}
Else If (A_Index == 4)
{
NAME := %A_Index%
}
Else If (A_Index == 5)
{
X_WIN := %A_Index%
}
Else If (A_Index == 6)
{
Y_WIN := %A_Index%
}
}
If (InStr(TOOL, "telnet.exe") || InStr(TOOL, "nc.exe"))
{
Run, %comspec% /C %TOOL% %HOST% %PORT%&&ECHO.&&TITLE %NAME% - Console is closed!&&ECHO Telnet connection "%HOST%:%PORT%" to "%NAME%" console port is lost!&&ECHO Press any key to exit...&&PAUSE >NUL ,,, NewPID
EXPECT = 1
}
Else If (InStr(TOOL, "putty.exe"))
{
Run, %TOOL% -telnet %HOST% %PORT% ,,, NewPID
}
Else ;Default mode if another tool is used
{
Run, %TOOL% %HOST% %PORT% ,,, NewPID
}
WinWait ahk_pid %NewPID%
{
If (EXPECT)
{
REGEX = (%NAME%|ciscoasa|pixfirewall)[#>\(]|Password:|Username:|login:|Telnet connection.*Press any key to exit
ConWinWidth = 32 ;Maximum router hostname length + # + 1 (CMD line length)
ConWinHeight = 100 ;How many lines to read from CMD output
WinSetTitle, ahk_pid %NewPID%,, %NAME% - Console Port - [%HOST%:%PORT%]
WinGetPos, X_Pos, Y_Pos,,, ahk_pid %NewPID%
WinMove, ahk_pid %NewPID%,, X_Pos+(mod(PORT, 10)*50)-250, Y_Pos+(mod(PORT, 10)*45)-200, X_WIN, Y_WIN
AttachConsole(NewPID)
Sleep, 500
Text := GetConsoleText(100, 1)
If (!RegExMatch(Text, " Dynamips "))
{
ControlSend,, `r`n, ahk_pid %NewPID%
ExitApp ;Manage output to Dynamips only - Until I find a way to do it for Qemu/Pemu
}
If (RegExMatch(Text, " AUX port ") > 1)
{
WinSetTitle, ahk_pid %NewPID%,, %NAME% - AUX Port - [%HOST%:%PORT%]
}
Loop, 45 ;After 45 seconds give up
{
If (RegExMatch(GetConsoleText(ConWinWidth, ConWinHeight), REGEX) > 1)
{
DllCall("FreeConsole")
ExitApp
}
Else
{
ControlSend,, `r, ahk_pid %NewPID%
Sleep, 1000
}
}
MsgBox ,, %TITLE%, Console port to device "%NAME%" is not responding!`nSometimes it can be unlocked by opening a bunch of consoles (~3 for Dynamips)
ExitApp
}
Else
{
WinMove, ahk_pid %NewPID%,,,, X_WIN, Y_WIN
WinSetTitle, ahk_pid %NewPID%,, %NAME% - Console Port - [%HOST%:%PORT%]
ControlSend,, `r`n`r`n, ahk_pid %NewPID%
}
}
GetConsoleText(ConWinWidth, ConWinHeight)
{
global hConOut
VarSetCapacity(info, 24, 0)
If (!DllCall("GetConsoleScreenBufferInfo","uint",hConOut,"uint",&info))
{
MsgBox ,, %TITLE%, GetConsoleScreenBufferInfo failed - error %A_LastError%.
ExitApp
}
VarSetCapacity(buf, ConWinWidth*ConWinHeight*4, 0)
If (!DllCall("ReadConsoleOutput","uint",hConOut,"uint",&buf,"uint",ConWinWidth|ConWinHeight<<16,"uint",0,"uint",&info+10))
{
MsgBox ,, %TITLE%, ReadConsoleOutput failed - error %A_LastError%.
ExitApp
}
VarSetCapacity(text, ConWinWidth*ConWinHeight)
Loop % ConWinWidth*ConWinHeight
{
text .= Chr(NumGet(buf, 4*(A_Index-1), "Char"))
}
Return text
}
AttachConsole(pid)
{
global hConOut
If (!DllCall("AttachConsole", "uint", pid))
{
MsgBox ,, %TITLE%, AttachConsole failed - error %A_LastError%.
ExitApp
}
hConOut:=DllCall("CreateFile","str","CONOUT$","uint",0xC0000000,"uint",7,"uint",0,"uint",3,"uint",0,"uint",0)
If (hConOut = -1)
{
MsgBox ,, %TITLE%, CreateFile failed - error %A_LastError%.
ExitApp
}
}




