deploy.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const fs = require("fs");
  2. const path = require("path");
  3. const { NodeSSH } = require("node-ssh");
  4. const vorpal = require("vorpal")();
  5. const ssh = new NodeSSH();
  6. let shell;
  7. ssh.connect({
  8. host: "10.5.17.151",
  9. username: "swjmrhb_ggfw/10.4.115.9/any",
  10. password: "EqRa6KSd2kF%C&mj",
  11. onKeyboardInteractive(
  12. name,
  13. instructions,
  14. instructionsLang,
  15. prompts,
  16. finish
  17. ) {
  18. console.log("🔊🚀🚀🚀🔊 ~ RollbackCommand ~", prompts);
  19. if (
  20. prompts.length > 0 &&
  21. prompts[0].prompt.toLowerCase().includes("password")
  22. ) {
  23. prompts([
  24. {
  25. type: "password",
  26. name: "newPassword",
  27. message: `input the password of ${hostItem}:`,
  28. },
  29. ]).then(({ newPassword }) => {
  30. finish([newPassword]);
  31. });
  32. }
  33. },
  34. })
  35. .then(() => {
  36. return ssh.requestShell();
  37. })
  38. .then((stream) => {
  39. shell = stream;
  40. shell
  41. .on("close", () => {
  42. console.log("Stream :: close");
  43. conn.end();
  44. })
  45. .on("data", (data) => {
  46. let str = data.toString();
  47. process.stdout.write(data);
  48. if (/^login\:/.test(str)) {
  49. console.log("enter name");
  50. stream.write("admin\n");
  51. }
  52. if (/^admin@.*password:/.test(str)) {
  53. console.log("enter password");
  54. shell.write("yWfuA@4NTX!b#2kG\n");
  55. setTimeout(() => {
  56. shell.write("su root\n");
  57. setTimeout(() => {
  58. shell.write("yWfuA@4NTX!b#2kG\n");
  59. setTimeout(() => {
  60. shell.write("./deploy.sh\n");
  61. }, 100);
  62. }, 100);
  63. }, 100);
  64. }
  65. });
  66. })
  67. .catch((e) => {
  68. console.log(e);
  69. });
  70. vorpal.command("su").action(function (args, callback) {
  71. shell.write("su root\n");
  72. setTimeout(() => {
  73. shell.write("yWfuA@4NTX!b#2kG\n");
  74. callback();
  75. }, 500);
  76. });
  77. vorpal.command("upload").action(function (args, callback) {
  78. ssh.putFiles([
  79. {
  80. local: "/Users/drew/Desktop/building3.jpeg",
  81. remote: "/home/admin/1.png",
  82. },
  83. ]).then(
  84. function () {
  85. console.log("The File thing is done");
  86. callback();
  87. },
  88. function (error) {
  89. console.log("Something's wrong");
  90. console.log(error);
  91. callback();
  92. }
  93. );
  94. });
  95. vorpal.command("ls").action(function (args, callback) {
  96. ssh.execCommand("ls").then(function (result) {
  97. console.log("STDOUT: " + result.stdout);
  98. console.log("STDERR: " + result.stderr);
  99. callback();
  100. });
  101. shell;
  102. });
  103. vorpal.delimiter("deploy$").show();