相关文章推荐
刀枪不入的领结  ·  Qt ...·  3 月前    · 
豁达的地瓜  ·  linux ...·  3 月前    · 
豁达的帽子  ·  Custom Forms with No ...·  9 月前    · 
精明的核桃  ·  Log system | GitLab·  1 年前    · 
"msg_type": "text", "content": { "text": "{0} {1}".format(dt.now().strftime('%Y-%m-%d %H:%M:%S'), _msg) r = requests.post(_url, data=json.dumps(data), headers=headers) return r.text if __name__ == '__main__': # 注释掉的是不必要的调试用的 # args_list = sys.argv # if args_list.__len__() < 2: # print("加参数 --msg 后面跟要发送的消息") # exit(1) # arg1 = sys.argv[1] # if arg1 == '-h' or arg1 == '--help': # print("加参数 --msg 后面跟要发送的消息") # exit(0) parser = argparse.ArgumentParser(prog='feishu_alert',description='飞书消息通知消息') parser.add_argument('--msg', type=str, default=None, required=True, help="要发送的消息体") args = parser.parse_args() msg = args.msg url = 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxx' print(send_msg(url, msg))

shell 脚本

$ cat feishu_alert.sh
#!/bin/bash
para=`echo ${@:2}`
python3 /usr/local/bin/feishu_alert.py $1 "$para"

echo ${@:2} 的作用是把 shell 脚本的参数,从第二个开始到最后一个参数,当作一个参数

否则shell脚本传入的参数只要有空格,即使加了引号,传入python 脚本后也会被切分成多个参数

  • 当不传任何参数时
$ sh feishu_alert.sh
usage: feishu_alert [-h] --msg MSG
feishu_alert: error: the following arguments are required: --msg
  • help 帮助信息
$ sh feishu_alert.sh -h
usage: feishu_alert [-h] --msg MSG
飞书消息通知消息
optional arguments:
  -h, --help  show this help message and exit
  --msg MSG   要发送的消息体
  • 发送报警信息
$ sh feishu_alert.sh --msg "消息测试 this is a test"
{"Extra":null,"StatusCode":0,"StatusMessage":"success"}
  • 消息样式截图
    python 飞书机器人 web hook 报警集成到Linux shell 脚本_大数据综合