全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
12
返回列表 发新帖
楼主: fxzx
打印 上一主题 下一主题

问一个傻逼问题

[复制链接]
11#
发表于 2019-10-15 20:28:42 | 只看该作者
tonyma 发表于 2019-10-15 20:24
用python吧,readlines() 开到机器邮箱通知,

1、首先要输出执行.sh脚本并输出返回的结果在一个.txt里面( ...

直接用python执行cli然后获取stdout和stderr就行
12#
发表于 2019-10-15 20:36:59 | 只看该作者
yyuueexxiinngg 发表于 2019-10-15 20:28
直接用python执行cli然后获取stdout和stderr就行


嗯,没试过,
上面这个中间生成了一个.txt,cat 可以看下里面的输出内容也容易判断返回结果怎么样的
13#
发表于 2019-10-15 20:45:44 | 只看该作者
nodejs版的,需要配置好cli,开好2个机器就会自动停止运行,之前用这个开到两个韩国机器
  1. const domainID = "YOUR_DOMAIN_ID";   // "xUCN:AP-SEOUL-1-AD-1"
  2. const imageID = "YOUR_IMAGE_ID";   // "ocid1.image.oc1.ap-seoul-1.xxxxxxxxxxxxxx"
  3. const subnetID = "YOUR_SUBNET_ID"; // "ocid1.subnet.oc1.ap-seoul-1.xxxxxxxxxxxxxx"
  4. const shape = "YOUR_SHAPE_ID";  // "VM.Standard.E2.1.Micro"
  5. const sshKey = "YOUR_SSH_KEY";  // "ssh-rsa xxxxxxxxxxxxxx"
  6. const compartmentID = "YOUR_COMPARTMENT_ID";  // "ocid1.tenancy.oc1..xxxxxxxxxxxxxx"
  7. const name1 = "free-kr1";
  8. const name2 = "free-kr2";
  9. const sleepTime = 5000; // ms


  10. const util = require("util");
  11. const exec = util.promisify(require("child_process").exec);
  12. const fs = require("fs");
  13. const readFile = fs.existsSync("./oracle-cloud.json") ? fs.readFileSync("./oracle-cloud.json") : `{"loopTime1":0,"loopTime2":0}`;
  14. const save = JSON.parse(readFile.toString());
  15. let loopTime1 = save.loopTime1;
  16. let loopTime2 = save.loopTime2;
  17. let isMachine1 = true;
  18. let machine1Finished = false;
  19. let machine2Finished = false;

  20. async function execute(callback) {
  21.   return new Promise(async resolve => {
  22.     try {
  23.       if (isMachine1) {
  24.         if (machine1Finished) {
  25.           resolve(false);
  26.           return;
  27.         }
  28.         loopTime1++;
  29.       } else {
  30.         if (machine2Finished) {
  31.           resolve(false);
  32.           return;
  33.         }
  34.         loopTime2++;
  35.       }

  36.       const cmd = `oci compute instance launch --availability-domain ${domainID} --display-name ${
  37.         isMachine1 ? name1 : name2
  38.       } --image-id ${imageID} --subnet-id ${subnetID}  --shape ${shape} --assign-public-ip true --metadata '{"ssh_authorized_keys": "${sshKey}"}' --compartment-id ${compartmentID}`
  39.       const result = await exec(cmd);
  40.       callback(result, resolve);
  41.     } catch (err) {
  42.       callback(err, resolve);
  43.     }
  44.   });
  45. }

  46. function sleep(ms) {
  47.   return new Promise(resolve => {
  48.     setTimeout(resolve, ms);
  49.   });
  50. }

  51. function callback(result, resolve) {
  52.   console.log(
  53.     `${name1}: ${loopTime1} ${
  54.       machine1Finished ? "Finished!" : "Creating"
  55.     }        ${name2}: ${loopTime2} ${machine2Finished ? "Finished!" : "Creating"}`
  56.   );
  57.   if (result.stderr) {
  58.     console.log(`Err: ${isMachine1 ? name1 : name2}`);
  59.     const jsonGroup = result.stderr
  60.       .toString()
  61.       .match(/\{(?:[^{}]|(\{(?:[^{}]|(\{[^{}]*\}))*\}))*\}/);
  62.     try {
  63.       const json = JSON.parse(jsonGroup[0]);
  64.       console.log(json.status, json.message);
  65.     } catch (e) {
  66.     }
  67.     resolve(true);
  68.   } else if (result.stdout) {
  69.     if (isMachine1) {
  70.       machine1Finished = true;
  71.     } else {
  72.       machine2Finished = true;
  73.     }
  74.     console.log("Out");
  75.     resolve(true);
  76.   }
  77. }

  78. async function init() {
  79.   console.log("Started");
  80.   while (!machine1Finished || !machine2Finished) {
  81.     const isExecuted = await execute(callback);
  82.     isMachine1 = !isMachine1;
  83.     if (isExecuted) {
  84.       fs.writeFileSync(
  85.         "./oracle-cloud.json",
  86.         JSON.stringify({loopTime1: loopTime1, loopTime2: loopTime2})
  87.       );
  88.       await sleep(sleepTime);
  89.     }
  90.   }
  91. }

  92. init();
  93. // By yyuueexxiinngg
  94. // Link https://github.com/yyuueexxiinngg/some-scripts/blob/master/oracle/oracle-cloud.js
复制代码
15#
发表于 2019-10-15 20:53:20 | 只看该作者
fxzx 发表于 2019-10-15 20:09
那怎么把获取的这个状态码写到一个变量里呢?

$? 看 exit code 等于0就是成功 否则就是失败
16#
发表于 2019-10-15 21:29:31 来自手机 | 只看该作者
感谢分享收藏了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-12-16 03:40 , Processed in 0.067015 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表