Some news:
Receivehandler is now a function to get the telnetcode. Anyways problem is the same.
Till i get an answer i wil edit here my actual code:
EDIT 20120314 13:38
Ok if i send the login data with the hotkey, i get a response and the app works properly, but i need to do this automated, why is there a difference?
EDIT 15:56
Problem seems solved, made new pseudothread with timer and made Telnettext global. Is someone knows a better way i would appreciate
Code:
#SingleInstance, Off
FileDelete Telnetlog.txt
FileDelete Eventlog.txt
sName = 192.168.0.1
PortNum = 23
Schritt=0
global TelnetCode
SetTimer ReceiveHandler, 500
Gui, Add, edit, vtext w500 h90, Hier kommt der Text rein
Gui, Add, text, vevent, Noch kein Event aufgetreten hier werden alle Parameter aufgelistet die an die Function Peer übergeben werden
Gui, Add, text, vdebug, debugkram und sowas halt
Gui, show
If (sName="")
InputBox, sName, Server, Enter the server to connect to, either as a domain name or IP number.
If (PortNum = "")
InputBox, PortNum, Port Number, Enter the number of the port to connect to.
;Needed if AHKsock isn't in one of your lib folders
#Include %A_ScriptDir%\AHKsock.ahk
;Set up an error handler (this is optional)
AHKsock_ErrorHandler("AHKsockErrors")
;Set default value to invalid handle
iPeerSocket := -1
If (i := AHKsock_Connect(sName, PortNum, "Peer"))
Return
return
sende(iPeerSocket, text)
{
sendet=1
i=1
Stringsplit Feld, text
FileAppend, gesendet ,Telnetlog.txt
Loop %Feld0%
{
fuell:=Feld%i%
i++
AHKsock_Send(iPeerSocket, &fuell, 1)
FileAppend %fuell% ,Telnetlog.txt
sleep 100
}
FileAppend ,`n ,Telnetlog.txt
sendet=
}
ReceiveHandler:
GuiControl ,, text, %TelnetCode%
;MsgBox %TelnetCode%
if Schritt=0
{
if (InStr(TelnetCode,"U s e r n a m e"))
{
GuiControl ,, debug, Dbg liest username
if not (schongemacht=1)
{
GuiControl ,, debug, Dbg schreibt admin
; wenn username steht admin nicht steht, und auch gerade nicht gesendet wird
sende(iPeerSocket,"admin")
fuell=`r
AHKsock_ForceSend(iPeerSocket, &fuell, 1)
Schritt=1
sleep 500
}
}
GuiControl ,, debug, Dbg liest 2tes mal
GuiControl ,, text, %TelnetCode%
if (InStr(TelnetCode,"P a s s"))
{
GuiControl ,, debug, Dbg liest Passwort
if (Schritt=1)
{
GuiControl ,, debug, Dbg schreibt 1234
sende(iPeerSocket,"1234")
sleep 150
fuell=`r
AHKsock_ForceSend(iPeerSocket, &fuell, 1)
GuiControl ,, text, %TelnetCode%
sleep 500
if (InStr(TelnetCode,"Z y X E L"))
Schritt=2
}
}
}
if (Schritt=2)
{
sende(iPeerSocket,"show running-config")
sleep 150
fuell=`r
AHKsock_ForceSend(iPeerSocket, &fuell, 1)
sleep 100
GuiControl ,, text, %TelnetCode%
if (InStr(TelnetCode,"C u r r e n t c o n f"))
{
iSchritt2len:=InStr(TelnetCode,"C u r r e n t c o n f")
sleep 1000
sRunningConfig:=SubStr(TelnetCode,iSchritt2len)
Schritt=3
iSchritt3len:=StrLen(TelnetCode)
;MsgBox % "Schritt 2 länge" . iSchritt2len . "Ende" . iSchritt3len
}
}
if (Schritt=3)
{
sende(iPeerSocket,"show logging")
sleep 150
fuell=`r
AHKsock_ForceSend(iPeerSocket, &fuell, 1)
sleep 3500
sLogging:=SubStr(TelnetCode,iSchritt3len)
FileAppend, %sRunningConfig% ,config.txt
FileAppend, %sLogging% ,syslog.txt
Schritt=4
}
if (Schritt=4)
{
sende(iPeerSocket,"")
sleep 150
fuell=`r
AHKsock_ForceSend(iPeerSocket, &fuell, 1)
}
return
Peer(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0, ByRef bData = 0, bDataLength = 0) {
FileAppend,
(
Event = %sEvent%
iSocket = %iSocket%
sName = %sName%
sAddr = %sAddr%
sPort = %sPort%
ByRef bData = %bData%
bDataLength = %bDataLength%
), Eventlog.txt
GuiControl,, event, %sEvent% iSocket = %iSocket%, sName = %sName%, sAddr = %sAddr%, sPort = %sPort%, ByRef bData = %bData%, bDataLength = %bDataLength%
Global iPeerSocket, bExiting, bSameMachine
Static iIgnoreDisconnect
If (sEvent = "ACCEPTED") {
MsgBox, % "WHOAH, NELLIE! A client with IP " sAddr " connected! But we aren't the server!"
} If (sEvent = "CONNECTED")
{
;Check if the connection attempt was successful
If (iSocket = -1) {
OutputDebug, % "AHKsock_Connect() failed."
Return
} Else OutputDebug, % "AHKsock_Connect() successfully connected on IP " sAddr "."
;We now have an established connection with a peer
;This is the same fail-safe as in the ACCEPTED event (see comment block there)
If (iPeerSocket <> -1) {
OutputDebug, % "We already have a peer! Disconnecting..."
AHKsock_Close(iSocket) ;Close the socket
iIgnoreDisconnect += 1 ;So that we don't react when this peer disconnects
Return
}
;Remember the socket
iPeerSocket := iSocket
}
Else If (sEvent = "DISCONNECTED")
{
;Check if we're supposed to ignore this event
If iIgnoreDisconnect
{
iIgnoreDisconnect -= 1
Return
}
;Reset variable
iPeerSocket := -1
;We should go back to listening (unless we're in the process of leaving)
}
Else If (sEvent = "RECEIVED")
{
hexData:=Bin2Hex(&bData, bDataLength)
TelnetCode:=HextoTelnet(hexData)
;MsgBox %TelnetCode%
;GuiControl ,, text, %bData% %TelnetCode%
FileAppend %TelnetCode%,Telnetlog.txt
;Send to the stream processor
;NotAStreamProcessor(bData, bDataLength)
}
}
NotAStreamProcessor(ByRef bNewData = 0, bNewDataLength = -1) {
If(bNewDataLength = -1)
Return
bDataPointer := &bNewData, bDataLength := bNewDataLength
;Start processing the stream of frames
iOffset := 0
;Get the string and add to dialog
VarSetCapacity(sTextData, bNewDataLength + 1, 0)
; AddDialog(bDataPointer, False)
CopyBinData(bDataPointer, &sTextData, bNewDataLength)
; GuiControl ,, text, %%
;Add the string to the dialog textbox
; AddDialog(&sTextData, False)
InsertText(htext,&sTextData)
}
AHKsockErrors(iError, iSocket)
{
MsgBox error %iError%
}
CopyBinData(ptrSource, ptrDestination, iLength)
{
If iLength ;Only do it if there's anything to copy
DllCall("RtlMoveMemory", "uint", ptrDestination, "uint", ptrSource, "uint", iLength)
}
/*! TheGood
Append text to an Edit control
http://www.autohotkey.com/forum/viewtopic.php?t=56717
*/
InsertText(hEdit, ptrText, iPos = -1) {
If (iPos = -1) {
SendMessage, 0x000E, 0, 0,, ahk_id %hEdit% ;WM_GETTEXTLENGTH
iPos := ErrorLevel
}
SendMessage, 0x00B1, iPos, iPos,, ahk_id text ;EM_SETSEL
SendMessage, 0x00C2, False, ptrText,, ahk_id text ;EM_REPLACESEL
}
;By Laszlo, adapted by TheGood
;http://www.autohotkey.com/forum/viewtopic.php?p=377086#377086
Bin2Hex(addr,len) {
Static fun, ptr
If (fun = "") {
If A_IsUnicode
If (A_PtrSize = 8)
h=4533c94c8bd14585c07e63458bd86690440fb60248ffc2418bc9410fb6c0c0e8043c090fb6c00f97c14180e00f66f7d96683e1076603c8410fb6c06683c1304180f8096641890a418bc90f97c166f7d94983c2046683e1076603c86683c13049ffcb6641894afe75a76645890ac366448909c3
Else h=558B6C241085ED7E5F568B74240C578B7C24148A078AC8C0E90447BA090000003AD11BD2F7DA66F7DA0FB6C96683E2076603D16683C230668916240FB2093AD01BC9F7D966F7D96683E1070FB6D06603CA6683C13066894E0283C6044D75B433C05F6689065E5DC38B54240833C966890A5DC3
Else h=558B6C241085ED7E45568B74240C578B7C24148A078AC8C0E9044780F9090F97C2F6DA80E20702D1240F80C2303C090F97C1F6D980E10702C880C1308816884E0183C6024D75CC5FC606005E5DC38B542408C602005DC3
VarSetCapacity(fun, StrLen(h) // 2)
Loop % StrLen(h) // 2
NumPut("0x" . SubStr(h, 2 * A_Index - 1, 2), fun, A_Index - 1, "Char")
ptr := A_PtrSize ? "Ptr" : "UInt"
DllCall("VirtualProtect", ptr, &fun, ptr, VarSetCapacity(fun), "UInt", 0x40, "UInt*", 0)
}
VarSetCapacity(hex, A_IsUnicode ? 4 * len + 2 : 2 * len + 1)
DllCall(&fun, ptr, &hex, ptr, addr, "UInt", len, "CDecl")
VarSetCapacity(hex, -1) ; update StrLen
Return hex
}
HextoTelnet(HexData)
{
Global TelnetFeld, Deci
;MsgBox test %HexData%
Read_Data_Num_Bytes := StrLen(HexData) / 2
Loop %Read_Data_Num_Bytes%
{
StringLeft, Byte, HexData, 2
StringTrimLeft, HexData, HexData, 2
Byte = 0x%Byte%
Byte := Byte + 0 ;Convert to Decimal
Hilfsvar:= Telnetfeld%Byte%
;MsgBox %Hilfsvar%
If Hilfsvar<1
Hilfsvar:=Chr(Byte)
Deci=%Deci% %Hilfsvar%
}
FileAppend, %Deci%, Telnetlog.txt
FileAppend, `n, Telnetlog.txt
;MsgBox test %HexData% %Read_Data_Num_Bytes% %Deci%
return Deci
}
#::
sende(iPeerSocket,"admin")
fuell=`r
AHKsock_ForceSend(iPeerSocket, &fuell, 1)
return
^::
sende(iPeerSocket,"1234")
fuell=`r
AHKsock_ForceSend(iPeerSocket, &fuell, 1)