我们有这个示例curl 命令,它检索数据作为输出。除此之外,该输出还包含另一个 URL,可以再次使用该 URL 来执行curl 命令以获取下一个数据。请帮助创建一个 bash 脚本,该脚本自动使用响应中的下一个 URL,在脚本中以编程方式从下一页返回事件。
例子:
curl -H '授权:承载' -XGET 'https://.loggly.com/apiv2/events/iterate?q=*&from=-10m&until=now&size=1'
输出样本:
{
"events": [
{
"raw": "{\"function\": \"handle\", \"publish_success\": 83, \"timestamp\": \"17-02-01 00:58:04,852149\", \"start_time\": 1485910682.351175, \"args\": \"\", \"duration\": 2501, \"message\": \"end publish run\", \"publish_fail\": 0, \"level\": \"INFO\", \"source_duration\": {\"duration\": 1, \"end_time\": 1485910682.351801}, \"feature_duration\": {\"duration\": 1427, \"end_time\": 1485910683.786751}, \"pathname\": \"/opt/loggly/web/app/alert/management/commands/alerterd.py\", \"lineno\": 329, \"cache_duration\": {\"duration\": 1, \"end_time\": 1485910684.821093}, \"action\": \"alerterd\", \"update_duration\": {\"duration\": 8, \"end_time\": 1485910684.852121}, \"end_time\": 1485910684}",
"logtypes": [
"json",
"syslog"
],
"timestamp": 1485910684852,
"unparsed": null,
"logmsg": "{\"function\": \"handle\", \"publish_success\": 83, \"timestamp\": \"17-02-01 00:58:04,852149\", \"start_time\": 1485910682.351175, \"args\": \"\", \"duration\": 2501, \"message\": \"end publish run\", \"publish_fail\": 0, \"level\": \"INFO\", \"source_duration\": {\"duration\": 1, \"end_time\": 1485910682.351801}, \"feature_duration\": {\"duration\": 1427, \"end_time\": 1485910683.786751}, \"pathname\": \"/opt/loggly/web/app/alert/management/commands/alerterd.py\", \"lineno\": 329, \"cache_duration\": {\"duration\": 1, \"end_time\": 1485910684.821093}, \"action\": \"alerterd\", \"update_duration\": {\"duration\": 8, \"end_time\": 1485910684.852121}, \"end_time\": 1485910684}",
"id": "7ce48bda-e819-11e6-808b-12a1c1f6d2c3",
"tags": [],
"event": {
"syslog": {
"severity": "Informational",
"appName": "msg",
"timestamp": "2017-02-01T00:58:04.852+00:00",
"facility": "local use 1",
"priority": "142",
"host": "127.0.0.1"
},
"json": {
"function": "handle",
"publish_success": 83,
"level": "INFO",
"timestamp": "17-02-01 00:58:04,852149",
"start_time": 1485910682.351175,
"cache_duration": {
"duration": 1,
"end_time": 1485910684.821093
},
"update_duration": {
"duration": 8,
"end_time": 1485910684.852121
},
"duration": 2501,
"pathname": "/opt/loggly/web/app/alert/management/commands/alerterd.py",
"end_time": 1485910684,
"source_duration": {
"duration": 1,
"end_time": 1485910682.351801
},
"action": "alerterd",
"message": "end publish run",
"publish_fail": 0,
"feature_duration": {
"duration": 1427,
"end_time": 1485910683.786751
},
"lineno": 329
}
}
}
],
"next": "https://<subdomain>.loggly.com/apiv2/events/iterate?next=eea25ee6-0e48-4428-a544-36d6441d132c"
}
所以我们希望再次运行另一个命令,如下所示:
curl -H '授权:承载' -XGET 'https://.loggly.com/apiv2/events/iterate?next=eea25ee6-0e48-4428-a544-36d6441d132c'
答案1
看看jq
命令。如果尚未安装,现在大多数存储库中都可以找到它。它用于从命令行解析 JSON。您可以在手册页或此处阅读更多信息:
例如,如果将curl命令的输出通过管道传输到jq,如下所示:
curl .... | jq '.next'
您将仅获得用引号引起来的 URL 值,如下所示:
"https://<subdomain>.loggly.com/apiv2/events/iterate?next=eea25ee6-0e48-4428-a544-36d6441d132c"
您可以测试该值的有效性或将其传递给另一个卷曲或您需要的任何内容。