Forum: Ruby telnet by socket

Posted by Ferdous ara (ferdous)
on 2012-10-11 11:42
Hi
i am trying to telnet by ruby.. i went through google. but i am bit
confused.
so i need bit help


require 'socket'
host = 'localhost'
port = 5555
socket = TCPSocket.open(host,port)
send = socket.send("recover BD-1",0)
response = socket.sead
print response

basically, if i do from command line

telnet localhost 5555
OK Server
recover BD-1             : then i will type this

and then telnet will output
OK

so i need to get that OK

but that socket is not doing anything

Can some one help me out please
Posted by unknown (Guest)
on 2012-10-11 19:53
(Received via mailing list)
Am 11.10.2012 11:42, schrieb Ferdous ara:
> send = socket.send("recover BD-1",0)
> response = socket.sead
                     ^^^^
typo
Posted by Brian Candler (candlerb)
on 2012-10-11 23:26
Ferdous ara wrote in post #1079397:
> Hi
> i am trying to telnet by ruby.. i went through google.

Then you should have found Net::Telnet which is part of the ruby 
standard library.

But then maybe you don't actually want telnet; what you want is to 
communicate over a TCP socket.

In that case, beware that the protocol will probably require you to end 
the command with \r\n, or at least \n, which you are not sending at the 
moment.
Posted by Ferdous ara (ferdous)
on 2012-10-13 13:39
HI
i found the solution i had to put

sock.puts "recover BD-1\r\n"
response = sock.read

i had to put \r\n
i guess its specifiq to environment .


but only problem is ,

its giving me 2 out put

OK Server
Ok

but how will just take the last output which is OK?

do i have to take the value as array ??

thanks
Posted by Todd B. (todd_b)
on 2012-10-13 14:37
(Received via mailing list)
On Sat, Oct 13, 2012 at 6:39 AM, Ferdous ara <lists@ruby-forum.com> 
wrote:
> but only problem is ,
>
> its giving me 2 out put
>
> OK Server
> Ok
>
> but how will just take the last output which is OK?
>
> do i have to take the value as array ??

It sounds like it's receiving and responding to \r and then \n. Just
take the first output.
Posted by Ferdous ara (ferdous)
on 2012-10-13 15:19
Hi
no the first response is :

OK Server

which is, when you connect to the telnet

but I need the 2nd one

OK
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.