Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/node
- var fs = require('fs');
- var ssh_client = require('ssh2').Client;
- var ip_list = './ip_list.txt';
- var ssh_config = {
- port: 22,
- username: 'username',
- password: 'password',
- readyTimeout: 5000 };
- var cmd = 'find /home/username -type f -iname "*.txt"';
- async function main() {
- let ip = '10.10.10.110';
- let result = await sshExec(ip);
- console.log(result[ip]);
- }
- function sshExec(ip) {
- return new Promise(function(resolve, reject) {
- let ssh = new ssh_client();
- let config = Object.assign(ssh_config, {'host': ip});
- let result = {};
- ssh.connect(config);
- ssh.on('ready', function() {
- console.log("Connected to " + ip + ".");
- ssh.exec(cmd, function(err, stream) {
- if (err) throw err;
- stream.on('data', function(data) {
- //console.log(data.toString());
- result[ip] = data.toString();
- }).stderr.on('data', function(data) {
- console.log('STDERR: ' + data);
- }).on('close', function(code, signal) {
- ssh.end();
- });
- });
- });
- ssh.on('error', function(e) {
- console.log(ip + ', connection failed, ' + e.message);
- });
- ssh.on('close', function(hadError) {
- if (!hadError) {
- console.log('Connection to ' + ip + ' closed without errors.');
- resolve(result);
- }
- else {
- console.log('Connection to ' + ip + ' closed with errors.');
- reject(result.ip = 'failure');
- }
- });
- });
- }
- main();
Add Comment
Please, Sign In to add comment