- Install NodeJS
- Install ssh2 (npm install ssh2)
- Fill the parameters at the top of the file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'), | |
client = require('ssh2').Client; | |
upoladFile({ | |
localFolder: "", | |
localFile: "", | |
remoteFolder: "", | |
remoteFile: "", | |
host: "", | |
port: 0, | |
username: "", | |
password: "" | |
}); | |
function upoladFile(p) { | |
var conn = new client(); | |
conn.on('ready', function () { | |
conn.sftp(function (err, sftp) { | |
if (err) throw err; | |
sftp.readdir(p.remoteFolder, function (err, list) { | |
if (err) throw err; | |
console.dir(list); | |
var readStream = fs.createReadStream(p.localFolder + p.localFile); | |
var writeStream = sftp.createWriteStream(p.remoteFolder + p.remoteFile, { mode: 0100664 }); | |
writeStream.on('close', function () { | |
sftp.readdir(p.remoteFolder, function (err, list) { | |
if (err) throw err; | |
console.dir(list); | |
conn.end(); | |
}); | |
}); | |
readStream.pipe(writeStream); | |
}); | |
}); | |
}).connect({ host: p.host, port: p.port, username: p.username, password: p.password }); | |
} |
No comments :
Post a Comment