分析攻擊IP來源地並畫出餅圖

大囚長發表於2018-05-30

此文中的API將臺灣列為國家,非本人立場,臺灣屬於中國,臺灣島生活的人不一定!
上碼:

#!/usr/bin/python
#coding=utf-8

'''
http://ip-api.com/json/ip
'''


import plotly  
import plotly.plotly  
import plotly.graph_objs as abcc  
import plotly.plotly  

class Piecharts:  
    def __init__(self):  
        print "餅圖生成中"  

    def makePiecharts(self,labels,values,filename):  
        trace = abcc.Pie(labels = labels,values= values)  
        plotly.offline.plot([trace],filename=filename) 


import requests
import sys

try:
    iplist = sys.argv[1]
except:
    print "IP list not given or some other error!"
countrylist = {}
regionlist = {}
citylist = {}

with open(iplist) as f:
    for ip in f.readlines():
        if ip.strip() != '':
            url = 'http://ip-api.com/json/' + ip.strip()
            try:
                result = requests.get(url)
                jsontext = result.json()
            except:
                print "Error: Data not retrieved!"
                continue
            status = jsontext['status']
            if status == 'fail':
                print "%s failed!" % ip.strip()
                continue
            mline = jsontext['as']
            city = jsontext['city']
            country = jsontext['country']
            countryCode = jsontext['countryCode']
            isp = jsontext['isp']
            lat = jsontext['lat']
            lon = jsontext['lon']
            org = jsontext['org']
            query = jsontext['query']
            region = jsontext['region']
            regionName = jsontext['regionName']
            timezone = jsontext['timezone']
            zipcode = jsontext['zip']

            if not country in countrylist:
                countrylist[country] = 0
            else:
                countrylist[country] += 1

            if not regionName in regionlist:
                regionlist[regionName] = 0
            else:
                regionlist[regionName] += 1

            if not city in citylist:
                citylist[city] = 0
            else:
                citylist[city] += 1
            try:
                print ip.strip() + '--' + country + '--' + regionName
            except:
                print "Special character!"
    print countrylist

    #country
    labels = [i for i in countrylist]  
    value = [countrylist[i] for i in countrylist]  
    drive = Piecharts()  
    drive.makePiecharts(labels,value,"country.html")  

    #region
    labels = [i for i in regionlist]  
    value = [regionlist[i] for i in regionlist]  
    drive = Piecharts()  
    drive.makePiecharts(labels,value,"region.html") 

    #city
    labels = [i for i in citylist]  
    value = [citylist[i] for i in citylist]  
    drive = Piecharts()  
    drive.makePiecharts(labels,value,"city.html") 

gevent協程併發版

#!/usr/bin/python
# coding=utf-8

'''
http://ip-api.com/json/ip
'''


import plotly
import plotly.graph_objs as abcc
import plotly.plotly


class Piecharts:
    def __init__(self):
        print u'餅圖生成中'

    def makePiecharts(self, labels, values, filename):
        trace = abcc.Pie(labels=labels, values=values)
        plotly.offline.plot([trace], filename=filename)


import requests
import sys

try:
    iplist = sys.argv[1]
except:
    print "IP list not given or some other error!"
countrylist = {}
regionlist = {}
citylist = {}


def locater(url):
    try:
        result = requests.get(url)
        jsontext = result.json()
    except:
        print "Error: Data not retrieved!"
        return
    status = jsontext['status']
    if status == 'fail':
        print "%s failed!" % ip.strip()
        return
    mline = jsontext['as']
    city = jsontext['city']
    country = jsontext['country']
    countryCode = jsontext['countryCode']
    isp = jsontext['isp']
    lat = jsontext['lat']
    lon = jsontext['lon']
    org = jsontext['org']
    query = jsontext['query']
    region = jsontext['region']
    regionName = jsontext['regionName']
    timezone = jsontext['timezone']
    zipcode = jsontext['zip']

    if not country in countrylist:
        countrylist[country] = 0
    else:
        countrylist[country] += 1

    if not regionName in regionlist:
        regionlist[regionName] = 0
    else:
        regionlist[regionName] += 1

    if not city in citylist:
        citylist[city] = 0
    else:
        citylist[city] += 1
    try:
        print ip.strip() + '--' + country + '--' + regionName
    except:
        print "Special character!"


from gevent import monkey
monkey.patch_socket()
from gevent import pool
import gevent

pool = pool.Pool(40)
glist = []
with open(iplist) as f:
    for ip in f.readlines():
        if ip.strip() != '':
            url = 'http://ip-api.com/json/' + ip.strip()
            glist.append(pool.spawn(locater, url))
    gevent.joinall(glist)

    # country
    labels = [i for i in countrylist]
    value = [countrylist[i] for i in countrylist]
    drive = Piecharts()
    drive.makePiecharts(labels, value, "country.html")

    # region
    labels = [i for i in regionlist]
    value = [regionlist[i] for i in regionlist]
    drive = Piecharts()
    drive.makePiecharts(labels, value, "region.html")

    # city
    labels = [i for i in citylist]
    value = [citylist[i] for i in citylist]
    drive = Piecharts()
    drive.makePiecharts(labels, value, "city.html")

餅圖效果:
這裡寫圖片描述

相關文章