From aa226ce052b549d1173a4604ed8f51d3db3b94e5 Mon Sep 17 00:00:00 2001 From: Mees van der Wijk Date: Tue, 4 Apr 2023 13:04:00 +0200 Subject: [PATCH] ~ Initial commit --- .gitignore | 2 + defaultConfig.json | 6 ++ index.js | 64 ++++++++++++++++++ index.ts | 71 ++++++++++++++++++++ package-lock.json | 161 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 17 +++++ tsconfig.json | 9 +++ 7 files changed, 330 insertions(+) create mode 100644 .gitignore create mode 100644 defaultConfig.json create mode 100644 index.js create mode 100644 index.ts create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ecdd212 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules/ +config.json \ No newline at end of file diff --git a/defaultConfig.json b/defaultConfig.json new file mode 100644 index 0000000..451a4ab --- /dev/null +++ b/defaultConfig.json @@ -0,0 +1,6 @@ +{ + "addresses": [], + "username": "ubnt", + "password": "ubnt", + "payload": [] +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..c6f87a0 --- /dev/null +++ b/index.js @@ -0,0 +1,64 @@ +var _a = require('fs-extra'), pathExists = _a.pathExists, copy = _a.copy, readJSON = _a.readJSON; +var join = require('path').join; +var Client = require('ssh2').Client; +var configPath = join(__filename, '..', 'config.json'); +var defaultConfigPath = join(__filename, '..', 'defaultConfig.json'); +var config = null; +pathExists(configPath, function (exists, err) { + if (err) + throw err; + if (exists) { + readJSON(config, function (configJson, err) { + if (err) + throw err; + config = configJson; + startPrepare(); + }); + } + else + copy(defaultConfigPath, configPath, function (err) { + if (err) + throw err; + console.log("----- Default config has been copied -----"); + }); +}); +function startPrepare() { + console.log("----- STARTING PREPARE ON ".concat(config.addresses.length, " DEVICES -----")); + (function handle(i) { + if (i === void 0) { i = 0; } + if (config.addresses[i]) { + var address = config.addresses[i]; + console.log("Starting on device ".concat(address)); + var ip = address.split(':')[0]; + var port = address.split(':')[1] != undefined ? address.split(':')[1] : 22; + var conn_1 = new Client(); + conn_1 + .on('ready', function () { + console.log(' Client :: ready'); + conn_1.shell(function (err, stream) { + if (err) + throw err; + stream + .on('close', function () { + console.log(' Stream :: close'); + conn_1.end(); + console.log("Finished device ".concat(address)); + handle(i + 1); + }) + .on('data', function (data) { + console.log(' Output: ' + data); + }); + stream.end("".concat(config.payload.join('\n'), "\nexit\n")); + }); + }) + .connect({ + host: ip, + port: port, + username: config.username, + password: config.password + }); + } + else + console.log('----- FINISHED ALL DEVICES -----'); + })(); +} diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..7bb3298 --- /dev/null +++ b/index.ts @@ -0,0 +1,71 @@ +const { pathExists, copy, readJSON } = require('fs-extra'); +const { join } = require('path'); +const { Client } = require('ssh2'); + +var configPath = join(__filename, '..', 'config.json'); +var defaultConfigPath = join(__filename, '..', 'defaultConfig.json'); + +var config: Config = null; + +pathExists(configPath, (exists, err) => { + if (err) throw err; + if (exists) { + readJSON(config, (configJson, err) => { + if (err) throw err; + config = configJson; + + startPrepare(); + }); + } else + copy(defaultConfigPath, configPath, (err) => { + if (err) throw err; + console.log(`----- Default config has been copied -----`); + }); +}); + +function startPrepare() { + console.log(`----- STARTING PREPARE ON ${config.addresses.length} DEVICES -----`); + (function handle(i = 0) { + if (config.addresses[i]) { + var address = config.addresses[i]; + + console.log(`Starting on device ${address}`); + + var ip = address.split(':')[0]; + var port = address.split(':')[1] != undefined ? address.split(':')[1] : 22; + + const conn = new Client(); + conn + .on('ready', () => { + console.log(' Client :: ready'); + conn.shell((err, stream) => { + if (err) throw err; + stream + .on('close', () => { + console.log(' Stream :: close'); + conn.end(); + console.log(`Finished device ${address}`); + handle(i + 1); + }) + .on('data', (data) => { + console.log(' Output: ' + data); + }); + stream.end(`${config.payload.join('\n')}\nexit\n`); + }); + }) + .connect({ + host: ip, + port: port, + username: config.username, + password: config.password + }); + } else console.log('----- FINISHED ALL DEVICES -----'); + })(); +} + +interface Config { + addresses: string[]; + username: string; + password: string; + payload: string[]; +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..359a9af --- /dev/null +++ b/package-lock.json @@ -0,0 +1,161 @@ +{ + "name": "ubiquitypreparetool", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "ubiquitypreparetool", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "@types/fs-extra": "^11.0.1", + "fs-extra": "^11.1.1", + "ssh2": "^1.11.0" + }, + "devDependencies": { + "@types/ssh2": "^1.11.9" + } + }, + "node_modules/@types/fs-extra": { + "version": "11.0.1", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-11.0.1.tgz", + "integrity": "sha512-MxObHvNl4A69ofaTRU8DFqvgzzv8s9yRtaPPm5gud9HDNvpB3GPQFvNuTWAI59B9huVGV5jXYJwbCsmBsOGYWA==", + "dependencies": { + "@types/jsonfile": "*", + "@types/node": "*" + } + }, + "node_modules/@types/jsonfile": { + "version": "6.1.1", + "resolved": "https://registry.npmjs.org/@types/jsonfile/-/jsonfile-6.1.1.tgz", + "integrity": "sha512-GSgiRCVeapDN+3pqA35IkQwasaCh/0YFH5dEF6S88iDvEn901DjOeH3/QPY+XYP1DFzDZPvIvfeEgk+7br5png==", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/node": { + "version": "18.15.11", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.11.tgz", + "integrity": "sha512-E5Kwq2n4SbMzQOn6wnmBjuK9ouqlURrcZDVfbo9ftDDTFt3nk7ZKK4GMOzoYgnpQJKcxwQw+lGaBvvlMo0qN/Q==" + }, + "node_modules/@types/ssh2": { + "version": "1.11.9", + "resolved": "https://registry.npmjs.org/@types/ssh2/-/ssh2-1.11.9.tgz", + "integrity": "sha512-XYMED+cBW11DfMidB/BlCcTzBsSAccaHLvdtdlVWGgDp7TlweGIfb+gHEalKptzVS2qb3jLoR0XrGCWljrIV9A==", + "dev": true, + "dependencies": { + "@types/node": "^18.11.18" + } + }, + "node_modules/asn1": { + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz", + "integrity": "sha512-ix/FxPn0MDjeyJ7i/yoHGFt/EX6LyNbxSEhPPXODPL+KB0VPk86UYfL0lMdy+KCnv+fmvIzySwaK5COwqVbWTQ==", + "dependencies": { + "safer-buffer": "~2.1.0" + } + }, + "node_modules/bcrypt-pbkdf": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", + "integrity": "sha512-qeFIXtP4MSoi6NLqO12WfqARWWuCKi2Rn/9hJLEmtB5yTNr9DqFWkJRCf2qShWzPeAMRnOgCrq0sg/KLv5ES9w==", + "dependencies": { + "tweetnacl": "^0.14.3" + } + }, + "node_modules/buildcheck": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/buildcheck/-/buildcheck-0.0.3.tgz", + "integrity": "sha512-pziaA+p/wdVImfcbsZLNF32EiWyujlQLwolMqUQE8xpKNOH7KmZQaY8sXN7DGOEzPAElo9QTaeNRfGnf3iOJbA==", + "optional": true, + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/cpu-features": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/cpu-features/-/cpu-features-0.0.4.tgz", + "integrity": "sha512-fKiZ/zp1mUwQbnzb9IghXtHtDoTMtNeb8oYGx6kX2SYfhnG0HNdBEBIzB9b5KlXu5DQPhfy3mInbBxFcgwAr3A==", + "hasInstallScript": true, + "optional": true, + "dependencies": { + "buildcheck": "0.0.3", + "nan": "^2.15.0" + }, + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/fs-extra": { + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.1.1.tgz", + "integrity": "sha512-MGIE4HOvQCeUCzmlHs0vXpih4ysz4wg9qiSAu6cd42lVwPbTM1TjV7RusoyQqMmk/95gdQZX72u+YW+c3eEpFQ==", + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=14.14" + } + }, + "node_modules/graceful-fs": { + "version": "4.2.11", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", + "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==" + }, + "node_modules/jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "dependencies": { + "universalify": "^2.0.0" + }, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, + "node_modules/nan": { + "version": "2.17.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz", + "integrity": "sha512-2ZTgtl0nJsO0KQCjEpxcIr5D+Yv90plTitZt9JBfQvVJDS5seMl3FOvsh3+9CoYWXf/1l5OaZzzF6nDm4cagaQ==", + "optional": true + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "node_modules/ssh2": { + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/ssh2/-/ssh2-1.11.0.tgz", + "integrity": "sha512-nfg0wZWGSsfUe/IBJkXVll3PEZ//YH2guww+mP88gTpuSU4FtZN7zu9JoeTGOyCNx2dTDtT9fOpWwlzyj4uOOw==", + "hasInstallScript": true, + "dependencies": { + "asn1": "^0.2.4", + "bcrypt-pbkdf": "^1.0.2" + }, + "engines": { + "node": ">=10.16.0" + }, + "optionalDependencies": { + "cpu-features": "~0.0.4", + "nan": "^2.16.0" + } + }, + "node_modules/tweetnacl": { + "version": "0.14.5", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz", + "integrity": "sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA==" + }, + "node_modules/universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", + "engines": { + "node": ">= 10.0.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..889cb4e --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "ubiquitypreparetool", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": {}, + "author": "", + "license": "ISC", + "dependencies": { + "@types/fs-extra": "^11.0.1", + "fs-extra": "^11.1.1", + "ssh2": "^1.11.0" + }, + "devDependencies": { + "@types/ssh2": "^1.11.9" + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f8da0a9 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "watch": true, + "outFile": "index.js" + }, + "include": [ + "index.ts" + ] +} \ No newline at end of file