Telnetting a server using node.js

6,271 views
Skip to first unread message

Bugzu G

unread,
Mar 23, 2011, 1:32:29 PM3/23/11
to nodejs
Hi Guyz,


I am new to nodejs. I am using this script for telnetting a server and
i am able to connect and receive response from server.

var net = require('net');
var conn = net.createConnection(port,host);

Is it write way to do?
if yes then how can i pass username and password so that i am able to
login without user interaction?



Thanks in advance.

Bugzu

Bugzu G

unread,
Mar 23, 2011, 12:35:53 PM3/23/11
to nod...@googlegroups.com

Bugzu G

unread,
Mar 24, 2011, 1:56:38 PM3/24/11
to nodejs

Anyone, please help me with this. Any help would be highly appreciated.

Thanks,
bugzu

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Matt

unread,
Mar 24, 2011, 2:04:03 PM3/24/11
to nod...@googlegroups.com, Bugzu G
Authentication depends entirely on the protocol in question.

Isaac Schlueter

unread,
Mar 24, 2011, 2:10:26 PM3/24/11
to nod...@googlegroups.com
What's the server?

So far, yes, that is the right way to create a connection. Here's the rest:

var conn = net.createConnection(port,host);
conn.on("connection", function (socket) {
socket.on("data", function (c) {
// data coming in. deal with it
socket.write("hello")
socket.end("send one last optional buffer and close the connection")
})
socket.on("end", function () {
// ITS OVER!
})
})

Andrew Stone

unread,
Mar 24, 2011, 2:15:48 PM3/24/11
to nod...@googlegroups.com, Bugzu G
I don't believe telnet has an option to do this. If you can use ssh. Telnet should have died years ago, although it is useful for things like getting a repl on localhost.

You can try the following though. 
echo {password} | telnet -l {username}  host.com

This is not a node issue and is strictly related to telnet.

-Andrew

Marco Rogers

unread,
Mar 24, 2011, 3:58:01 PM3/24/11
to nod...@googlegroups.com, Bugzu G, Andrew Stone

@Bugzu Since you say you're new to node, I'll add a little info that might be helpful.  The net socket that you are creating is just a "dumb" socket.  Node doesn't give you any help in figuring out how to talk over that socket.  You can use it like a telnet connection.  But you'll have to parse the data coming from the other end and respond accordingly.  For example, expanding Isaac's example.

var conn = net.createConnection(port,host);
conn.on("connection", function (socket) {
  socket.on("data", function (c) {
    var data = c + ''; // make sure it's a string
    switch (data) {
      case 'login: ': // prompting you for a login
         socket.write('username');  // send username
         break;
      case 'password: ': // prompting you for password
          socket.write('supersekrit');  // send password
          break;
      case 'Invalid Username!':
          // handle this
          break;
      default:
          // do something else
          break;
    }
  })
  socket.on("end", function () {
    // ITS OVER!
  })
})


You can get data and write data to the socket as long as it's open. And you have to manage that communication yourself.  The important thing to note here is that data coming off the socket is in chunks. It's not necessarily going to be line by line, and almost certainly won't be suitable for a simple switch/case statement like this.  This is just to illustrate the idea.

Hope this helps.

:Marco

Bugzu G

unread,
Mar 25, 2011, 8:30:39 AM3/25/11
to Andrew Stone, nod...@googlegroups.com
Thanks all for replying.

what i am doing is below creating a conn. with freechess.org

var port = 5000, host = "freechess.org";

var net = require('net');

var fics = net.createConnection(port,host);

    fics.addListener('connect',function(){
        var input = process.openStdin(),
            output = process.stdout;

        sys.puts('Connected to FICS');

        fics.addListener('data',function(chunk){
            //here client is browser...i am doing this using socket.io
            client.send(chunk.toString('utf8',0,chunk.length));
        })
    });


what i want to do is send username and password to freechess.org without user interaction.


Thanks,
bugzu

Bugzu G

unread,
Mar 25, 2011, 8:33:42 AM3/25/11
to nodejs
As Marcos pointed socket.write...but it didnt work.
Is there any other way to accomplish this.

Thanks All,
bugzu

Jann Horn

unread,
Mar 25, 2011, 1:51:10 PM3/25/11
to nod...@googlegroups.com
Am Freitag, den 25.03.2011, 18:00 +0530 schrieb Bugzu G:
> fics.addListener('connect',function(){
> var input = process.openStdin(),
> output = process.stdout;
>
> sys.puts('Connected to FICS');
>
> fics.addListener('data',function(chunk){
> //here client is browser...i am doing this using socket.io
> client.send(chunk.toString('utf8',0,chunk.length));
> })
> });
> what i want to do is send username and password to freechess.org without
> user interaction.

Try to write to "fics" when the "connect" event fires, maybe it will
work. If it doesn't, you probably have to look through the incoming
chunks until you get a login prompt, send your username, wait for the
password prompt and send the password.

Jann

signature.asc
Reply all
Reply to author
Forward
0 new messages