Search code examples
javascriptnode.jstelnet

NodeJS Cannot receive data from telnet socket after writing to it


when I connect I get the initial data as if I opened the telnet manually from the CMD, but I'm trying to send a command and it doesn't seem to be working as it's not logging the data. I looked everywhere and I'm not sure what to do.

Here is the code I used

 const conn = new net.Socket();
            conn.connect({port: 587, host: mxOrDomain}, () => {
                conn.setEncoding('utf8');
                conn.on('data', (data) => {
                    const message = data.toString();
                    console.log(message);

                });
                conn.on('error', (data) => {
                    console.log('error : ', data);
                });

                conn.on('drain', () => {
                    console.log('drained');
                });

                conn.on('end', () => {
                    console.log('Ended');
                });

                setTimeout(() => {
                    conn.write('HELO blahbalh.com\\r\\n', 'utf8');
                }, 2000);
            });

Also, is there a reason why telnet {domain} 587 works in CMD and not when using cp.exec() or .spawn() ?


Solution

  • Turns out you have to end your write string with \r\n but my IDE put \\r\\n by default.