5

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.

3
  • This basically boils down to the fact that telnet and ftp handeling the input and output on their own. Try to find settings for these programs where they could perform their tasks in a kind of standalone mode. Commented Dec 11, 2013 at 9:36
  • Otherwise I would suggest using specific node packages for the tasks: npmjs.org/package/telnet and github.com/mscdex/node-ftp Commented Dec 11, 2013 at 9:38
  • @TheHippo I think the output is related with child.stdout. Commented Dec 11, 2013 at 9:57

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

2

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 

3 Comments

In fact, if you run ftp command in a Linux shell, there is a prompt: "ftp>". Can I get this prompt from stdout's data event?
I think that is not specific to telnet or ftp. Prompt is for the tty. All it does is it changes the tty environment's PS1 var which holds your prompt. If you want command line interpreter like functionality you should use readline. It has a prompt.
Simply create a readline instance and attach it to your ftp or telnet process, while setting the prompt.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.