WebHook接口传入参数并交给shell执行
参考文档1:https://blog.csdn.net/lswzw/article/details/106917430
参考文档2:https://stackoverflow.com/questions/67300515/rundeck-webhook-to-pass-an-argument-to-a-shell-script
WebHook部署按此执行即可:https://teddyou.com/?id=144
新建一个json文件,作为WebHook启动配置文件,以下为实例:
[
{
"id": "4444", // 这里填写 http://server/hooks/{ID} 对应的是{ID} 值。也是唯一值。
"execute-command": "./test4.sh", // 须要执行的shell文件。
"command-working-directory": "./", // 执行的工作目录。
"pass-arguments-to-command": // 指定获取 post请求参数
[
{
"source": "payload", // 这里是固定,获取请求信息
"name": "text.content" // 这里text.content 是获取 json里面 text:content 里面对应的值。 在测试里面能更直观查看。
}
],
"trigger-rule": // 指定数据格式才执行。
{
"match":
{
"type": "value",
"value": "lswzw", // 这里做的是匹配固定值。
"parameter":
{
"source": "payload", // 获取请求信息
"name": "msgtype" // 获取请求里 json msgtype:对应的参数
}
}
}
},
// 下面是创建一个2222 这个接口, 请求后执行不同的指令。
{
"id": "2222",
"execute-command": "./test2.sh",
"command-working-directory": "./",
"pass-arguments-to-command":
[
{
"source": "payload",
"name": "text.content"
}
],
"trigger-rule":
{
"match":
{
"type": "value",
"value": "lswzw",
"parameter":
{
"source": "payload",
"name": "msgtype"
}
}
}
}
]启动命令:
./webhook -port 9000 -hotreload -hooks webhook.json -verbose
是否启动成功留意loaded行,是否出现ID值。
[root@cd3 webhook]# ./webhook -port 9000 -hotreload -hooks ./hooks.json -verbose
[webhook] 2023/06/20 19:19:01 version 2.8.1 starting
[webhook] 2023/06/20 19:19:01 setting up os signal watcher
[webhook] 2023/06/20 19:19:01 attempting to load hooks from ./hooks.json
[webhook] 2023/06/20 19:19:01 os signal watcher ready
[webhook] 2023/06/20 19:19:01 found 2 hook(s) in file
[webhook] 2023/06/20 19:19:01 loaded: test1
[webhook] 2023/06/20 19:19:01 loaded: 2222
[webhook] 2023/06/20 19:19:01 setting up file watcher for ./hooks.json
[webhook] 2023/06/20 19:19:01 serving hooks on http://0.0.0.0:9000/hooks/{id}此日志说明启动成功,如果启动失败,则需要继续修改配置文件。
参数的使用:
#!/bin/bash echo $1 >> test
参数进入类似于
xxx.sh x1 x2
以上格式,如果有第二个参数使用$2即可。
如果要传入多个参数,可用以下方法:
{
"msgtype": "val",
"text":{
"content":"{{player.name}}%%{{player.steam.timePlayed}}%%{{player.org.timePlayed}}"
}
}#!/bin/bash
C1=`echo $1|awk -F '%%' '{print $1}'`
C2=`echo $1|awk -F '%%' '{print $2}'`
C3=`echo $1|awk -F '%%' '{print $3}'`
H3=`echo "scale=2;${C3}/3600"|bc`
H2=`echo "scale=2;${C2}/60"|bc`以特定分隔符隔开。
发表评论