问题
Amusing node JS ssh2-sftp-client. I want to the list all the directory and its subdirectories in a given path?
let sftp = new ssh2SftpClient();
console.log(sftp);
sftp.connect({
host: 'xx.xxx.xxx.xxx',
port: '22',
username: 'centos',
privateKey: require('fs').readFileSync('/home/myHome/aws_int.ppk')
}).then(() => {
return sftp.list('/home/centos/myHome');
}).then((data) => {
console.log('the data info : ' + data);
for(i = 0; i < data.length; i++) {
console.log(data);
console.log(data[i].name);
}
}).catch((err) => {
console.log('catch error : ' + err);
}).catch(() => {
console.log('catch error : ' + err);
});
https://www.npmjs.com/package/ssh2-sftp-client
The above code returns only the directories in the given path but not its subdirectories.
回答1:
why are you using username/ password? sftp is supposed to use ssh keys. once the keys are shared, you dont need password.. you should be able to connect with the username.
来源:https://stackoverflow.com/questions/48589154/how-to-list-all-subdirectory-using-nodejs-sftp-client