首页 > 解决方案 > 使用 Node.js 套接字代码提交的 FTP 命令未处理

问题描述

我有以下使用 telnet 的 FTP 会话:

$ telnet ftp.fsn.hu 21
Trying 195.228.252.133...
Connected to ftp.fsn.hu.
Escape character is '^]'.
220 ftp.fsn.hu FTP server (Version 6.00LS) ready.
USER anonymous
331 Guest login ok, send your email address as password.
PASS joe@example.com
230 Guest login ok, access restrictions apply.

我试图自动化这个:

var net = require('net');

var sock = net.Socket();

var regex220 = /^220/;

var regex331 = /^331/;

var regex230 = /^230/;

sock.on('data', function (buffer)
  { console.log('data received:\n' + buffer);

    if (buffer.toString().match(regex220) !== null)
    {
      sock.write('USER anonymous');
      console.log("sent: 'USER anonymous'");
    } else if (buffer.toString().match(regex331) !== null)
    {
      sock.write('PASS joe@example.com');
      console.log("sent: 'PASS joe@example.com'");
    } else if (buffer.toString().match(regex230) !== null)
    {
      console.log('LOGGED IN');
    }
  });

sock.connect(21, 'ftp.fsn.hu');

在 Fedora 32 和 Node.js 12.16.1 上运行它我得到

$ node fsn.js
data received:
220 ftp.fsn.hu FTP server (Version 6.00LS) ready.

sent: 'USER anonymous'

它挂在那里。

为什么这个节目没有通过第二轮和第三轮?

标签: node.jssocketseventsftp

解决方案


您必须使用 CRLF 终止(提交)命令:

sock.write('USER anonymous\r\n');

推荐阅读