问一个傻逼问题

搬瓦工机场JMS

撸甲骨文的API脚本如果撸到了,返回的状态码是多少?(没机器是500,请求过多是429)
贵州网友:我半分钟一次
江苏网友:不用判断,1分钟一次就请求吧
福建网友:oci-cli吗?200吧,我5秒一次请求,判断的就是stderr和stdout
辽宁网友:忘了,反正会返回一大堆信息。你拿个能随便开的区试试就知道了
广东网友:看看不说话
澳门网友:那怎么把获取的这个状态码写到一个变量里呢?
山西网友:我用nodejs写的,没用bash
云南网友:怎么弄,那个大佬发出来以下啊
河南网友:
用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()

复制代码

云南网友:
用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()

复制代码

山东网友:
嗯,没试过,
上面这个中间生成了一个.txt,cat 可以看下里面的输出内容也容易判断返回结果怎么样的
山东网友: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

复制代码

陕西网友:收藏了
吉林网友:$? 看 exit code 等于0就是成功 否则就是失败
安徽网友:感谢分享收藏了

未经允许不得转载:美国VPS_搬瓦工CN2 GIA VPS » 问一个傻逼问题

赞 (0) 打赏

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏