Advertisement
Guest User

Untitled

a guest
Jan 20th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. var Client = require('ssh2');
  2.  
  3. var hostList = [
  4. {
  5. host: '23123123123213',
  6. username: 'root',
  7. password: 'OWPEFJEWOwefoijwepfjwepfowewefwefewfewfwe',
  8. port: 22
  9. },
  10. {
  11. host: '123123123123123123',
  12. username: 'root',
  13. password: 'waofwepjfoewfpwef',
  14. port: 22
  15. }
  16. ];
  17.  
  18.  
  19. var SSH = function(hosts) {
  20. this._hosts = hosts.map(function(host) {
  21. return new Host(host);
  22. });
  23. };
  24.  
  25. SSH.prototype.run = function(cb) {
  26. this.callback = cb;
  27. }
  28.  
  29. SSH.prototype.connect = function() {
  30. this._hosts.forEach(function(host) {
  31. host.connect(this);
  32. }.bind(this));
  33. };
  34.  
  35. var Host = function(options) {
  36. this._client = new Client();
  37. this.host = options.host;
  38. this.port = options.port;
  39. this.username = options.username;
  40. this.password = options.password;
  41. };
  42.  
  43. Host.prototype.connect = function(ssh) {
  44. var self = this;
  45. this._client.on('ready', function() {
  46. ssh.callback(self);
  47. });
  48.  
  49. this._client.on('error', function(err) {
  50. if (err) console.log(err);
  51. });
  52. this._client.connect({
  53. host: this.host,
  54. username: this.username,
  55. password: this.password,
  56. port: this.port
  57. });
  58. };
  59.  
  60. Host.prototype.cd = function(path, callback) {
  61. this.currentDir = path;
  62. this._client.sftp(function(err, sftp) {
  63. sftp.opendir(path, function(err, buffer) {
  64. sftp.readdir(buffer, function(err, list) {
  65. callback(err, list);
  66. });
  67. });
  68. });
  69. };
  70.  
  71. var ConnGroup = new SSH(hostList);
  72.  
  73.  
  74. ConnGroup.run(function(conn) {
  75. conn.cd('/', function(err, list) {
  76. });
  77. });
  78.  
  79.  
  80. ConnGroup.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement