-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGetIPinfo.py
89 lines (69 loc) · 1.74 KB
/
GetIPinfo.py
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
#!/usr/bin/python
#https://github.com/maxmind/GeoIP2-python
import datetime
import psutil
from influxdb import InfluxDBClient
import geoip2.database
import socket
import sys
print str(sys.argv[1])
print str(sys.argv[1])
print(socket.gethostname())
reader = geoip2.database.Reader('/home/titan/Scripts/Grafana/GeoLite2-City.mmdb')
response = reader.city(str(sys.argv[1]))
Lat = response.location.latitude
ISO = response.country.iso_code
Long = response.location.longitude
State = response.subdivisions.most_specific.name
City = response.city.name
Country = response.country.name
Zip = response.postal.code
IP = str(sys.argv[1])
print (Country)
print (State)
print (City)
print (Zip)
print (Long)
print (Lat)
print (ISO)
print (IP)
reader.close()
# influx configuration - edit these
ifuser = "PASSWORD"
ifpass = "PASSWORD"
#ifdb = "DATABASE"
ifdb = "Test"
ifhost = "192.168.0.1"
ifport = 8081
hostname = socket.gethostname()
measurement_name = (hostname + ":Locations")
print (measurement_name)
# take a timestamp for this measurement
time = datetime.datetime.utcnow()
# format the data as a single measurement for influx
body = [
{
"measurement": measurement_name,
"time": time,
"tags": {
"key": ISO,
"latitude": Lat,
"longitude": Long,
"name": Country
},
"fields": {
"latitude": Lat,
"longitude": Long,
"State": State,
"City": City,
"key": ISO,
"IPAdress": IP,
"name": Country,
"metric": 1
}
}
]
# connect to influx
ifclient = InfluxDBClient(ifhost,ifport,ifuser,ifpass,ifdb)
# write the measurement
ifclient.write_points(body)