我爱编程技术干货

Node 使用 SFTP 上传文件到服务器

2018-04-11  本文已影响456人  Havoc_Zhang

安装模块node-ssh

npm i node-ssh
Example
var path, node_ssh, ssh, fs

fs = require('fs')
path = require('path')
node_ssh = require('node-ssh')
ssh = new node_ssh()
const password = ''

//connect sftp
ssh.connect({
 host: '',
 username: '',
 port: 22,
 password,
 tryKeyboard: true,
 onKeyboardInteractive: (name, instructions, instructionsLang, prompts, finish) => {
     if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) {
       finish([password])
     }
   }
})

//upload file
.then(function() {
 ssh.putFile('localFile path', 'remotFile path').then(function() {
   console.log("The File thing is done")
 }, function(error) {
   console.log("Something's wrong")
   console.log(error)
 })
})
上一篇下一篇

猜你喜欢

热点阅读