Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get output of child_process.spawn with interactive scripts

I cannot 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 normal scripts such as '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 spawn this script, I wish to fetch the prompt "your name>" with the data event, so that I can latter input something into stdin.

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

like image 706
Dong Avatar asked Dec 11 '13 09:12

Dong


People also ask

How to use spawn child process?

Spawned Child Processes. The spawn function launches a command in a new process and we can use it to pass that command any arguments. For example, here's code to spawn a new process that will execute the pwd command. const { spawn } = require('child_process'); const child = spawn('pwd');

What does exec() do in NodeJS?

The exec() function in Node. js creates a new shell process and executes a command in that shell. The output of the command is kept in a buffer in memory, which you can accept via a callback function passed into exec() .

What is child_ process in Node?

Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.


2 Answers

ls, cat is controllable via input output and error stream.

ftp, telnet is controllable indirectly via tty.

The protocol is also base on input/output stream but it is more complicated. You can use available package to handle that protocol.

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

The author of pty have some interesting examples, he forward pty to web via web socket, including terminal games: https://github.com/chjj/tty.js

like image 167
damphat Avatar answered Sep 22 '22 23:09

damphat


In interactive mode there is a command interpreter that reads user input from stdin, then accodingly prints output. So you have to write to stdin to do something. For example add following lines to your code with telnet command:

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

Output:

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
like image 27
user568109 Avatar answered Sep 20 '22 23:09

user568109