Files
SC200/sc200-pull.py
2022-10-20 08:39:23 +08:00

298 lines
7.7 KiB
Python
Executable File

#!/usr/bin/python3
from netaddr import IPNetwork
from ipaddress import IPv4Address
from xmlrpc.client import ServerProxy
#import sqlite3
import socket
import csv
import queue
import threading
import sys
import http.client
WORKERS = 10
if len(sys.argv) == 1:
Nets = [
'10.250.2.128/28',
'10.250.4.128/28',
'10.250.6.192/26',
'10.250.7.192/26',
'10.250.8.192/27',
'10.250.9.192/27',
'10.250.48.0/27',
'10.250.48.32/27',
'10.250.48.64/27',
'10.250.48.96/27',
'10.250.48.128/27',
'10.250.48.160/27',
'10.250.49.0/27',
'10.250.49.32/27',
'10.250.49.64/27',
'10.250.49.128/29',
'10.250.49.128/27',
'10.250.49.160/27',
'10.250.50.0/27',
'10.250.50.32/27',
'10.250.50.64/27',
'10.250.50.96/27',
'10.250.50.128/27',
'10.250.50.160/27',
'10.250.50.192/27',
'10.250.50.224/27',
'10.250.51.0/27',
'10.250.51.32/27',
'10.250.51.64/27',
'10.250.51.96/27',
'10.250.51.128/27',
'10.250.52.0/27',
'10.250.52.32/27',
'10.250.52.64/27',
'10.250.52.96/27',
'10.250.52.128/27',
'10.250.52.160/27',
'10.250.52.192/27',
'10.250.52.224/27',
'10.250.53.0/27',
'10.250.63.224/27',
'10.250.65.240/28',
'10.250.66.24/29',
'10.250.67.240/28',
'10.250.68.24/29',
'10.250.69.24/29',
'10.250.70.24/29',
'10.250.72.240/28',
'10.250.73.240/28',
'10.250.74.24/29',
'10.250.75.24/29',
'10.250.76.24/29',
'10.250.77.24/29',
'10.250.78.240/28',
'10.250.79.24/29',
'10.250.81.24/29',
'10.250.83.24/29',
'10.250.84.24/29',
'10.250.85.24/29',
'10.250.86.24/29',
'10.250.89.240/28',
'192.168.1.10/32',
'192.168.2.10/32',
'192.168.3.10/32',
'192.168.4.10/32',
'192.168.5.10/32',
'192.168.6.10/32',
'192.168.7.10/32',
'192.168.32.10/32',
'192.168.33.10/32',
'192.168.34.10/32',
'192.168.35.10/32',
'192.168.36.10/32',
'192.168.37.10/32',
'192.168.65.10/32',
'192.168.66.10/32',
'192.168.67.10/32',
'192.168.68.10/32',
'192.168.69.20/32',
'192.168.70.10/32',
'192.168.97.10/32',
'192.168.98.10/32',
'192.168.99.10/32',
'192.168.100.10/32',
'192.168.101.10/32',
'192.168.102.10/32',
'192.168.103.10/32',
'192.168.104.10/32',
'192.168.105.10/32',
'192.168.106.10/32',
'192.168.107.10/32',
'192.168.108.10/32',
'192.168.109.10/32',
'192.168.110.10/32',
'192.168.111.10/32',
'192.168.112.20/32',
'192.168.113.10/32',
'192.168.115.10/32',
'192.168.129.10/32',
'192.168.130.10/32',
'192.168.131.10/32',
'192.168.132.10/32',
'192.168.133.10/32',
'192.168.134.10/32',
'192.168.135.10/32',
'192.168.136.10/32',
'192.168.137.10/32',
'192.168.138.10/32',
'192.168.139.10/32',
'192.168.140.10/32',
'192.168.160.10/32',
'192.168.161.10/32',
'192.168.162.10/32',
'192.168.163.10/32',
'192.168.164.10/32',
'192.168.165.10/32',
'192.168.167.10/32',
'192.168.169.10/32',
'192.168.192.10/32',
'192.168.193.10/32',
'192.168.194.10/32',
'192.168.195.10/32',
'192.168.196.10/32'
]
else:
Nets = sys.argv[1:]
#Nets = [
#'10.250.48.0/27']
#print((Nets))
Settings = [
'Site-Name',
'Site-Address',
'System-Location',
'Site-Notes',
'IP-Address',
'Subnet-Mask',
'Gateway-Address',
'Float-Voltage',
'Battery-Capacity',
'AC-Rectifier-Current-Limit',
'High-Float-Threshold',
'Enable-Active-Voltage-Control',
'Enable-Temperature-Compensation',
'Temperature-Compensation-Slope',
'Temperature-Compensation-Reference-Temperature',
'Temperature-Compensation-Upper-Limit',
'Temperature-Compensation-Lower-Limit',
'Enable-Equalize',
'Equalize-Voltage',
'Equalize-Duration',
'Enable-Fast-Charge',
'Fast-Charge-Voltage',
'Fast-Charge-Voltage-Threshold',
'Fast-Charge-Ampere-Hour-Threshold',
'Fast-Charge-Recharge-Percentage',
'Fast-Charge-Maximum-Duration',
'Fast-Charge-Ampere-Hour-Stop-Threshold',
'Enable-Battery-Current-Limit',
'BCL-Limit',
'Enable-Battery-Test',
'Battery-Test-Duration',
'Battery-Test-Termination-Voltage',
'LVD-Disconnect-Voltage:1',
'LVD-Reconnect-Voltage:1'
] + \
['Alarm-Name'] + \
['Alarm-Name:' + repr(x) for x in range(1, 59)] + \
['Alarm-Severity'] + \
['Alarm-Severity:' + repr(x) for x in range(1, 59)]
NetworkSettings = [
'IP-Address',
'Subnet-Mask',
'Gateway-Address'
]
def GetStatusCode(host, path="/"):
try:
conn = http.client.HTTPConnection(host)
conn.request("GET", path)
return conn.getresponse().status
except Exception:
return None
class Worker(threading.Thread):
def __init__(self, IPs, Results):
self.__IPs = IPs
self.__Results = Results
self.parent = threading.current_thread()
threading.Thread.__init__(self)
def run(self):
retry = False
socket.setdefaulttimeout(5)
while 1:
IP = self.__IPs.get()
if IP is None:
self.__Results.put(None)
break
if GetStatusCode(str(IP), "/languages") == 200:
print((self.getName() + ' processing: ' + str(IP)))
while True:
proxy = ServerProxy('http://%s/xmlrpc' % IP)
try:
Values = proxy.db.get(Settings)
except socket.timeout:
retry = True
continue
if retry:
retry = False
else:
break
if len(Values) > 0:
for idx, setting in enumerate(Settings):
if setting in NetworkSettings:
print(Values[idx])
Values[idx] = str(IPv4Address(Values[idx]))
Values.insert(0, str(IP))
self.__Results.put(Values)
else:
print((self.getName() + ' skipping: ' + str(IP)))
def Output():
Count = WORKERS
fieldNames = ['IP'] + Settings
with open('/tmp/sc200.csv', 'w', newline='') as outFile:
csvWriter = csv.DictWriter(outFile, fieldnames=fieldNames,
delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
csvWriter.writeheader()
while 1:
Row = Results.get()
if Row is None:
if Count > 1:
Count -= 1
#print(Count)
else:
break
else:
csvWriter.writerow(dict(list(zip(fieldNames, Row))))
outFile.close()
IPs = queue.Queue(0)
Results = queue.Queue(0)
for i in range(WORKERS):
Worker(IPs, Results).start()
for SubNet in Nets:
Net = IPNetwork(SubNet)
for IP in Net:
if ((IP != Net.network and IP != Net.broadcast) or
Net.broadcast is None):
IPs.put(IP)
for i in range(WORKERS):
IPs.put(None)
if threading.current_thread() is not threading.main_thread():
sys.exit(0)
Output()
sys.exit(0)