Skip to content

Instantly share code, notes, and snippets.

@dennisheitmann
Last active August 22, 2022 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennisheitmann/16237c99ef2521ad7919a5836f52d065 to your computer and use it in GitHub Desktop.
Save dennisheitmann/16237c99ef2521ad7919a5836f52d065 to your computer and use it in GitHub Desktop.
Ping servers and save information in Excel-Sheet
# MIT License
#
# Copyright (c) 2022 Dennis Heitmann
import sys
import time
import datetime
import socks
import socket
from icmplib import ping
import xlsxwriter
pingHosts = ['mail.nxxt.de',
'www.nxxt.de']
def ping_all_hosts():
global row
global col
col = 0
row = row + 1
print("------------------------------")
print('Time: ', end='')
print(datetime.datetime.now().isoformat())
worksheet.write(row, col, datetime.datetime.now().isoformat())
col = col + 1
for pingHost in pingHosts:
print('Host: ', end='')
print(pingHost)
host = ping(pingHost, count=10, interval=1, timeout=5, privileged=False)
if (host.is_alive):
print('IP: ', end='')
print(host.address)
print('RTT: ', end='')
print(host.avg_rtt)
worksheet.write(row, col, host.avg_rtt)
col = col + 1
print('Loss: ', end='')
print(host.packet_loss)
worksheet.write(row, col, host.packet_loss)
col = col + 1
print('Jitter: ', end='')
print(host.jitter)
worksheet.write(row, col, host.jitter)
col = col + 1
print('Alive: ', end='')
print(host.is_alive)
worksheet.write(row, col, host.is_alive)
col = col + 1
else:
print(host.address, end="")
print(" is not reachable")
col = col + 4
if __name__ == "__main__":
# Create a workbook and add a worksheet (name with datetime stamp)
datetimeStr = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M')
workbook = xlsxwriter.Workbook(datetimeStr + '_' + 'Pings.xlsx')
worksheet = workbook.add_worksheet()
global row
global col
row = 0
col = 0
worksheet.write(row, col, "Timestamp")
col = col + 1
for pingHost in pingHosts:
worksheet.write(row, col, pingHost)
col = col + 1
worksheet.write(row, col, pingHost)
col = col + 1
worksheet.write(row, col, pingHost)
col = col + 1
worksheet.write(row, col, pingHost)
col = col + 1
row = row + 1
col = 0
worksheet.write(row, col, "Timestamp")
col = col + 1
for pingHost in pingHosts:
worksheet.write(row, col, "RTT")
col = col + 1
worksheet.write(row, col, "Loss")
col = col + 1
worksheet.write(row, col, "Jitter")
col = col + 1
worksheet.write(row, col, "Alive")
col = col + 1
try:
while(True):
ping_all_hosts()
time.sleep(10)
except KeyboardInterrupt:
print("Bye")
# Saving Excel file
workbook.close()
sys.exit()
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment