Unable to get child_process.spawn output with interactive scripts

I can not get any output in the following code:

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);
});

It works for regular scripts like 'ls', 'pwd', etc. But not for interactive programs such as 'ftp', 'telnet'. Any suggestions?


Edit:

Take another script, for example:

#!/usr/bin/env python
name = raw_input("your name>")
print name

When this script appears, I want to get a "your name" prompt with a data event so that I can contribute something to stdin.

The problem is that I received nothing in the data event, and it seemed that none of these events were triggered.

+4
source share
2 answers

ls, cat .

ftp, telnet tty.

/, . .

https://github.com/chjj/pty.js

var pty = require('pty.js');
var term = pty.spawn('ftp', [], options);

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

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

pty , pty - -, : https://github.com/chjj/tty.js

+3

, 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
+2

All Articles