我使用MaxMind的GeoLite。我的真正任务是通过特定的IP确定我们系统中的CityId。在MaxMind中,我通过IP找到locid,然后在locid和CityId之间使用hand-maded交叉表。但这个交叉表尚未完成。我从MaxMind找到了localization的好文件,其中每个locid都映射到GeoNames id。 GeoNames id对我有好处,但这个文件还没有完成。例如,此文件中没有伯明翰。 那么,有人有这样的问题吗?如果有任何决定方法吗?
答案 0 :(得分:1)
MaxMind的GeoLite2 databases返回GeoNames ID。
答案 1 :(得分:0)
这是一个三个部分,为您提供所有想要的输出。由你来包装,但提供所有城市信息。通过反向查找lon lat和iprange。
1个IP查找需要python GeoIP
示例。然后剥离IP范围。查看每个范围。然后标记其city / lon lat。这不是每个城市的每个IP。但它会为您提供主要的提供者,并且如果不能完全准确地估计他们所处的城市或旁边的城市,那么它将非常接近。
#!/usr/bin/python
import GeoIP
gi = GeoIP.open("/bin/script/tbl/state/GeoLiteCity.dat",GeoIP.GEOIP_STANDARD)
with open ("city.txt", "r") as myfile:
data=myfile.read().replace('\n', '')
gir = gi.record_by_addr(data)
if gir != None:
print gir['city']
print gir['region']
print gir['region_name']
print gir['latitude']
print gir['longitude']
#!/bin/bash
cd /bin/script/tbl/state
for state in $(cat state.abrv); do
state=$(echo $state)
cat outputfile | grep $state | cut -f1 -d"," > SB
output=sb.csv
echo "ip,country" > $output
for i in $( cat SB );
do echo "$i,\"$( geoiplookup -f GeoLiteCity.dat $i -i | cut -d' ' -f4-99 )\"" >> $output
done
echo "a" > sb1.csv && echo "a" >> sb1.csv && echo "a" >> sb1.csv
cat sb.csv >> sb1.csv
awk 'NR == 1 || NR % 7 == 0' sb1.csv | tr -d ' ' | tr '-' '/' > sb.csv
tail -n +2 "sb.csv" > $state
rm SB sb.csv sb1.csv
done
然后,您可以为iptables
生成一个快速加载的脚本。这会创建一个新链,因此您可以先删除一个catch。 Python会将这些加载到链的顶部。然后,您可以继续设置主链,而无需等待30分钟才能加载。 (当然,取决于您的计算机)
#IPT=/sbin/iptables
#ACT=/bin/script/tbl/state/active.txt
#Py=/usr/bin/python
#suba='p=subprocess.Popen(["'
#subb='"],?stdout?=?subprocess.PIPE)'
#sub2='output?,?err?=?p.communicate()'
#sub3='print?output'
#
#### Any Changes?
#if diff 'active.txt' 'active.old' > /dev/null; then
# echo 'Loading Group Interests'
# /bin/bash state.bash
# $Py state.py
# exit
#else
# echo 'Modifying Rules For'
# echo $(diff active.txt active.old | head -50 | tail -49 | cut -f2 -d'0')
#fi
#
### Then lets go
# echo '#!/bin/bash' > state.bash
# echo '#!/usr/bin/python' > state.py
# echo 'import?subprocess' >> state.py
#
#for state in $(cat $ACT)
#do
# echo $IPT' -N '$state >> state.bash
# echo $IPT' -A '$state' -j DROP' >> state.bash
#done
#/bin/bash state.bash
#
#for state in $(cat $ACT)
#do
# BADIPS=$(egrep -v -E "^#|^$" $state | sed 's/[A-Za-z]*//g' | tr -d ":")
# for ip in $BADIPS
# do
# echo $ip | cut -f1 -d'/' > city.txt
# $Py city.py | head -4 | tail -1 > lon.ip
# $Py city.py | head -5 | tail -1 > lat.ip
# city=$(cat city.ip | tr ' ' '_')
# lon=$(cat lon.ip)
# lat=$(cat lat.ip)
# echo $suba'iptables -A INPUT -s '$ip' -j '$state$subb >> state.py
# echo $sub2 >> state.py
# echo $sub3 >> state.py
# echo $suba'iptables -A FORWARD -s '$ip' -j '$state$subb >> state.py
# echo $sub2 >> state.py
# echo $sub3 >> state.py
# echo $suba'iptables -A OUTPUT -d '$ip' -j '$state$subb >> state.py
# echo $sub2 >> state.py
# echo $sub3 >> state.py
# echo $suba"iptables -A "$state" -s "$ip" -j LOG --log-prefix \
#'STATE_,"$ip","$state","$city","$lon":"$lat",_OUT_:'"$subb >> state.py
# echo $sub2 >> state.py
# echo $sub3 >> state.py
# echo $suba"iptables -A "$state" -d "$ip" -j LOG --log-prefix \
#'STATE_,"$ip","$state","$city","$lon":"$lat",_IN_:'"$subb >> state.py
# echo $sub2 >> state.py
# echo $sub3 >> state.py
# echo $suba"iptables -A "$state" -s "$ip" -j LOG --log-prefix \
'STATE_,"$ip","$state","$city","$lon":"$lat",_OUT_:'"$subb >> state.py
# echo $sub2 >> state.py
# echo $sub3 >> state.py
# echo $suba"iptables -A "$state" -d "$ip" -j LOG --log-prefix #'STATE_,"$ip","$state","$city","$lon":"$lat",_IN_:'"$subb >> state.py
# echo $sub2 >> state.py
# echo $sub3 >> state.py
# done
#done
### MAGIC
sed 's/ /"','"/g' state.py > state1.py
cat state1.py | tr '?' ' ' > state.py
sleep 3
python state.py
cat active.txt > active.old
### adapting this to ASN or country is way easier.
### your welcome
### last time I post anything on this webpage. There is NOTHING wrong with my formatting. go cry about it
答案 2 :(得分:0)
在找到以下内容之前,我无法在maxmind数据库geolite2中为国家/地区匹配geoname_id和registered_country_geoname_id:
https://www.kaggle.com/geonames/geonames-database?select=geonames.csv