全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 1555|回复: 15
打印 上一主题 下一主题

问一个傻逼问题

[复制链接]
跳转到指定楼层
1#
发表于 2019-10-15 19:46:55 来自手机 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
撸甲骨文的API脚本如果撸到了,返回的状态码是多少?(没机器是500,请求过多是429)
推荐
 楼主| 发表于 2019-10-15 19:51:37 来自手机 | 只看该作者
undefined 发表于 2019-10-15 19:50
不用判断,1分钟一次就请求吧

我半分钟一次
16#
发表于 2019-10-15 21:29:31 来自手机 | 只看该作者
感谢分享收藏了
15#
发表于 2019-10-15 20:53:20 | 只看该作者
fxzx 发表于 2019-10-15 20:09
那怎么把获取的这个状态码写到一个变量里呢?

$? 看 exit code 等于0就是成功 否则就是失败
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
复制代码
12#
发表于 2019-10-15 20:36:59 | 只看该作者
yyuueexxiinngg 发表于 2019-10-15 20:28
直接用python执行cli然后获取stdout和stderr就行


嗯,没试过,
上面这个中间生成了一个.txt,cat 可以看下里面的输出内容也容易判断返回结果怎么样的
11#
发表于 2019-10-15 20:28:42 | 只看该作者
tonyma 发表于 2019-10-15 20:24
用python吧,readlines() 开到机器邮箱通知,

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

直接用python执行cli然后获取stdout和stderr就行
10#
发表于 2019-10-15 20:24:17 | 只看该作者
本帖最后由 tonyma 于 2019-10-15 21:01 编辑

用python吧,readlines() 开到机器邮箱通知,

1、首先要contab定时执行.sh脚本并输出返回的结果在一个.txt里面(代码:/bin/bash /root/oci.sh >/var/tmp/test.txt 2>&1)
2、python 代码读取.txt文件,readlines(),判断返回结果行数组中的关键字error(一般没库存和其他错误内容都是返回一个error)
3、oci.sh (
                  第一行: oci compute instance launch --availability-domain ULJk:AP-SEOUL-1-AD-1 --image(这里是oci 命令)
                  第二行: python jsontest.py
                 )


  1. # -*- coding: UTF-8 -*-
  2. import json
  3. import io
  4. import smtplib
  5. from email.mime.text import MIMEText
  6. def success_mail():
  7.   mail_host = 'smtp.qq.com' #发送发服务器地址
  8.   mail_user = '[email protected]'#发送方邮箱
  9.   mail_pass = '###' #发送方密码
  10.   sender = '[email protected]'
  11.   receivers = ['[email protected]']
  12.   message = MIMEText('successfully open the  machine','plain','utf-8')#发送的内容
  13.   message['Subject'] = 'congratulations!'
  14.   message['From'] = sender
  15.   message['To'] = receivers[0]

  16.   try:
  17.     smtpObj = smtplib.SMTP_SSL("smtp.qq.com",465)
  18.     smtpObj.login(mail_user,mail_pass)
  19.     smtpObj.sendmail(
  20.         sender,receivers,message.as_string())
  21.     smtpObj.quit()
  22.     print('success')
  23.   except smtplib.SMTPException as e:
  24.     print('error',e)
  25. def fail_mail():
  26.   mail_host = 'smtp.qq.com'#发送发服务器地址
  27.   mail_user = '[email protected]'#发送方邮箱
  28.   mail_pass = '###' #发送方密码
  29.   sender = '[email protected]'
  30.   receivers = ['[email protected]']#接收方
  31.   message = MIMEText('fail to open the  machine','plain','utf-8') #发送的内容
  32.   message['Subject'] = 'fail!'
  33.   message['From'] = sender
  34.   message['To'] = receivers[0]


  35.   try:
  36.     smtpObj = smtplib.SMTP_SSL("smtp.qq.com",465)

  37.     smtpObj.login(mail_user,mail_pass)

  38.     smtpObj.sendmail(
  39.         sender,receivers,message.as_string())

  40.     smtpObj.quit()
  41.     print('success')
  42.   except smtplib.SMTPException as e:
  43.     print('error',e)

  44. f = open('/var/tmp/test.txt','r')
  45. lines=f.readlines()
  46. if 'Error' not in lines[2]:
  47.    success_mail()
复制代码
9#
发表于 2019-10-15 20:17:03 | 只看该作者
怎么弄,那个大佬发出来以下啊
8#
发表于 2019-10-15 20:14:03 | 只看该作者
fxzx 发表于 2019-10-15 20:09
那怎么把获取的这个状态码写到一个变量里呢?

我用nodejs写的,没用bash
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-12 17:31 , Processed in 0.065833 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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