-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_isp.sh
More file actions
executable file
·102 lines (86 loc) · 2.26 KB
/
check_isp.sh
File metadata and controls
executable file
·102 lines (86 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
#
# Author: Payne Zheng <zzuai520@live.com>
# Date: 2017-01-12 09:27:39
# Location: Shenzhen
# Desc: check any in dns server ip is isp
#
# Help function
Help() {
echo "Usega : $(basename $0) ip_list interval"
echo "example : bash $(basename $0) /root/sh.txt 2"
}
# log function
log() {
logger -t "CHECK_ISP" -p local0.info "$*"
}
# set variable
ip_list=$1
interval=$2
outdir=/data/check_isp/$(date '+%Y%m%d%H%M')
test ! -d $outdir && mkdir -p $outdir
outfile=$outdir/ALL_check_isp.log
cn_outfile=$outdir/CN_check_isp.log
failed_file=$outdir/failed_ip.list
python_script=/root/conv.py
# To write python3 script
cat > $python_script <<EOF
#!/usr/bin/env python3
import sys
import os
import json
from subprocess import Popen, PIPE
def fetch(ip):
url = 'http://ip.taobao.com/service/getIpInfo.php'
url = '%s?ip=%s' % (url, ip)
pipe = Popen(['curl', url], stdout=PIPE, stderr=PIPE)
stdout, stderr = pipe.communicate()
res = json.loads(stdout.decode('utf8'))
if res['code'] != 0:
exit(1)
else:
data = res['data']
country = data['country']
country_id = data['country_id']
region = data['region']
city = data['city']
isp = data['isp']
ip = data['ip']
print(country_id, ip, country, region, city, isp)
exit(0)
if __name__ == '__main__':
if len(sys.argv) != 2:
bname = os.path.basename(sys.argv[0])
print("usage: %s TEXT" % bname, file=sys.stderr)
exit(1)
ip = sys.argv[1]
fetch(ip)
EOF
# check args
test $# -ne 2 && { Help;exit; }
# log mark start time
log "check start.."
# loop read checkip action to python3 script
while read ip
do
let i++
echo "NO:$i"
python3 $python_script $ip >> $outfile
if test $? -ne 0; then
sleep 1;
python3 $python_script $ip >> $outfile
test $? -ne 0 && echo "$ip" >> $failed_file
fi
sleep $interval
done <$ip_list
# log mark end time
log "check done.."
# filter country_id is "CN"
grep -wE "^CN" "$outfile" > "$cn_outfile"
# end echo information
echo "check done..."
echo "All test results file : $outfile"
echo "CN test results file : $cn_outfile"
echo "check failed file : $failed_file"
# delete python3 script tempfile
rm -f $python_script