ehang-io / nps

一款轻量级、高性能、功能强大的内网穿透代理服务器。支持tcp、udp、socks5、http等几乎所有流量转发,可用来访问内网网站、本地支付接口调试、ssh访问、远程桌面,内网dns解析、内网socks5代理等等……,并带有功能强大的web管理端。a lightweight, high-performance, powerful intranet penetration proxy server, with a powerful web management terminal.
https://ehang.io/nps/documents
GNU General Public License v3.0
29.75k stars 5.37k forks source link

写了个shell脚本定时扫描docker日志并推送 #1184

Open GreenHatHG opened 1 year ago

GreenHatHG commented 1 year ago

每隔10s扫描docker日志,有关键字则提出ip查询归属地,然后推送到ios的bark

shell

/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"

timer

/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