撸甲骨文的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
)
- # -*- coding: UTF-8 -*-
- import json
- import io
- import smtplib
- from email.mime.text import MIMEText
- def success_mail():
- mail_host = ‘smtp.qq.com’ #发送发服务器地址
- mail_user = ‘[email protected]’#发送方邮箱
- mail_pass = ‘###’ #发送方密码
- sender = ‘[email protected]’
- receivers = [‘[email protected]’]
- message = MIMEText(‘successfully open the machine’,’plain’,’utf-8′)#发送的内容
- message[‘Subject’] = ‘congratulations!’
- message[‘From’] = sender
- message[‘To’] = receivers[0]
- try:
- smtpObj = smtplib.SMTP_SSL("smtp.qq.com",465)
- smtpObj.login(mail_user,mail_pass)
- smtpObj.sendmail(
- sender,receivers,message.as_string())
- smtpObj.quit()
- print(‘success’)
- except smtplib.SMTPException as e:
- print(‘error’,e)
- def fail_mail():
- mail_host = ‘smtp.qq.com’#发送发服务器地址
- mail_user = ‘[email protected]’#发送方邮箱
- mail_pass = ‘###’ #发送方密码
- sender = ‘[email protected]’
- receivers = [‘[email protected]’]#接收方
- message = MIMEText(‘fail to open the machine’,’plain’,’utf-8′) #发送的内容
- message[‘Subject’] = ‘fail!’
- message[‘From’] = sender
- message[‘To’] = receivers[0]
- try:
- smtpObj = smtplib.SMTP_SSL("smtp.qq.com",465)
- smtpObj.login(mail_user,mail_pass)
- smtpObj.sendmail(
- sender,receivers,message.as_string())
- smtpObj.quit()
- print(‘success’)
- except smtplib.SMTPException as e:
- print(‘error’,e)
- f = open(‘/var/tmp/test.txt’,’r’)
- lines=f.readlines()
- if ‘Error’ not in lines[2]:
- 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
)
- # -*- coding: UTF-8 -*-
- import json
- import io
- import smtplib
- from email.mime.text import MIMEText
- def success_mail():
- mail_host = ‘smtp.qq.com’ #发送发服务器地址
- mail_user = ‘[email protected]’#发送方邮箱
- mail_pass = ‘###’ #发送方密码
- sender = ‘[email protected]’
- receivers = [‘[email protected]’]
- message = MIMEText(‘successfully open the machine’,’plain’,’utf-8′)#发送的内容
- message[‘Subject’] = ‘congratulations!’
- message[‘From’] = sender
- message[‘To’] = receivers[0]
- try:
- smtpObj = smtplib.SMTP_SSL("smtp.qq.com",465)
- smtpObj.login(mail_user,mail_pass)
- smtpObj.sendmail(
- sender,receivers,message.as_string())
- smtpObj.quit()
- print(‘success’)
- except smtplib.SMTPException as e:
- print(‘error’,e)
- def fail_mail():
- mail_host = ‘smtp.qq.com’#发送发服务器地址
- mail_user = ‘[email protected]’#发送方邮箱
- mail_pass = ‘###’ #发送方密码
- sender = ‘[email protected]’
- receivers = [‘[email protected]’]#接收方
- message = MIMEText(‘fail to open the machine’,’plain’,’utf-8′) #发送的内容
- message[‘Subject’] = ‘fail!’
- message[‘From’] = sender
- message[‘To’] = receivers[0]
- try:
- smtpObj = smtplib.SMTP_SSL("smtp.qq.com",465)
- smtpObj.login(mail_user,mail_pass)
- smtpObj.sendmail(
- sender,receivers,message.as_string())
- smtpObj.quit()
- print(‘success’)
- except smtplib.SMTPException as e:
- print(‘error’,e)
- f = open(‘/var/tmp/test.txt’,’r’)
- lines=f.readlines()
- if ‘Error’ not in lines[2]:
- success_mail()
复制代码
山东网友:
嗯,没试过,
上面这个中间生成了一个.txt,cat 可以看下里面的输出内容也容易判断返回结果怎么样的
山东网友:nodejs版的,需要配置好cli,开好2个机器就会自动停止运行,之前用这个开到两个韩国机器
- const domainID = "YOUR_DOMAIN_ID"; // "xUCN:AP-SEOUL-1-AD-1"
- const imageID = "YOUR_IMAGE_ID"; // "ocid1.image.oc1.ap-seoul-1.xxxxxxxxxxxxxx"
- const subnetID = "YOUR_SUBNET_ID"; // "ocid1.subnet.oc1.ap-seoul-1.xxxxxxxxxxxxxx"
- const shape = "YOUR_SHAPE_ID"; // "VM.Standard.E2.1.Micro"
- const sshKey = "YOUR_SSH_KEY"; // "ssh-rsa xxxxxxxxxxxxxx"
- const compartmentID = "YOUR_COMPARTMENT_ID"; // "ocid1.tenancy.oc1..xxxxxxxxxxxxxx"
- const name1 = "free-kr1";
- const name2 = "free-kr2";
- const sleepTime = 5000; // ms
- const util = require("util");
- const exec = util.promisify(require("child_process").exec);
- const fs = require("fs");
- const readFile = fs.existsSync("./oracle-cloud.json") ? fs.readFileSync("./oracle-cloud.json") : `{"loopTime1":0,"loopTime2":0}`;
- const save = JSON.parse(readFile.toString());
- let loopTime1 = save.loopTime1;
- let loopTime2 = save.loopTime2;
- let isMachine1 = true;
- let machine1Finished = false;
- let machine2Finished = false;
- async function execute(callback) {
- return new Promise(async resolve => {
- try {
- if (isMachine1) {
- if (machine1Finished) {
- resolve(false);
- return;
- }
- loopTime1++;
- } else {
- if (machine2Finished) {
- resolve(false);
- return;
- }
- loopTime2++;
- }
- const cmd = `oci compute instance launch –availability-domain ${domainID} –display-name ${
- isMachine1 ? name1 : name2
- } –image-id ${imageID} –subnet-id ${subnetID} –shape ${shape} –assign-public-ip true –metadata ‘{"ssh_authorized_keys": "${sshKey}"}’ –compartment-id ${compartmentID}`
- const result = await exec(cmd);
- callback(result, resolve);
- } catch (err) {
- callback(err, resolve);
- }
- });
- }
- function sleep(ms) {
- return new Promise(resolve => {
- setTimeout(resolve, ms);
- });
- }
- function callback(result, resolve) {
- console.log(
- `${name1}: ${loopTime1} ${
- machine1Finished ? "Finished!" : "Creating"
- } ${name2}: ${loopTime2} ${machine2Finished ? "Finished!" : "Creating"}`
- );
- if (result.stderr) {
- console.log(`Err: ${isMachine1 ? name1 : name2}`);
- const jsonGroup = result.stderr
- .toString()
- .match(/\{(?:[^{}]|(\{(?:[^{}]|(\{[^{}]*\}))*\}))*\}/);
- try {
- const json = JSON.parse(jsonGroup[0]);
- console.log(json.status, json.message);
- } catch (e) {
- }
- resolve(true);
- } else if (result.stdout) {
- if (isMachine1) {
- machine1Finished = true;
- } else {
- machine2Finished = true;
- }
- console.log("Out");
- resolve(true);
- }
- }
- async function init() {
- console.log("Started");
- while (!machine1Finished || !machine2Finished) {
- const isExecuted = await execute(callback);
- isMachine1 = !isMachine1;
- if (isExecuted) {
- fs.writeFileSync(
- "./oracle-cloud.json",
- JSON.stringify({loopTime1: loopTime1, loopTime2: loopTime2})
- );
- await sleep(sleepTime);
- }
- }
- }
- init();
- // By yyuueexxiinngg
- // Link https://github.com/yyuueexxiinngg/some-scripts/blob/master/oracle/oracle-cloud.js
复制代码
陕西网友:收藏了
吉林网友:$? 看 exit code 等于0就是成功 否则就是失败
安徽网友:感谢分享收藏了
未经允许不得转载:美国VPS_搬瓦工CN2 GIA VPS » 问一个傻逼问题