【发布时间】:2015-11-12 14:04:29
【问题描述】:
我正在尝试使用节点 child_process 模块在 Windows 中执行命令。问题是有问题的应用程序(Windows 中的 ftp 命令行)提示输入用户名和密码。我尝试通过以下方式向命令提供询问的信息:
var ftp = spawn('ftp',['<<ip of server>>']);
var authenticated = false;
var commandsSended = false;
ftp.stdin.write("debug\n");
ftp.stdin.write(os.EOL);
ftp.stdout.on("data", function(data) {
console.log("FTPService: " + data.toString());
if(data.toString().indexOf("226 Transfer complete") > -1) {
ftp.stdin.write('bye\n');
} else if(data.toString().indexOf("Gebruiker") > -1) {
ftp.stdin.write("<<username of server>>");
ftp.stdin.write(os.EOL);
} else if(data.toString().indexOf("Wachtwoord") > -1) {
ftp.stdin.write("<<password of server>>");
ftp.stdin.write(os.EOL);
authenticated = true;
}
if(authenticated && !commandsSended) {
ftp.stdin.write("put " + filePath + " " + newName + "\n");
commandsSended = true;
}
});
ftp.on('exit', function(code) {
if(code > 0) {
console.log("FTPService closed with error " + code);
} else {
console.log("FTPService closed without error");
}
});
这不会导致错误,但根本不起作用。在linux中我没有问题,因为我可以将登录信息作为命令的参数发送。我知道 nodejs 有它自己的模块,但我需要让它与命令行一起工作(还需要执行其他命令,这也会导致需要填写提示)。
【问题讨论】: