-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetDevices.py
103 lines (103 loc) · 2.64 KB
/
NetDevices.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import time
import requests
import psycopg
import logging.handlers
import vars
def get_net_devices():
ret = False
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
API_OBJ_RESPONSE_SIZE = 50
fields = [
'apn',
'bsid',
'carrier',
'carrier_id',
'channel',
'connection_state',
'dns0',
'dns1',
'esn',
'gateway',
'gsn',
'homecarrid',
'hostname',
'iccid',
'id',
'imei',
'imsi',
'ipv4_address',
'ipv6_address',
'is_asset',
'is_gps_supported',
'is_upgrade_supported',
'ltebandwidth',
'mac',
'manufacturer',
'mdn',
'meid',
'mfg_model',
'mfg_product',
'mn_ha_spi',
'mn_ha_ss',
'mode',
'model',
'modem_fw',
'mtu',
'nai',
'name',
'netmask',
'pin_status',
'port',
'prlv',
'profile',
'rfband',
'rfband5g',
'rfchannel',
'roam',
'router',
'rxchannel',
'serial',
'service_type',
'ssid',
'summary',
'txchannel',
'type',
'uid',
'updated_at',
'uptime',
'ver_pkg',
'version',
'wimax_realm'
]
fields = ','.join(fields)
try:
conn = psycopg.connect(vars.conn_string)
cursor = conn.cursor()
cursor.execute('TRUNCATE cradlepoint.net_devices RESTART IDENTITY CASCADE;')
with cursor.copy("COPY cradlepoint.net_devices ({}) FROM STDIN;".format(fields)) as copy:
url = "https://www.cradlepointecm.com/api/v2/net_devices/?fields={0}&limit={1}".format(fields, API_OBJ_RESPONSE_SIZE)
while url:
vars.hits+=1
if vars.hits%50==0:
time.sleep(10)
req = requests.get(url, headers=vars.headers)
req.raise_for_status()
resp = req.json()
url = resp['meta']['next']
for net_device in resp['data']:
copy.write_row(net_device[column] for column in net_device.keys())
cursor.execute('UPDATE cradlepoint.net_devices SET "router_id"=SUBSTRING("router", \'\\d+(?=/$)\')::INT, "router"=NULL WHERE "router" IS NOT NULL;')
conn.commit()
except Exception as e:
logger.exception(e)
ret = True
try:
if (ret):
conn.rollback()
cursor.close()
conn.close()
except Exception as e:
logger.exception(e)
ret = True
return ret