ich hoffe das ich hier jetzt auch richtig bin. Ich bin was programmieren angeht der absolute noob. Aber ich habe mir ein Projekt überlegt an dem ich jetzt mühsam einiges geschafft habe. An dem ich jetzt aber hänge und trotz google und auch hier suche ich keine lösung finde. Habe mir auch das unicode paper von python angeschaut aber ich komm einfach nicht weiter.
Bei meinem Script handelt es sich um eine art Pingtool.
Ich lese eine txt Datei ein in der ein Host und eine IP steht und pinge diese IP und poste das Ergebnis dann in eine weitere txt Datei. Damit ich später mir aus den gesammelten Daten eine Grafische Ausgabe machen kann.
Mein Script läuft unter Mac OS wunderbar nur auf Windows nicht. Hier brauche ich jetzt hilfe.
Hier mein Code:
Code: Alles auswählen
import sys
from time import sleep
import os
import time
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
# Erkenne das Betriebssystem nt = Windows, posix = Linux/Unix & Mac
scanos = os.name
timestamp = time.strftime('%d.%m.%Y %H:%M:%S', time.localtime()) # Zeitstempel erzeugen
with open("ip_list.txt") as file:
dump = file.read()
dump = dump.splitlines()
print(dump)
# Ping each ip address in the list and write the result to output.txt
def ping_plotter_posix(f):
#timestamp = time.strftime('%d.%m.%Y %H:%M:%S', time.localtime()) # Zeitstempel erzeugen
for i in dump:
# hier wird die ip_lis.txt aufgesplittet in ip und host und daraus wird dann der pingbefehl gebaut
ip = i.split()[1]
host = i.split()[0]
#print(host)
#print(ip)
res = os.popen(f"ping {ip} -c 5").read() # pingbefehl wird auf der konsole ausgeführt
# if anweisung zum suchen eine bestimmten ergebnisses, hier 64 bytes
if "64 bytes" in res:
#print(res)
lines = res.splitlines()
resultline = lines[9]
part = resultline.split("/")
avg = part[4]
#print(avg)
# hier splitte ich das ergebnis nach , auf um die Porzentzahl für den packetloss zu ermitteln
resultline1 = lines[8]
part1 = resultline1.split(",")
ploss = part1[2]
#print(ploss)
f.write(str(host) + '\t' + str(timestamp) + '\t' + str(ip) + '\t' + 'UP' '\t' + 'avg ' + avg + '\t' + ploss + '\n')
#f.close()
else:
# print(res)
f.write(str(host) + '\t' + str(timestamp) + '\t' + str(ip) + '\t' + 'DOWN X X X X X' + '\n')
def ping_plotter_nt(f):
# timestamp = time.strftime('%d.%m.%Y %H:%M:%S', time.localtime()) # Zeitstempel erzeugen
for i in dump:
# hier wird die ip_lis.txt aufgesplittet in ip und host und daraus wird dann der pingbefehl gebaut
ip = i.split()[1]
host = i.split()[0]
# print(host)
# print(ip)
res = os.popen(f"ping {ip} -n 5").read() # pingbefehl wird auf der konsole ausgeführt
# if anweisung zum suchen eine bestimmten ergebnisses, hier 64 bytes
if "32 bytes" in res:
# print(res)
lines = res.splitlines()
resultline = lines[11]
part = resultline.split("=")
avg = part[3]
print(avg)
# hier splitte ich das ergebnis nach , auf um die Porzentzahl für den packetloss zu ermitteln
resultline1 = lines[9]
part1 = resultline1.split(",")
ploss = part1[0]
print(ploss)
f.write(str(host) + '\t' + str(timestamp) + '\t' + str(
ip) + '\t' + 'UP' '\t' + 'avg ' + avg + '\t' + ploss + '\n')
# f.close()
else:
print(res)
f.write(str(host) + '\t' + str(timestamp) + '\t' + str(ip) + '\t' + 'DOWN X X X X X' + '\n')
# loop for function ping_plotter, range = loopcount + sleep timer
try:
print("Press Ctrl-C to terminate")
if 'posix' in scanos:
f = open("output.txt", "a")
for loop in range(3):
ping_plotter_posix(f)
sleep(1)
f.close()
elif 'nt' in scanos:
f = open("output.txt", "a")
for loop in range(3):
ping_plotter_nt(f)
sleep(1)
f.close()
else:
print('Das Betriebssystem ' + color.BOLD + color.RED + scanos + color.END + ' ist mir nicht bekannt.')
sys.exit()
except KeyboardInterrupt:
print("Press Ctrl-C to terminate")
pass
# Open output.txt file
with open("output.txt") as file:
output = file.read()
print(output)
Wenn ich jetzt dieses Script auf Windows ausführe dann bekomme ich Folgende Fehlermeldung.
Traceback (most recent call last):
File "C:\Users\sascha\Documents\pyscript\main.py", line 117, in <module>
ping_plotter_nt(f)
File "C:\Users\XXX\Documents\pyscript\main.py", line 77, in ping_plotter_nt
res = os.popen(f"ping {ip} -n 5").read() # pingbefehl wird auf der konsole ausgeführt
File "C:\Users\XXX\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 18: character maps to <undefined>
Ich habe selbst jetzt mehrere Codecs ausprobiert aber leider ohne erfolg.
Kann mich jemand mit der Nase auf die Lösung stoßen?