我写了一个测试scp传输的代码。这是代码。
var async = require('async'),
nexpect = require('nexpect'),
arg = {
'host' : '192.168.0.3',
'username' : 'root',
'password' : 'rootpwd',
'path' : '~'
},
file_list = ['a.txt', 'b.txt', 'c.txt'];
function scpFileTransfer(arg, callback) {
nexpect.spawn('scp ' + arg.file + ' ' + arg.username + '@' + arg.host + ':' + arg.path, { stream: 'stderr' })
.wait(/password/)
.sendline(arg.password)
.run(function (err) {
if(err) console.log(err);
else console.log('from ' + arg.file + ' to ' + arg.username + '@' + arg.host + ':' + arg.path + ' success!');
callback();
}
);
}
async.eachSeries(file_list, function(item, callback) {
arg.file = item;
scpFileTransfer(arg, function () {
callback();
});
}, function (err) {
if(err) console.trace(err);
else console.log('success');
});
我期望这样的输出,
from a.txt to root@192.168.0.3:~ success!
from b.txt to root@192.168.0.3:~ success!
from c.txt to root@192.168.0.3:~ success!
但输出与我的预期不同。我的 node.js 模块正在等待命令行输入。如何在没有命令行输入的情况下运行我的代码?