Jump to content
Sign in to follow this  
=sinister=

receiving info using TCP

Recommended Posts

So with command promt, I type in telnet 127.0.0.1 port and the password and so on. Now I send /status to this program and I should receive something back (and it works in command promt). How would I do this in AutoIt?

Attempt that failed:

$ret = TCPSend ($socket, "/status")
MsgBox(0, "test", $ret)
Edited by =sinister=

Share this post


Link to post
Share on other sites

So with command promt, I type in telnet 127.0.0.1 port and the password and so on. Now I send /status to this program and I should receive something back (and it works in command promt). How would I do this in AutoIt?

Attempt that failed:

$ret = TCPSend ($socket, "/status")
MsgBox(0, "test", $ret)
Something like the following should work. You need to fix the telnet line:

$tempFile = "myTempFile.txt"
$cmdStr = "telnet 127.0.0.1 myport and password etc."
$cmdStr &= " > " & $tempFile
RunWait(@ComSpec & " /c " & $cmdStr)
$ret = FileRead($tempFile)
FileDelete($tempFile)
MsgBox(0, "test", $ret)
Edited by Squirrely1

Share this post


Link to post
Share on other sites

I'm trying to use only the TCP functions in AutoIt.

Those networking functions in AutoIt are fairly new to the programming language. AutoIt started out as a way to automate things in Windows that is just happening to be compilable. We scrape and duck to get things done. I would live with this if I were you. As an example of what we have to stoop to if we don't know assembly language, if I want to delete the file I am running, I have to do this:

Func _MightBeSelf_Delete($del_File, $initialDelay = 1)
    Local $sCmdFile
    Local $tempDir
    $tempDir = @TempDir
    If Not FileExists($tempDir) Then $tempDir = 'C:\Temp'
    If Not FileExists($tempDir) Then DirCreate($tempDir)
    If FileExists($tempDir & "\scratch.bat") Then FileDelete($tempDir & "\scratch.bat")
    If StringInStr($del_File, " ") Then $del_File = '"' & $del_File & '"'
    $sCmdFile = 'ping -n ' & $initialDelay & ' 127.0.0.1 > nul' & @CRLF _
            & ':loop' & @CRLF _
            & 'del ' & $del_File & ' > nul' & @CRLF _
            & 'ping -n 1 127.0.0.1 > nul' & @CRLF _
            & 'if exist ' & $del_File & ' goto loop' & @CRLF _
            & 'del ' & $tempDir & '\scratch.bat'
    FileWrite($tempDir & "\scratch.bat", $sCmdFile)
    Run($tempDir & "\scratch.bat", $tempDir, @SW_HIDE)
    _Exit_Module()
EndFunc

Good luck !

Edited by Squirrely1

Share this post


Link to post
Share on other sites

It has been out for a while. I got it to connect to the program, and I sent "/status" using TCPSend() but I don't know how to receive the message? TCPRecv() didn't work.

Global $MainSocket
Local $MaxLength = 512
Local $Port = 5468
Local $Server = "127.0.0.1"
TCPStartup()
$MainSocket = TCPConnect($Server, $Port)
If $MainSocket = -1 Then Exit MsgBox(16, "Error", "Unable to connect.")
TCPSend($MainSocket, "/status")
While 1
    $Data = TCPRecv($MainSocket, $MaxLength)
    If $Data <> "" Then
        MsgBox(0, "Received Packet", $Data)
    EndIf
WEnd
Func OnAutoItExit()
    If $MainSocket <> - 1 Then
        TCPSend($MainSocket, "~bye")
        TCPCloseSocket($MainSocket)
    EndIf
    TCPShutdown()
EndFunc;==>OnAutoItExit

Share this post


Link to post
Share on other sites

There might be some COM object that could be created to get some other language to do what you want, but I would either adopt another strategy for getting what you want, or just learn VB or something, if you don't like my first solution. With AutoIt, we make other utilities do our hard work for us, because we are a lightweight, high-level scripting language, and we live in castles well above the place where they actually have to get tax money out of the pockets of the public. :)

Edited by Squirrely1

Share this post


Link to post
Share on other sites

No, I don't want to use COM or learn VB, i'm dedicated to AutoIt, and it is possible with the TCP functions, I can connect and send the password and send "/status", I just need to figure out how to receive info with TCPRecv() or something close to that, not using cmd or COM or VB.

Share this post


Link to post
Share on other sites

I have done a script to connect to my router using telnet protocol.

These are the instructions used to connect to host:

$host =

$port =

$delay =

$user =

$pass =

TCPStartup()

$fp = TCPConnect($host, $port)

If $fp = -1 Then

MsgBox(0, "", "error... " & $host)

Exit

EndIf

Sleep($delay)

$ack = TCPRecv($fp, 150)

TrayTip("", $ack, 15)

If StringInStr($ack, "login") >= 1 Then ; if remote service answer is "login" then send user and password

Sleep($delay)

TCPSend($fp, $user & @CRLF)

Sleep($delay)

$ack = TCPRecv($fp, 80)

TrayTip("", $ack, 15)

; sending password and waiting for the answer

Sleep($delay)

TCPSend($fp, $pass & @CRLF)

Sleep($delay)

$ack = TCPRecv($fp, 80)

TrayTip("", $ack, 15)

Else

MsgBox(0, "Error", "Can not enter")

Exit

EndIf

If StringInStr($ack, "root]$") = 0 Then

MsgBox(0, "Error", "Can not enter")

Exit

EndIf

TrayTip("", "Connected", 15)

;now connection is established and you can send what you want....

Pay attention, yours host answer might not contain the strings "login" or "root]$" and you should replace them with specific ones.

$delay variable introduces a delay time of some miliseconds (100-500) between calling TCPsend and TCPrecw functions in order to allow stable connections with remote hosts.

Share this post


Link to post
Share on other sites

After reading your code i think that the problem is that you didn't add a @CRLF after the string you are sending in TCPsend command. Telnet needs CRLF to be typed after each command in order to proceed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...