72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
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[];
|
|
}
|