Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106898

Re: Python,ping,csv

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Jason Friedman <jsf80238@gmail.com>
Newsgroups comp.lang.python
Subject Re: Python,ping,csv
Date Mon, 11 Apr 2016 19:17:49 -0600
Lines 54
Message-ID <mailman.41.1460423871.15650.python-list@python.org> (permalink)
References <o1JNy.43731$pt.31548@tornado.fastwebnet.it> <CANy1k1h392HiXq-onCAFj1zJkK0Z31AXeR6fEtnf7fZ-5fHkaQ@mail.gmail.com> <mailman.0.1460259273.424.python-list@python.org> <L3KOy.43878$pt.32317@tornado.fastwebnet.it> <CANy1k1hEMDmkZaQugrbgWdY3ekv5EQocyUJ9pLzXFU-y8++r1A@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de dfpZR1X8soDtsdbyuS7EhAO1iY6N63egF4OKz0mdKCJA==
Return-Path <jsf80238@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'subject:Python': 0.05; '"""': 0.05; 'sys': 0.05; 'hosts': 0.07; 'mask': 0.07; "'w',": 0.09; 'csv': 0.09; 'ping': 0.09; 'res': 0.09; 'output': 0.13; 'localhost': 0.16; 'match:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subprocess': 0.16; 'writer': 0.16; 'bytes': 0.18; '&gt;': 0.18; '%s"': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'command': 0.26; 'message-id:@mail.gmail.com': 0.27; '---': 0.28; 'loss,': 0.29; 'to:name:python-list': 0.30; 'point': 0.33; 'me?': 0.34; 'file': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'expected': 0.35; 'skip:i 20': 0.36; 'received:209.85': 0.36; 'data.': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'received:209': 0.38; 'format': 0.39; 'to:addr:python.org': 0.40; 'address': 0.61; 'reach': 0.61; 'url:0': 0.63; 'skip:w 30': 0.64; 'results': 0.66; '100%': 0.72; 'address,': 0.77; '100': 0.79; 'url:10': 0.79; 'subject:,': 0.82; "'3',": 0.84; 'received,': 0.84; 'url:59': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to; bh=o6+gnQ+ftJUG2F12jRejXbcPKafh3zMrsh0J87V25tA=; b=zGoMO4nVBW2tyFe3YyfQ5UTMUtQb6eyIKtGYyQhcYodWflCrs5k1zRPTJMH5SXvVtT 6ffLAADC5FwLPCOQ2A+tHOCLEa2UbUHDRZZqhEWSeCLLlDGCx/SQNmdzAeD/uXQ9j8Mg xK/yC4MY5TC9lwYIb2TJJ2PMEOg44agH4OVcrHwne71yaK5DbEMImcMZNeyyxL6/3yh8 mx0s8mvodudJBFIP/PA+QCXXHxUDXPr5RpFcepwwK+PiQ3ZTKaObZhZ40a4OtiQLcmmP dZJOi+bGkAeMtcnwyG/wGvCPW7DSaf51fdWb77YOINUACSkoHAAbwkLSqqP6oFKEx1T2 dPdw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=o6+gnQ+ftJUG2F12jRejXbcPKafh3zMrsh0J87V25tA=; b=PlxNWlllX7TpnfIsN9AxlvG2xxdWEAndluBapJ+D9bqKrpf/CMDPfRKS/G+MkC1rwK 29AcI377lQ8tRwojGJ0+CgECVWUK6DbKnAOncPrBpxjyF64CUzxo67VssgFgKyQTa/Nk YVYvT0CmdUYnO9fRnGHaduafTHIkab3JQ4GFMvFoPrJhprO+F3llz1alpehWAMsLusYP Nu+iTh9v8XnRJcXI9GwaVauWwr78cjDUJud7xL0vhmS+nD1w50yOMAYGwvlx4SAsbPZi FRota7IYu39Pun8XKVLlKD04jk2jOEjus+wz7C+yeg6IgfR0RniBWfgJgwUDu/08gSAR 0p8w==
X-Gm-Message-State AOPr4FXgIgwVKF19Eh2S9K0ErzBIWUv5u1lHmFTfZN2jU/9vEdDuf9D75jks4NteJINSoPHSoS83i0ENzBW8mg==
X-Received by 10.140.29.246 with SMTP id b109mr760825qgb.2.1460423869124; Mon, 11 Apr 2016 18:17:49 -0700 (PDT)
In-Reply-To <L3KOy.43878$pt.32317@tornado.fastwebnet.it>
X-Content-Filtered-By Mailman/MimeDel 2.1.21
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CANy1k1hEMDmkZaQugrbgWdY3ekv5EQocyUJ9pLzXFU-y8++r1A@mail.gmail.com>
X-Mailman-Original-References <o1JNy.43731$pt.31548@tornado.fastwebnet.it> <CANy1k1h392HiXq-onCAFj1zJkK0Z31AXeR6fEtnf7fZ-5fHkaQ@mail.gmail.com> <mailman.0.1460259273.424.python-list@python.org> <L3KOy.43878$pt.32317@tornado.fastwebnet.it>
Xref csiph.com comp.lang.python:106898

Show key headers only | View raw


> I added a line.
> I would need to put the output into a csv file which contained the results
> of the hosts up and down.
> Can you help me?
>
>
> import subprocess
> from ipaddress import IPv4Network
> for address in IPv4Network('10.24.59.0/24').hosts():
>         a = str(address)
>         res = subprocess.call(['ping', '-c', '3', address])

"""Typical output from ping:

$ ping -c 3 10.2.2.2
PING 10.2.2.2 (10.2.2.2) 56(84) bytes of data.

--- 10.2.2.2 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 1999ms


$ ping -c 3 localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.042 ms

--- localhost ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.030/0.037/0.042/0.007 ms
"""

import csv
import ipaddress
import re
import subprocess
import sys
NETWORK = "192.168.1.0"
MASK = "24"
with open('some.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(("Address", "% success"))
    for address in ipaddress.IPv4Network('%s/%s' % (NETWORK, MASK)).hosts():
        print("Pinging %s ..." % address, file=sys.stderr)
        command = "ping -c 3 %s" % address
        output = subprocess.getoutput(command)
        match = re.search(r"(\d+)% packet loss", output)
        if match:
            percent_lost = match.group(1)
            writer.writerow((str(address), 100 - int(percent_lost)))
        else:
            # If we reach this point the ping command output
            # was not in the expected output format
            writer.writerow((str(address), ""))

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python,ping,csv Smith <smith@a-team.it> - 2016-04-08 09:25 +0200
  Re: Python,ping,csv Joel Goldstick <joel.goldstick@gmail.com> - 2016-04-08 05:18 -0400
  Re: Python,ping,csv Jason Friedman <jsf80238@gmail.com> - 2016-04-09 21:29 -0600
    Re: Python,ping,csv Smith <smith@a-team.it> - 2016-04-11 10:22 +0200
    Re: Python,ping,csv Smith <smith@a-team.it> - 2016-04-11 11:24 +0200
      Re: Python,ping,csv Jason Friedman <jsf80238@gmail.com> - 2016-04-11 19:17 -0600
      Re: Python,ping,csv Jason Friedman <jsf80238@gmail.com> - 2016-04-12 00:01 +0000

csiph-web