钉钉告警脚本-shell类型-只需要用终极大招就行,需要手机号码用英文状态下逗号分隔

该文提供了一个Bash脚本示例,用于向钉钉机器人发送消息并@多个指定的手机号码。脚本通过接收命令行参数,组装JSON数据并利用curl发送POST请求到钉钉webhook接口来实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文本类型,只可以@一个人的

新建一个send.sh并且赋予可执行权限

#!/bin/bash

# 钉钉机器人 webhook 地址
webhook_url="https://oapi.dingtalk.com/robot/send?access_token=自己的token"

# 消息内容
subject="$1"
message="$2"
at_mobiles="$3"

# 组装 JSON 数据
json="{\"msgtype\":\"text\",\"text\":{\"content\":\"${subject}\n${message}\"},\"at\":{\"atMobiles\":[\"${at_mobiles}\"],\"isAtAll\":false}}"

# 发送 POST 请求
curl -s -H "Content-Type: application/json" -X POST --data "${json}" "${webhook_url}" >/dev/null 2>&1

如何运行:
./send.sh 123 456 手机号

文本类型,可以@多个人的

新建一个send.sh并且赋予可执行权限

#!/bin/bash

# 钉钉机器人 webhook 地址
webhook_url="https://oapi.dingtalk.com/robot/send?access_token=自己的token"

# 消息内容
subject="$1"
message="$2"
at_mobiles=( "$3" "$4" "$5" ) # 将需要@的手机号码放入一个数组中

# 组装 JSON 数据
at_list=""
for mobile in "${at_mobiles[@]}"
do
  at_list+="\"$mobile\","
done
at_list="${at_list%,}" # 去除最后一个逗号

json="{\"msgtype\":\"text\",\"text\":{\"content\":\"${subject}\n${message}\"},\"at\":{\"atMobiles\":[${at_list}],\"isAtAll\":false}}"

# 发送 POST 请求
curl -s -H "Content-Type: application/json" -X POST --data "${json}" "${webhook_url}" >/dev/null 2>&1

如何运行:
./send.sh 123 456 手机号1 手机号2

当然也可以@100个人

#!/bin/bash

# 钉钉机器人 webhook 地址
webhook_url="https://oapi.dingtalk.com/robot/send?access_token=自己的token"

# 消息内容
subject="$1"
message="$2"
at_mobiles=( "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}" "${11}" "${12}" "${13}" "${14}" "${15}" "${16}" "${17}" "${18}" "${19}" "${20}" "${21}" "${22}" "${23}" "${24}" "${25}" "${26}" "${27}" "${28}" "${29}" "${30}" "${31}" "${32}" "${33}" "${34}" "${35}" "${36}" "${37}" "${38}" "${39}" "${40}" "${41}" "${42}" "${43}" "${44}" "${45}" "${46}" "${47}" "${48}" "${49}" "${50}" "${51}" "${52}" "${53}" "${54}" "${55}" "${56}" "${57}" "${58}" "${59}" "${60}" "${61}" "${62}" "${63}" "${64}" "${65}" "${66}" "${67}" "${68}" "${69}" "${70}" "${71}" "${72}" "${73}" "${74}" "${75}" "${76}" "${77}" "${78}" "${79}" "${80}" "${81}" "${82}" "${83}" "${84}" "${85}" "${86}" "${87}" "${88}" "${89}" "${90}" "${91}" "${92}" "${93}" "${94}" "${95}" "${96}" "${97}" "${98}" "${99}" "${100}" )

# 组装 JSON 数据
at_list=""
for mobile in "${at_mobiles[@]}"
do
  at_list+="\"$mobile\","
done
at_list="${at_list%,}" # 去除最后一个逗号

json="{\"msgtype\":\"text\",\"text\":{\"content\":\"${subject}\n${message}\"},\"at\":{\"atMobiles\":[${at_list}],\"isAtAll\":false}}"

# 发送 POST 请求
curl -s -H "Content-Type: application/json" -X POST --data "${json}" "${webhook_url}" >/dev/null 2>&1

**

终极大招,可以@1个人,也可以@无数个人

**
手机号码用英文状态下的,隔开,并传参数

#!/bin/bash

# 钉钉机器人 webhook 地址
webhook_url="https://oapi.dingtalk.com/robot/send?access_token=自己的token"

# 消息内容
subject="$1"
message="$2"
at_mobiles="$3" # 将包含手机号码的字符串赋值给变量

# 将分隔符设置为逗号
IFS=','

# 将 $at_mobiles 字符串分解为数组
read -ra mobiles_array <<< "$at_mobiles"

# 组装 JSON 数据
at_list=""
for mobile in "${mobiles_array[@]}"
do
  at_list+="\"$mobile\","
done
at_list="${at_list%,}" # 去除最后一个逗号

json="{\"msgtype\":\"text\",\"text\":{\"content\":\"${subject}\n${message}\"},\"at\":{\"atMobiles\":[${at_list}],\"isAtAll\":false}}"

# 发送 POST 请求
curl -s -H "Content-Type: application/json" -X POST --data "${json}" "${webhook_url}" >/dev/null 2>&1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ascarl2010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值