ssh.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const { Client } = require("ssh2");
  2. const dotenv = require("dotenv").config();
  3. const client1 = new Client();
  4. const client2 = new Client();
  5. function booleToGrove(){
  6. client1.on('ready', () => {
  7. console.log('FIRST :: connection ready');
  8. client1.forwardOut(process.env.BOOLE, 12345, process.env.GROVE, 22, (err, stream) => {
  9. if (err) {
  10. console.log('FIRST :: forwardOut error: ' + err);
  11. return conn1.end();
  12. }
  13. client2.connect({
  14. sock: stream,
  15. username: process.env.SSH_USER,
  16. password: process.env.SSH_PASSWORD,
  17. tryKeyboard: true,
  18. });
  19. });
  20. }).connect({
  21. host: process.env.BOOLE,
  22. username: process.env.SSH_USER,
  23. password: process.env.SSH_PASSWORD,
  24. });
  25. }
  26. function startGrove(){
  27. client2.on('ready', () => {
  28. // This connection is the one to 10.1.1.40
  29. console.log('SECOND :: connection ready');
  30. client2.shell('uptime', (err, stream) => {
  31. if (err) {
  32. console.log('SECOND :: exec error: ' + err);
  33. return client1.end();
  34. }
  35. stream.on('close', () => {
  36. client1.end(); // close parent (and this) connection
  37. }).on('data', (data) => {
  38. console.log(data.toString());
  39. });
  40. });
  41. })
  42. //return client2;
  43. }
  44. module.exports = {
  45. //startBoole,
  46. startGrove,
  47. //scpToBoole,
  48. booleToGrove,
  49. }