Guest User

Untitled

a guest
Feb 18th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #!/usr/local/bin/node
  2. var fs = require('fs');
  3. var ssh_client = require('ssh2').Client;
  4.  
  5.  
  6. var ip_list = './ip_list.txt';
  7. var ssh_config = {
  8. port: 22,
  9. username: 'username',
  10. password: 'password',
  11. readyTimeout: 5000 };
  12. var cmd = 'find /home/username -type f -iname "*.txt"';
  13.  
  14.  
  15. async function main() {
  16. let ip = '10.10.10.110';
  17. let result = await sshExec(ip);
  18. console.log(result[ip]);
  19. }
  20.  
  21.  
  22. function sshExec(ip) {
  23. return new Promise(function(resolve, reject) {
  24. let ssh = new ssh_client();
  25. let config = Object.assign(ssh_config, {'host': ip});
  26. let result = {};
  27.  
  28. ssh.connect(config);
  29.  
  30. ssh.on('ready', function() {
  31. console.log("Connected to " + ip + ".");
  32. ssh.exec(cmd, function(err, stream) {
  33. if (err) throw err;
  34. stream.on('data', function(data) {
  35. //console.log(data.toString());
  36. result[ip] = data.toString();
  37. }).stderr.on('data', function(data) {
  38. console.log('STDERR: ' + data);
  39. }).on('close', function(code, signal) {
  40. ssh.end();
  41. });
  42. });
  43. });
  44.  
  45. ssh.on('error', function(e) {
  46. console.log(ip + ', connection failed, ' + e.message);
  47. });
  48.  
  49. ssh.on('close', function(hadError) {
  50. if (!hadError) {
  51. console.log('Connection to ' + ip + ' closed without errors.');
  52. resolve(result);
  53. }
  54. else {
  55. console.log('Connection to ' + ip + ' closed with errors.');
  56. reject(result.ip = 'failure');
  57. }
  58. });
  59. });
  60. }
  61.  
  62.  
  63. main();
Add Comment
Please, Sign In to add comment