我正在尝试使用xinetd和bash为webhook编写一个简单的处理程序。 我有这个configurationxinetd的小事情:
service github-hooks { port = 61000 socket_type = stream protocol = tcp wait = no user = ubuntu server = /home/ubuntu/github-hooks.sh }
和这个bash脚本:
#!/bin/bash echo -e "HTTP/1.1 200 OK"
现在,我想读取在webhook发送的数据,所以我可以做一些更有趣的事情,总是返回200。
我如何从我的bash脚本中读取发布数据?
我试过了:
while read line; do echo "$line" >> /home/ubuntu/test done < /dev/stdin
但这不适合我。
编辑
由于下面的build议,我停止了xinetd并使用nc来查看通过线路nc原始数据:
$nc -l 61000
得到这个:
POST / HTTP/1.1 Host: <snip>:61000 Accept: */* User-Agent: GitHub-Hookshot/375c44e X-GitHub-Event: pull_request X-GitHub-Delivery: 2dc1fb00-1c8e-11e6-9955-64afafb6ce32 content-type: application/json X-Hub-Signature: sha1=45afd85b7d4312fa8ac4c56638e8e9699e2ddb36 Content-Length: 20558 {"action":"opened","number":116,"pull_request": <snip>
所以,数据正在发送。 现在知道有11行正在发送,我读了11行:
for i in {0..10} do read line echo "$line" >> /home/ubuntu/test done
而且,我得到相同的输出(伟大的成功:)
POST / HTTP/1.1 Host: <snip>:61000 Accept: */* User-Agent: GitHub-Hookshot/375c44e X-GitHub-Event: pull_request X-GitHub-Delivery: 2dc1fb00-1c8e-11e6-9955-64afafb6ce32 content-type: application/json X-Hub-Signature: sha1=45afd85b7d4312fa8ac4c56638e8e9699e2ddb36 Content-Length: 20558 {"action":"opened","number":116,"pull_request": <snip>
也许我应该只读9行,然后使用Content-Length参数来读取其余的? 我仍然不明白发生了什么,所以任何信息都会非常有帮助。
好的,我从来没有弄清楚如何在有input的情况下阅读,但是我注意到头上有一个长度。 所以,我parsing出长度,然后读取这些字符数:
#!/bin/bash for i in {1..9} do read line done array=(${line}) length=${array[1]} read line read -n ${length:0:-1} line echo "$line" >> /home/ubuntu/test echo -e "HTTP/1.1 200 OK"
而且,现在我已经获得了github发送的发布数据。