Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 无法使用交互式脚本获取child_process.spawn的输出_Node.js_Spawn_Child Process - Fatal编程技术网

Node.js 无法使用交互式脚本获取child_process.spawn的输出

Node.js 无法使用交互式脚本获取child_process.spawn的输出,node.js,spawn,child-process,Node.js,Spawn,Child Process,我无法在以下代码中获得任何输出: var spawn = require('child_process').spawn, script = 'ftp', child = spawn(script); child.stdout.on('data', function (data) { console.log('stdout: ' + data); }); child.stderr.on('data', function (data) { console.log('std

我无法在以下代码中获得任何输出:

var spawn = require('child_process').spawn,
    script = 'ftp',
    child = spawn(script);

child.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

child.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

child.on('close', function (code) {
  console.log('child process exited with code ' + code);
});
它适用于“ls”、“pwd”等普通脚本,但不适用于“ftp”、“telnet”等交互式程序。有什么建议吗


编辑:

以另一个脚本为例:

#!/usr/bin/env python
name = raw_input("your name>")
print name
生成此脚本时,我希望获取带有数据事件的提示“yourname>”,以便以后可以在stdin中输入一些内容


问题是我在数据事件中什么也没有得到,而且似乎这些事件都没有被触发。

在交互模式中,有一个命令解释器,它从stdin读取用户输入,然后相应地打印输出。所以你必须写信给stdin做点什么。例如,使用
telnet
命令向代码中添加以下行:

child.stdin.write('?\n');
child.stdin.write('quit\n');
输出:

stdout: Commands may be abbreviated.  Commands are:

!               cr              mdir            proxy           send
$               delete          mget            sendport        site
account         debug           mkdir           put             size
append          dir             mls             pwd             status
ascii           disconnect      mode            quit            struct
bell            form            modtime         quote           system
binary          get             mput            recv            sunique
bye             glob            newer           reget           tenex
case            hash            nmap            rstatus         trace
ccc             help            nlist           rhelp           type
cd              idle            ntrans          rename          user
cdup            image           open            reset           umask
chmod           lcd             passive         restart         verbose
clear           ls              private         rmdir           ?
close           macdef          prompt          runique
cprotect        mdelete         protect         safe

child process exited with code 0

ls
cat
可通过输入输出和错误流进行控制

ftp
telnet
可通过tty间接控制

该协议也基于输入/输出流,但更为复杂。您可以使用可用的包来处理该协议

pty的作者有一些有趣的例子,他通过web套接字将pty转发到web,包括终端游戏:

这基本上归结为telnet和ftp自己处理输入和输出的事实。尝试查找这些程序的设置,以便它们可以在某种独立模式下执行任务。否则,我建议为任务使用特定的节点包:和@TheHippo我认为输出与child.stdout有关。事实上,如果在Linux shell中运行ftp命令,会出现提示:“ftp>”。我能从stdout的数据事件中得到这个提示吗?我想这不是telnet或ftp特有的。提示为tty。它所做的只是更改保存提示的tty环境的
PS1
var。如果您想要类似命令行解释器的功能,您应该使用。它有一个提示。只需创建一个readline实例并将其附加到ftp或telnet进程,同时设置提示。
var pty = require('pty.js');
var term = pty.spawn('ftp', [], options);

term.on('data', function(data) {
  console.log(data);
});

term.write(ftpCmd + '\r');