Giter Club home page Giter Club logo

multi-pings's Introduction

Multi-Pings

This script will automate ping tests to multiple network nodes instead of having to ping 10 or 20 IP addresses individually. This script is my first project and my introduction to the realm of software ๐Ÿค“. Show me some encouragement by staring ๐Ÿ˜„.

Modules Used

import csv
import os
import time
from datetime import datetime

File Handling

The script runs by first opening a CSV file using the open() function then assigning the return value to a variable as seen below:

lagos = open('Lagos_DCN.csv', 'r',)
accra = open('Accra_DCN.csv', 'r',)
seixal = open('Seixal_DCN.csv', 'r',)
civ = open('CIV_DCN.csv', 'r',)
  • Note: the content of my CSV file looks like this:
    name ip_address
    Node 1 8.8.8.8
    Node 2 ...
    Node 3 ...

Then these files are read using csv.reader() as seen below:

lagos_dcn = csv.reader(lagos)
accra_dcn = csv.reader(accra)
seixal_dcn = csv.reader(seixal)
civ_dcn = csv.reader(civ)

csv.reader() will return an iterable object and assign it to variale as above. You can find more information in the python documentation

A function was now created to handle pings to IP addresses read from the CSV file as seen below:

def run_ping(ip_address):
    ping_reply = os.system('ping -n 3 ' + ip_address)
    return ping_reply

Then the function below will save ping status of IP addresses that are not reachable to a .txt file then use the datetime module imported to include the current date to the filename. From the os module os.makedirs() function is used to create a directory within the script's directory.

def saveResult(nodeName, NodeStatus):
    name = nodeName + ' on ' + datetime.now().strftime('%d-%m-%Y')
    filename = "Result/%s.txt"% name
    if not os.path.exists(os.path.dirname(filename)):
                try:
                    os.makedirs(os.path.dirname(filename))
                except OSError:
                   pass
    with open(filename, "x") as f:
        for items in NodeStatus:
            f.write(items + '\n')
  • since f.write() function does not wirte list rather strings, there was a need to iterate through a list of ping status.

Usage

  • Note: A list was created to hold ping status.

# * This list holds the nodes that are not reachable. 
statusList = []

The code below does the whole magic. So, with lagos_dcn = csv.reader(lagos) object:

print('\n'+ asterisks + '\nLAGOS DCN Connectivity test' + '\n'+ asterisks)
print('\nOpening LAGOS DCN Nodes...........')
time.sleep(3)
for row in lagos_dcn:
    if row[0] == 'name':
        pass
    else:
        print('\n' + asterisks1 + ' Running pings on {} '.format(row[0]) + asterisks1)
        if run_ping(row[1]) == 1:
            stat = '{}'.format(row[0]) + ' is not reachable'
            statusList.append(stat)
        time.sleep(2)
saveResult('Lagos_DCN', statusList)
statusList.clear()
lagos.close()
  • Note:

    • since I was dealing with network devices already in production and due to security, I didn't want to run simultanous pings to those devices which may cause the network device to think that a DDOS attack is coming in then trigger a shutdown ๐Ÿ˜ฐ. So, I used time.sleep(2) to delay pings to the next device in the CSV file by 2 seconds
    • since I am working with 4 CSV file, I decided to reuse the same statusList = [] to hold the result of all unreachable nodes in each file. to achieve this, I clear the entire the list after each iteration using statusList.clear().

multi-pings's People

Contributors

celnet-hub avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

blacdev

multi-pings's Issues

User can upload a CSV file

In the current script, the CSV file is hardcoded (immutable)

Task

  • Rewrite script so that CSV file of IP addresses can be uploaded/requested from the User

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.