server.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const node_ssh = require("node-ssh");
  2. const config = require("./config");
  3. const path = require("path");
  4. const fs = require("fs");
  5. console.log(process.argv.join(" "));
  6. let host = process.argv[2];
  7. let index = process.argv[3];
  8. console.log(index + "号机 " + host);
  9. let jump_server = new node_ssh();
  10. jump_server
  11. .connect({
  12. host: "173.242.117.220",
  13. username: "root",
  14. password: "6EdUaZZCs63l",
  15. port: 26160
  16. })
  17. .then(client => {
  18. client.connection.forwardOut(
  19. "127.0.0.1",
  20. 12345 + index,
  21. "47.244.39.44",
  22. 22,
  23. function(err, stream) {
  24. if (err) {
  25. console.log("跳板机连接失败" + err);
  26. return jump_server.end();
  27. }
  28. console.log("跳板机连接成功");
  29. start(stream);
  30. }
  31. );
  32. });
  33. function start(stream) {
  34. ssh = new node_ssh();
  35. let conf = {
  36. host: host,
  37. username: "root",
  38. port: 22,
  39. password: config.password
  40. };
  41. if (stream) {
  42. conf = {
  43. sock: stream,
  44. username: "root",
  45. password: config.password
  46. };
  47. }
  48. ssh.connect(conf)
  49. .then(async function() {
  50. let option = {
  51. cwd: "/root",
  52. onStdout(chunk) {
  53. console.log(chunk.toString("utf8"));
  54. },
  55. onStderr(chunk) {
  56. console.log(chunk.toString("utf8"));
  57. }
  58. };
  59. const failed = [];
  60. const successful = [];
  61. let status = await ssh.putDirectory(__dirname, "/root/autoTrade", {
  62. recursive: true,
  63. concurrency: 30,
  64. validate: function(itemPath) {
  65. const baseName = path.basename(itemPath);
  66. return (
  67. baseName.substr(0, 1) !== "." &&
  68. baseName !== "node_modules" // do not allow dot files
  69. ); // do not allow node_modules
  70. },
  71. tick: function(localPath, remotePath, error) {
  72. if (error) {
  73. failed.push(localPath);
  74. } else {
  75. successful.push(localPath);
  76. }
  77. }
  78. });
  79. console.log(
  80. "the directory transfer was",
  81. status ? "successful" : "unsuccessful"
  82. );
  83. console.log("failed transfers", failed.join("\n"));
  84. console.log("successful transfers", successful.join("\n"));
  85. let nodeInstalled = false;
  86. try {
  87. await ssh
  88. .exec("node", ["-v"], {
  89. cwd: "/root",
  90. onStdout(chunk) {
  91. console.log(chunk.toString("utf8"));
  92. },
  93. onStderr(chunk) {
  94. console.log(chunk.toString("utf8"));
  95. }
  96. })
  97. .catch(e => {
  98. console.log(e);
  99. });
  100. } catch (e) {
  101. console.log("安装node");
  102. await ssh.execCommand(
  103. "curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -",
  104. option
  105. );
  106. await ssh.execCommand("sudo apt-get install -y nodejs", option);
  107. console.log("node安装完成");
  108. }
  109. await ssh
  110. .execCommand("npm install", {
  111. ...option,
  112. cwd: "/root/autoTrade"
  113. })
  114. .catch(e => {
  115. console.log("npm install failed");
  116. });
  117. await ssh
  118. .execCommand("ls -la", { ...option, cwd: "/root/autoTrade" })
  119. .catch(e => {
  120. console.log("ls -la failed");
  121. });
  122. await ssh
  123. .execCommand("killall node", {
  124. ...option,
  125. cwd: "/root/autoTrade"
  126. })
  127. .catch(e => {
  128. console.log("killall node failed");
  129. });
  130. await ssh
  131. .execCommand(`node index.js ${index}`, {
  132. ...option,
  133. cwd: "/root/autoTrade"
  134. })
  135. .catch(e => {
  136. console.log("node index.js failed");
  137. });
  138. })
  139. .catch(e => {
  140. console.log(e);
  141. });
  142. }