Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pping.py
#1
i wrote this command-line interface script to do multiple pings in parallel:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
"""Ping multiple IP addresses in parallel.

file          pping.py
name          pping - parallel ping
purpose       Ping multiple IP addresses in parallel
email         10054452614123394844460370234029112340408691

The intent is that this command works correctly under both Python 2 and
Python 3.  Please report failures or code improvement to the author.
"""

__license__ = """
Copyright (C) 2017, by Phil D. Howard - all other rights reserved

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

The author may be contacted by decoding the number
10054452614123394844460370234029112340408691
(provu igi la numeron al duuma)
"""

from multiprocessing import Process
from socket import inet_pton, AF_INET, AF_INET6
from subprocess import call
from sys import argv, exc_info, stderr, stdout


def validipv4(addr):
    """Return True if the given IP address is valid for IPv4 or False if not.

function       validipv4
argument       1 (str) that may be an IPv4 address
purpose        return True if the given IP address is valid for IPv4 or False if not
"""
    try:
        inet_pton(AF_INET,addr)
        r = True
    except (OSError,IOError):
        r = False
    return r


def validipv6(addr):
    """Return True if the given IP address is valid for IPv6 or False if not.

function       validipv6
argument       1 (str) that maybe an IPv6 address
purpose        return True if the given IP address is valid for IPv6 or False if not
"""
    try:
        inet_pton(AF_INET6,addr)
        r = True
    except (OSError,IOError):
        r = False
    return r


def validip(addr):
    """Return True if the given IP address is valid for IPv4 or IPv6 or False if not either.

function       validip
argument       1 (str) that maybe an IP address
purpose        return True if the given IP address is valid for IPv4 or IPv6 or
               False if not either.   
"""
    return validipv4(addr) or validipv6(addr)


def run(cmd):
    try:
        call(cmd)
    except KeyboardInterrupt:
        pass
    return

def main(args):
    na = len( args )
    cmds = []
    opts = []
    for n in range( na ):
        if n < 1:
            continue
        arg = args[ n ]
        stdout.flush()
        if validip( arg ):
            cmds += [ opts + [ arg ] ]
        if arg[0] == '-':
            n = n + 1
            narg = args[ n ]
            opts += [ arg ]
            opts += [ narg ]
    pros = []
    for pc in cmds:
        pro=Process(target=run,args=([['ping']+pc]))
        pro.start()
        pros+=[pro]
    for pro in pros:
        try:
            pro.join(360)
        except KeyboardInterrupt:
            pass
    return 0


if __name__ == '__main__':
    try:
        result = main( argv )
        stdout.flush()
    except KeyboardInterrupt:
        result = 141
        print( '' )
    except IOError:
        result = 142

    try:
        exit( int( result ) )
    except ValueError:
        print( str( result ), file=stderr )
        exit( 1 )
    except TypeError:
        if result == None:
            exit( 0 )
        exit( 255 )

# EOF
Output:
lt1/forums /home/forums 13> pping 2001:4860:4860::8844 8.8.4.4 PING 8.8.4.4 (8.8.4.4) 56(84) bytes of data. PING 2001:4860:4860::8844(2001:4860:4860::8844) 56 data bytes 64 bytes from 8.8.4.4: icmp_seq=1 ttl=46 time=143 ms 64 bytes from 2001:4860:4860::8844: icmp_seq=1 ttl=57 time=143 ms 64 bytes from 8.8.4.4: icmp_seq=2 ttl=46 time=142 ms 64 bytes from 2001:4860:4860::8844: icmp_seq=2 ttl=57 time=143 ms 64 bytes from 8.8.4.4: icmp_seq=3 ttl=46 time=142 ms 64 bytes from 2001:4860:4860::8844: icmp_seq=3 ttl=57 time=143 ms ^C --- 8.8.4.4 ping statistics --- --- 2001:4860:4860::8844 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2001ms 3 packets transmitted, 3 received, 0% packet loss, time 2001ms rtt min/avg/max/mdev = 142.442/142.716/143.045/0.249 ms rtt min/avg/max/mdev = 143.508/143.725/143.845/0.345 ms lt1/forums /home/forums 14>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020