swapped over to json for output

If two servers were found at the same time, they might try to write to the xlsx file  at the same time. this could corrupt the file. outputting to json, and just using another script to convert is much safer.
This commit is contained in:
Astatin3
2023-09-05 07:23:38 -06:00
parent 04322bacee
commit c7b5427fae
3 changed files with 60 additions and 25 deletions
+11 -1
View File
@@ -6,5 +6,15 @@ Usage:
Usage:
srvstatus.py -p IP
srvstatus.py -f inlist.txt
srvstatus.py -f inlist.txt -o outfile.xlsx
srvstatus.py -f inlist.txt -o outfile.json
```
Then convert json to xlsx using:
```
json2xlsx.py infile.json outfile.xlsx
```
Dependencies:
```
Pillow mcstatus openpyxl
```
+18
View File
@@ -0,0 +1,18 @@
import sys
import json
from openpyxl import Workbook
inputfile = sys.argv[1]
outputfile = sys.argv[2]
book = Workbook()
sheet = book.active
sheet.append(("ip", "latency", "pver", "ver", "cplayers", "mplayers", "motd", "isicon"))
with open(inputfile) as f:
data = json.load(f)
for entry in data:
sheet.append((entry["ip"], entry["latency"], entry["pver"], entry["ver"], entry["cplayers"], entry["mplayers"], entry["motd"], entry["isicon"]))
book.save(outputfile)
+31 -24
View File
@@ -2,38 +2,34 @@ import io
import math
import base64
import sys
import json
from time import sleep
from threading import Thread
from PIL import Image
from mcstatus import JavaServer
from openpyxl import load_workbook
from openpyxl import Workbook
imgsize = (16,16)
sleeptime = 0.1
nullimg = b"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAaklEQVQ4T62TWw7AIAgEl/sf2qoRY4ywa1tD/JvhoRjGKUAN/RhQY1y3sKdpEltht2Z17Mmm4A3cW3AjE+yVTk4RnNqUBdGMJEE2YCpgr5MKGNymHwoUOBSo8FGw/j72J1LBDfzPMn1d5wfNZUf5qKNxAQAAAABJRU5ErkJggg=="
threadcount = 1
savecount = 5
foundcount = 0
outputfilename = None
def appenddata(ip, latency, pver, ver, cplayers, mplayers, motd, isicon):
book = None
sheet = None
#with open("output.txt", "a") as file:
# file.write(f"{ip}|{latency}|{pver}|{ver}|{cplayers}|{mplayers}|{motd}|{isicon}\n")
data = None
try:
book = load_workbook(filename=outputfilename)
sheet = book.active
with open(outputfilename, "r") as file:
data = json.load(file)
except:
book = Workbook()
sheet = book.active
sheet.append(("ip", "latency", "pver", "ver", "cplayers", "mplayers", "motd", "isicon"))
sheet.append((ip, latency, pver, ver, cplayers, mplayers, motd, isicon))
book.save(outputfilename)
appenddata(ip, latency, pver, ver, cplayers, mplayers, motd, isicon)
data.append({"ip": ip, "latency": latency, "pver": pver, "ver": ver, "cplayers": cplayers, "mplayers": mplayers, "motd": motd, "isicon": isicon})
with open(outputfilename, "w") as file:
json.dump(data, file)
def img2ascii(icon):
@@ -82,9 +78,6 @@ def scanip(ip):
motdtxt = status.motd.to_plain()
if outputfilename != None:
appenddata(ip, status.latency, ver.protocol, ver.name, players.online, players.max, motdtxt, isicon)
motd = ["", ""]
if "\n" in motdtxt:
split = motdtxt.split("\n")
@@ -112,10 +105,14 @@ def scanip(ip):
line += f"{motd[1]}"
output += f"{line}\n"
#with open("output.txt", "a") as file:
# file.write(f"{output}\n")
print(f"{output}\n") # If everything isn't printed at the same time, the output text could become mixed.
global foundcount
foundcount += 1
if outputfilename != None:
appenddata(ip, math.ceil(status.latency), ver.protocol, ver.name, players.online, players.max, motdtxt, isicon)
threadcount -= 1
return True
threads = []
@@ -124,8 +121,15 @@ if sys.argv[1] == "-p":
if not scanip(sys.argv[2]):
print("Server not online")
elif sys.argv[1] == "-f":
if sys.argv[3] == "-o":
if len(sys.argv) == 4 and sys.argv[3] == "-o":
outputfilename = sys.argv[4]
try:
open(outputfilename, "r")
except:
with open(outputfilename, "a") as file:
file.write("[]")
with open(outputfilename, "r") as file:
data = json.load(file)
with open(sys.argv[2]) as file:
length = sum(1 for line in open(sys.argv[2]))
@@ -137,8 +141,9 @@ elif sys.argv[1] == "-f":
per = round(100*(index/length), 2)
pergap = ' '*(6-len(str(per)))
thrgap = ' '*(3-len(str(threadcount)))
fndgap = ' '*(4-len(str(foundcount)))
print(f"Scanning IP: {ip}{ipgap} {pergap}{per}% done {thrgap} {threadcount} pending", end='\r')
print(f"Scanning IP: {ip}{ipgap} {pergap}{per}% done {thrgap} {threadcount} pending {fndgap}{foundcount} found", end='\r')
thread = Thread(target = scanip, args = (ip, ))
thread.start()
threads.append(thread)
@@ -148,8 +153,10 @@ elif sys.argv[1] == "-f":
for thread in threads:
thread.join()
cursavecount = 10
appenddata("ip", "latency", "pver", "ver", "cplayers", "mplayers", "motd", "isicon")
else:
print("Usage:\n"+\
"srvstatus.py -p IP\n"+\
"srvstatus.py -f inlist.txt\n"+\
"srvstatus.py -f inlist.txt -o outfile.xlsx")
"srvstatus.py -f inlist.txt -o outfile.xlsx")