Skip to content

Instantly share code, notes, and snippets.

@blacktwin
Last active December 11, 2016 16:49
Show Gist options
  • Save blacktwin/1c809d858c4d128080d3e453645395f0 to your computer and use it in GitHub Desktop.
Save blacktwin/1c809d858c4d128080d3e453645395f0 to your computer and use it in GitHub Desktop.
Travel from random Class C IPs
import pygeoip
import matplotlib.pyplot as plt
import matplotlib as mpl
import random
import numpy as np
from mpl_toolkits.basemap import Basemap
from collections import OrderedDict
from geopy.geocoders import Nominatim
geolocator = Nominatim()
#Edit here
#how many random Class C IP addresses you want to run?
c = 100
#
#Map settings
plt.figure(figsize=(16, 9), dpi=100, frameon=False, tight_layout=True)
m = Basemap(projection='robin', lat_0=0, lon_0=-100, resolution='l', area_thresh=100000.0)
mpl.rcParams['legend.handlelength'] = 0
m.drawmapboundary(fill_color='#1F1F1F')
m.drawcoastlines()
m.drawstates()
m.drawcountries()
m.fillcontinents(color='#3C3C3C', lake_color='#1F1F1F')
m.drawmeridians(np.arange(0, 360, 30))
m.drawparallels(np.arange(-90, 90, 30))
title_string = 'Random IP Connections'
legend_title = 'Legend'
cord_lst = []
gclines = []
label_lst = []
while c > 0:
##Class C range - 128.0.0.0 191.255.255.255
oct1_r = random.randint(128, 191)
oct_r = random.randint(0, 255)
IP = str(oct1_r) + '.' + str(oct_r) + '.' + str(oct_r) + '.' + str(oct_r)
try:
gi = pygeoip.GeoIP('GeoLiteCity.dat')
gir = gi.record_by_addr(IP)
if not isinstance(gir['city'], unicode):
pass
else:
cord_lst += [[str(gir['latitude'])] + [str(gir['longitude'])] + [str(gir['city'])] + [IP]]
c -= 1
except Exception as e:
pass
#print(cord_lst)
for (lat, lon, city, ip) in cord_lst:
x, y = m(lon, lat)
labels = city + ' @ ' + ip
label_lst += [labels]
m.plot(x, y, marker='.', color='red', markersize=50, alpha=0.09)
gclines += [lon] + [lat]
for i, j, k, l in zip(gclines[0::2], gclines[1::2], gclines[2::2], gclines[3::2]):
m.drawgreatcircle(float(i), float(j), float(k), float(l), linewidth=1, alpha=0.5,
color='#AC7420')
for i, j, k in zip(label_lst[0::2], label_lst[1::2], label_lst[2::2]):
label_path = [i, j, j, k]
for t, n in zip(label_path[0::2], label_path[1::2]):
f_label = t + ' --> ' + n
z, q = m('0', '0')
m.plot(z, q, marker='.', color='red', label=f_label)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = OrderedDict(zip(labels, handles))
leg = plt.legend(by_label.values(), by_label.keys(), fancybox=True, fontsize='x-small', numpoints=1,
title=legend_title)
if leg:
lleng = len(leg.legendHandles)
for i in range(0, lleng):
leg.legendHandles[i]._legmarker.set_markersize(5)
leg.legendHandles[i]._legmarker.set_alpha(1)
leg.get_title().set_color('#7B777C')
leg.draggable()
leg.get_frame().set_facecolor('#2C2C2C')
for text in leg.get_texts():
plt.setp(text, color='#A5A5A7')
plt.tight_layout()
plt.title(title_string)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment