Open GreenHatHG opened 1 year ago
每隔10s扫描docker日志,有关键字则提出ip查询归属地,然后推送到ios的bark
/root/nps/check_nps_log.sh
#!/bin/bash function get_location() { local ip=$1 local query_ip_url="https://api.vore.top/api/IPdata?ip=$ip" local location=$(curl -s -X GET -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.105 Safari/537.36' --compressed "$query_ip_url") if echo "$location" | grep -q "SUCCESS"; then echo "$location" return fi query_ip_url="http://opendata.baidu.com/api.php?query=$ip&co=&resource_id=6006&oe=utf8" location=$(curl -s -X GET -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.105 Safari/537.36' --compressed "$query_ip_url") if echo "$location" | grep -q "location"; then echo "$location" fi } log=$(docker logs nps --since 10s | tail -n +1 | grep -e 'get connection' -e 'connection,') echo $log ips=$(echo "$log" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b") ips=($(echo "${ips[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) if [ -z "$ips" ]; then exit 1 fi content="👋nps activity \n $log \n \n" for ip in $ips do content+="🛠ip info\n $(get_location "$ip")\n\n" done content=$(echo -e "$content" | jq -Rs '.') payload="{\"title\": \"nps_activity\", \"body\": $content, \"device_key\": \"bark device key\", \"icon\": \"https://thumbs.dreamstime.com/z/basic-rgb-147910302.jpg\", \"group\": \"nps\"}" curl -s -X POST -H "Content-Type: application/json" -d "$payload" "https://api.day.app/push"
/etc/systemd/system/check_nps_log.service
[Unit] Description=check nps log [Service] ExecStart=bash /root/nps/check_nps_log.sh
/etc/systemd/system/check_nps_log.timer
[Unit] Description=check nps log [Timer] OnBootSec=10 OnUnitActiveSec=10 [Install] WantedBy=timers.target
systemctl enable --now check_nps_log.timer
每隔10s扫描docker日志,有关键字则提出ip查询归属地,然后推送到ios的bark
shell
/root/nps/check_nps_log.sh
timer
/etc/systemd/system/check_nps_log.service
/etc/systemd/system/check_nps_log.timer