Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Client = require('ssh2');
- var hostList = [
- {
- host: '23123123123213',
- username: 'root',
- password: 'OWPEFJEWOwefoijwepfjwepfowewefwefewfewfwe',
- port: 22
- },
- {
- host: '123123123123123123',
- username: 'root',
- password: 'waofwepjfoewfpwef',
- port: 22
- }
- ];
- var SSH = function(hosts) {
- this._hosts = hosts.map(function(host) {
- return new Host(host);
- });
- };
- SSH.prototype.run = function(cb) {
- this.callback = cb;
- }
- SSH.prototype.connect = function() {
- this._hosts.forEach(function(host) {
- host.connect(this);
- }.bind(this));
- };
- var Host = function(options) {
- this._client = new Client();
- this.host = options.host;
- this.port = options.port;
- this.username = options.username;
- this.password = options.password;
- };
- Host.prototype.connect = function(ssh) {
- var self = this;
- this._client.on('ready', function() {
- ssh.callback(self);
- });
- this._client.on('error', function(err) {
- if (err) console.log(err);
- });
- this._client.connect({
- host: this.host,
- username: this.username,
- password: this.password,
- port: this.port
- });
- };
- Host.prototype.cd = function(path, callback) {
- this.currentDir = path;
- this._client.sftp(function(err, sftp) {
- sftp.opendir(path, function(err, buffer) {
- sftp.readdir(buffer, function(err, list) {
- callback(err, list);
- });
- });
- });
- };
- var ConnGroup = new SSH(hostList);
- ConnGroup.run(function(conn) {
- conn.cd('/', function(err, list) {
- });
- });
- ConnGroup.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement