AutoHotkey Community

It is currently May 27th, 2012, 3:55 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 140 posts ]  Go to page Previous  1 ... 6, 7, 8, 9, 10  Next
Author Message
 Post subject:
PostPosted: May 11th, 2011, 3:17 pm 
Offline

Joined: May 17th, 2007, 9:06 pm
Posts: 421
Location: England
Would be nice to see UDP support


Report this post
Top
 Profile  
Reply with quote  
 Post subject: AHKSock instead of WS2
PostPosted: July 22nd, 2011, 4:56 pm 
Offline

Joined: June 19th, 2011, 8:34 pm
Posts: 4
Location: Belgium
Would anybody here be able to give a headstart on how to use AHKSock to control Firefox using MozRepl (MozRepl GitHub page)?

Formerly, I used FF.ahk (FF.ahk forum thread), but since I've been using AHK_L, the WS2 library, which FF.ahk uses, doesn't work for me anymore.
All I need is to have FF.ahk to work with AHKSock instead of WS2, but I myself do not understand enough of the working principles to do it myself.

Any help would be greatly appreciated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 9th, 2011, 11:12 pm 
Saved this thread in my bookmarks awhile ago and I've finnally gotten around to trying to implement it to my needs. I decided the best way to do this would be to try and modify the examples.

Starting out with first Client / Server examples. I got the idea of reversing the functions, that is having a Server Listen / Receive data rather then Listen / Send it.

Server code executes but I'm not sure if it's working because the Client refuses to start with and reports this error:

Code:
C:\Program Files\AutoHotkey\Lib\AHKsock.ahk (418) : ==> Functions cannot contain functions.
     Specifically: AHKsock_Listen(sPort, sFunction = False)


Server Code AHK Basic:
Code:
/*! TheGood
    AHKsock - A simple AHK implementation of Winsock.
    AHKsock Example 1 - Simple Scenario
    http://www.autohotkey.com/forum/viewtopic.php?p=355775
    Last updated: January 21, 2011
   
    In this simple scenario:
        1.  First, the server starts listening for clients on port 27015.
        2.  As soon as the client is started, it attempts to connect to the server.
        3.  As soon as the connection is established, the CONNECTED/ACCEPTED event is fired on both client and server.
        4.  Once the server receives the SEND event, it will send some binary data to the client.
        5.  Once the binary data is successfully sent, the server will initiate shutdown.
        6.  The client receives a RECEIVED event with the data sent by the server.
        7.  As soon as the client receives the DISCONNECT event, it exits.
        8.  The server receives a DISCONNECT event as well, meaning that shutdown is complete.
        9.  The server goes back to listening for clients on port 27015.
        10. The client can be started up again to have the same process repeat.
   
    This example does not have a GUI, but uses OutputDebug to output its data. See the command's docs for more details.
    If the server isn't running on the same computer, change sServer to the IP address or hostname of the server.
*/
    ;Needed if AHKsock isn't in one of your lib folders
    ;#Include %A_ScriptDir%\AHKsock.ahk
   
    ;Register OnExit subroutine so that AHKsock_Close is called before exit
    OnExit, CloseAHKsock
   
    ;Add menu item for exiting gracefully (see comment block in CloseAHKsock)
    Menu, Tray, Add
    Menu, Tray, Add, Exit Gracefully, CloseAHKsock
   
    ;Set up an error handler (this is optional)
    AHKsock_ErrorHandler("AHKsockErrors")
   
   
   
    ;Listen on port 27015
    If (i := AHKsock_Listen(27015, "Recv")) {
        OutputDebug, % "AHKsock_Listen() failed with return value = " i " and ErrorLevel = " ErrorLevel
        ExitApp
    }
Return

CloseAHKsock:
   
    AHKsock_Close()
ExitApp
Recv(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0, ByRef bData = 0, iLength = 0) {
   
    If (sEvent = "ACCEPTED") {
        OutputDebug, % "Server - A client connected!"
       

        If bConnected {
            OutputDebug, % "Server - Disconnected new client because we are still serving a previous client."
            AHKsock_Close(iSocket)
           
        } Else { ;If we weren't connected before, we are now!
            bConnected := True
            bDataSent := 0 ;Reset bDataSent for the new client
        }
       
    } Else If (sEvent = "DISCONNECTED") {
        OutputDebug, % "Server - The client disconnected. Going back to listening..."
       
    } Else If (sEvent = "RECEIVED") {
       
        ;We received data. Output it.
        OutputDebug, % "Client - We received " iLength " bytes."
        OutputDebug, % "Client - Data: " Bin2Hex(&bData, iLength)

        }
      
       ;Reset the static variable
        bDataSent := 0
   
        OutputDebug, % "Server - Closing the client connection now..."
        If (i := AHKsock_Close(iSocket))
            OutputDebug, % "Server - The shutdown() call failed. ErrorLevel = " ErrorLevel
       
        ;Reset the connection status so that we can accept new clients.
        ;We don't need to actually wait for the client we just served to disconnect because as long as we are done sending
        ;data to it, we can use the bDataSent variable to track the data sending progress with another client!
        bConnected := False
    }


;We're not actually handling errors here. This is here just to make us aware of errors if any do come up.
AHKsockErrors(iError, iSocket) {
    OutputDebug, % "Server - Error " iError " with error code = " ErrorLevel ((iSocket <> -1) ? " on socket " iSocket : "")
}

;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
}


Client Code:

Code:
    ;Needed if AHKsock isn't in one of your lib folders
    ;#Include %A_ScriptDir%\AHKsock.ahk
   
    ;Allow multiple instances. This is to allow you to stress-test the server. For more info on how to perform the
    ;stress-test, see second comment block in the ACCEPTED event of the Send() function in the server script.
    #SingleInstance, Off
   
    ;Contains the IP address or hostname of the server we will connect to
    sServer := "localhost"
   
    ;Register OnExit subroutine so that AHKsock_Close is called before exit
    OnExit, CloseAHKsock
   
    ;Add menu item for exiting gracefully (see comment block in CloseAHKsock)
    Menu, Tray, Add
    Menu, Tray, Add, Exit Gracefully, CloseAHKsock
   
    ;Set up an error handler (this is optional)
    AHKsock_ErrorHandler("AHKsockErrors")
   
   VarSetCapacity(bData, bDataLength := 10, 0x01)
   
    ;Connect on this computer on port 27015
    If (i := AHKsock_Connect(sServer, 27015, "Send")) {
        OutputDebug, % "AHKsock_Connect() failed with return value = " i " and ErrorLevel = " ErrorLevel
        ExitApp
    }
Return

CloseAHKsock:

    AHKsock_Close()
ExitApp

Send(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0, ByRef bRecvData = 0, bRecvDataLength = 0) {
   Global bData, bDataLength
    Static bDataSent, bConnected
   
   If (sEvent = "CONNECTED") {
       
        ;Check if the connection attempt was succesful
        If (iSocket = -1) {
            OutputDebug, % "Client - AHKsock_Connect() failed. Exiting..."
            ExitApp
        } Else OutputDebug, % "Client - AHKsock_Connect() successfully connected!"
       
    } Else If (sEvent = "DISCONNECTED") {
       
        OutputDebug, % "Client - The server closed the connection. Exiting..."
        ExitApp
       
    } Else If (sEvent = "SEND") {
               
        Loop {
           
            ;Try to send the data
            If ((i := AHKsock_Send(iSocket, &bData + bDataSent, bDataLength - bDataSent)) < 0) {
               
                ;Check if we received WSAEWOULDBLOCK.
                If (i = -2) {
                    ;That's ok. We can leave and we'll keep sending from
                    ;where we left off the next time we get the SEND event.
                    Return
                   
                ;Something bad has happened with AHKsock_Send
                } Else OutputDebug, % "Server - AHKsock_Send failed with return value = " i " and ErrorLevel = " ErrorLevel
               
            ;We were able to send bytes!
            } Else OutputDebug, % "Server - Sent " i " bytes!"
           
            ;Check if everything was sent
            If (i < bDataLength - bDataSent)
                bDataSent += i ;Advance the offset so that at the next iteration, we'll start sending from where we left off
            Else Break ;We're done
}

;We're not actually handling errors here. This is here just to make us aware of errors if any do come up.
;AHKsockErrors(iError, iSocket) {
 ;   OutputDebug, % "Server - Error " iError " with error code = " ErrorLevel ((iSocket <> -1) ? " on socket " iSocket : "")
;}


Any help with this would be greatly appreciated!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2011, 9:24 pm 
Disregard the above post!

I'm not sure exactly how I screwed up but the above code was not what I intending to post. :oops:

And I did successfully reverse the functions after I posted.

So nothing more to see here, carry on!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 11th, 2011, 5:31 pm 
Offline

Joined: October 15th, 2007, 3:10 pm
Posts: 790
Location: England
ahhh... the advantages of registering... :wink:

_________________
My code is written for AHK Basic unless otherwise specified. This means it may not work in AHK_L (especially Unicode), due to a few known compatibility issues.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 5th, 2011, 8:16 pm 
Hope I'm not asking too basic a question, but I'm unable to run example 3. I downloaded the AHKsock.ahk and example 3 for AHK_L to the same folder, then try to run example 3. I receive this error:

Code:
Error: Call to nonexistent function.

Specifically: AHKsock_ErrorHandler("AHKsockErrors")

(Line 36)

I get this same error for any of the example scripts. :(

Any ideas?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 5th, 2011, 8:25 pm 
Neeevermind! Needed the Include line for AHKsock. (Moral: Read more first!)


Report this post
Top
  
Reply with quote  
 Post subject: Server
PostPosted: September 14th, 2011, 9:24 am 
Hi everybody, newbie here. I would need a TCP server in AHK, that would listen on a defined port, and when it get's a message, it would read it and start an app. In. I send a path to a file c:/photo.jpg, and I need the server reads it and starts IrfanView with the file variable.
If I understand correctly, AHKsock should do this without problems, but I didn't manage to modify the example server script to do what I need. Anyone could help me with this? Thanks


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 28th, 2011, 6:48 am 
It's nicer if you add a tunneling script for HTTP and HTTPS.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 29th, 2012, 2:52 pm 
Offline

Joined: February 29th, 2012, 2:43 pm
Posts: 1
Hello,

Nice implementation to make some client/server apps.
I was wondering if any script based on AHKsock in order to replicate keyboard/mouse key pressed from one pc to another was already developped? if not, I will try to do it but I will not do it new if someone already did that work ! :wink:


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 3rd, 2012, 7:47 am 
So sorry to res such an old thread, but I have been trying with all my might to get this working for me. The only problem that I really face is actually being able to take the received data and turn it into a usable variable.

I am using example 3 primarily, and obviously I"m doing something wrong. I don't want to output the sent variable to a GUI. I want to just have it as a variable that can use it.

What I try doing is stripping out all the GUI code and then when i get the receive event, just do a msgbox with the variable. Sadly, it will only receive once and display info. After that, whenever I send it stuff, it will not display anything until I call the close function for the socket.

So, either, anyone have ideas on why I can't get it to display the variable? or anyone know of a good way to convert this binary data that is sent into a usable variable for me? Thanks.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2012, 8:19 am 
knackko wrote:
Hello,

Nice implementation to make some client/server apps.
I was wondering if any script based on AHKsock in order to replicate keyboard/mouse key pressed from one pc to another was already developped? if not, I will try to do it but I will not do it new if someone already did that work ! :wink:


If you do get that working, please let me know\post code. I'd be _very_ interested in this too.


Report this post
Top
  
Reply with quote  
PostPosted: March 14th, 2012, 8:32 am 
Hi

First i would like to say this is an awesome forum here, i have learned so much by reading the posts.

I have got a problem with my Telnet App. It connects to a Zyxel ES-4124 and should do some troubleshooting later.
I have built my app with the example Nr 3.

I think my problem is, the app gets no call on Peer() after it sends out the username.

So here is my code:
Code:
#SingleInstance, Off
FileDelete Telnetlog.txt 
sName = 192.168.0.1
PortNum = 23



Telnetfeld50= Status
Telnetfeld240=SE
Telnetfeld240= SE
Telnetfeld241= NOP
Telnetfeld242= DM
Telnetfeld243= BRK
Telnetfeld244= IP-
Telnetfeld245= AO
Telnetfeld246= AYT
Telnetfeld247= EC
Telnetfeld248= EL
Telnetfeld249= GA
Telnetfeld250= SB
Telnetfeld251= WIRD
Telnetfeld252= WONT
Telnetfeld253= DO
Telnetfeld254= DONT
Telnetfeld255= IAC

Gui, Add, edit, vtext w500 h90, Hier kommt der Text rein
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_ForceSend(iPeerSocket, &fuell, 1)
  FileAppend %fuell% ,Telnetlog.txt
 }
FileAppend ,`n ,Telnetlog.txt
sendet=
}

receiveHandler:
if (InStr(TelnetCode,"U s e r n a m e :"))
{
   if not (InStr(TelnetCode,"a d m i n "))
   {

      if not (sendet)
      {
         ; wenn username steht admin nicht steht, und auch gerade nicht gesendet wird
         sende(iPeerSocket,"admin")
         fuell=`r
         AHKsock_ForceSend(iPeerSocket, &fuell, 1)

      }
   }
   else
   {
      MsgBox 1
      if not (InStr(TelnetCode,"*"))
      {
         MsgBox 2
         ;wenn username steht admin steht und noch kein * steht
         sende(iPeerSocket,"1234")
         sleep 150
         fuell=`r
         AHKsock_ForceSend(iPeerSocket, &fuell, 1)
      }
   }
}
sleep 100
sEvent="RECEIVED"
return

Peer(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0, ByRef bData = 0, bDataLength = 0) {
    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, %TelnetCode%
      FileAppend %TelnetCode%,Telnetlog.txt
      Gosub ReceiveHandler
    }
}

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 %hEdit% ;EM_SETSEL
    SendMessage, 0x00C2, False, ptrText,, ahk_id %hEdit% ;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
}


would be nice to get some hints

regards Kibo


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2012, 9:11 am 
Offline

Joined: March 14th, 2012, 9:09 am
Posts: 1
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 4th, 2012, 7:21 pm 
Offline

Joined: March 29th, 2012, 6:10 pm
Posts: 47
knackko wrote:
Hello,

I was wondering if any script based on AHKsock in order to replicate keyboard/mouse key pressed from one pc to another was already developped? if not, I will try to do it but I will not do it new if someone already did that work ! :wink:


I would also be very interested in how this would be accomplished. I am using AHK to "Multi-Box" an MMORPG LOTRO. I now use AHK on 3 PC's simultaneously and have Hotkeys to assist me in the game controlling and navigating multiple characters/game copies. So I now must pivot from one keyboard to the other two to trigger the various AHK scripts to get my characters to do my bidding. This is workable but I feel like AHK could do better here. I do realize there is a commercially available application for this (ISBoxer) but again I feel like AHK could do the job.

I am a bit of a noobie in AHK, but is it possible to get my AHK to send the commands to my other two PC's without me having to pivot from keyboard to keyboard? I am posting here because the AHKsocks seems to be in the direction of what I want to do. Can I add short trigger-able AHKSend and AHKListen scripts on the target PC machine. Please forgive me if my understanding is completely wrong. Should this question be a new thread? Thanx in advance.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 140 posts ]  Go to page Previous  1 ... 6, 7, 8, 9, 10  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 25 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group