|
|
本帖最后由 liyafe1997 于 2019-6-14 15:08 编辑
半个月过去了,小鸡还没有放出来,由于手上的小鸡略多,每次手动查比较麻烦,就批量利用ipcheck的API跑吧,感觉ipcheck的误报也不是那么严重,将就着用吧,查询结果都会推送到telegram BOT上,shell写的烂,勿见怪!

准备工作:[img][/img]
1、telegram BOT TOKEN:去找botfather创建个bot吧https://t.me/BotFather
2、telegram chat_id:https://t.me/get_id_bot
3、脚本需要用到jq,脚本有设置检测是否安装,没怎么测试发行版,不过应该可以用
配置信息存放:
1、找个目录存放ip_list.csv和status.txt俩个文件
2、ip_list.csv格式
- region,ip
- 样例:
- HK,192.168.1.188
复制代码
定时运行-样例:
- 0 */3 * * * bash /root/tcp_check/check_tcp.sh
复制代码
代码,代码中文(高墙)转换为"g f w":
- #!/usr/bin/env bash
- tg_bot_api="xxxxxxx"
- tg_chat_id="xxxxxx"
- ip_list=/root/tcp_check/ip_list.csv
- tmp=/root/tcp_check/status.txt
- check_sys(){
- if [ -f /etc/redhat-release ]; then
- release="centos"
- elif cat /etc/issue | grep -Eqi "debian"; then
- release="debian"
- elif cat /etc/issue | grep -Eqi "ubuntu"; then
- release="ubuntu"
- elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
- release="centos"
- elif cat /proc/version | grep -Eqi "debian"; then
- release="debian"
- elif cat /proc/version | grep -Eqi "ubuntu"; then
- release="ubuntu"
- elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
- release="centos"
- else
- release=""
- fi
- }
- install_jq(){
- check_sys
- if [[ ${release} == "centos" ]]
- then
- check_jq=$(sudo yum list installed jq|grep "jq"|wc -l)
- if [[ ${check_jq} -eq 0 ]]
- then
- sudo yum install -y jq
- fi
- elif [[ ${release} == "ubuntu" || ${release} == "debian" ]]
- then
- check_jq=$(sudo apt list installed jq|grep "jq"|wc -l)
- if [[ ${check_jq} -eq 0 ]]
- then
- sudo apt install -y jq
- fi
- fi
- }
- check_tcp(){
- install_jq
- time=$(date "+%Y-%m-%d-%H:%M:%S")
- for (( i = 1; i <= $(cat ${ip_list}|wc -l); ++i )); do
- get_ip=$(cat ${ip_list}|sed -n ''${i}'p'|awk -F ',' '{print$2}')
- get_region=$(cat ${ip_list}|sed -n ''${i}'p'|awk -F ',' '{print$1}')
- tcp_status=$(curl -s --connect-timeout 5 --retry 2 --request -XGET --url 'https://ipcheck.need.sh/api_v2.php?ip='${get_ip}'' |jq .data.inside_高墙.tcp.alive)
- if [[ ${tcp_status} == "true" ]]; then
- echo "${get_region}:${get_ip},TCP未被阻断" >> ${tmp}
- elif [[ ${tcp_status} == "false" ]]; then
- echo "\`${get_region}:${get_ip},TCP被阻断\`" >> ${tmp}
- else
- exit
- fi
- done
- msg_templete="***检测时间-${time}***
- \`主机状态\`
- '$(cat ${tmp})'"
- #text=$(echo "${msg_templete}"|python -c 'import sys;import urllib;import urllib.parse; print (urllib.parse.quote_plus(sys.stdin.read()))')
- text=$(echo "${msg_templete}"|python -c 'import sys;import urllib; print (urllib.quote_plus(sys.stdin.read()))')
- curl -s --connect-timeout 5 --retry 2 --request GET --url 'https://api.telegram.org/bot'${tg_bot_api}'/sendMessage?chat_id='${tg_chat_id}'&text='${text}'&parse_mode=markdown'
- rm -rf ${tmp}
- }
- check_tcp
复制代码 |
|