Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Jason Friedman Newsgroups: comp.lang.python Subject: Re: Python,ping,csv Date: Tue, 12 Apr 2016 00:01:00 +0000 (UTC) Lines: 60 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de +lr9L0aFEmfJVXL8Y6u2HQhxuAHYA6pPZkiclWiZ8Jdg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*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; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'res': 0.09; 'output': 0.13; 'localhost': 0.16; 'match:': 0.16; 'message- id:@post.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subprocess': 0.16; 'writer': 0.16; 'bytes': 0.18; '%s"': 0.22; 'import': 0.24; 'header:User-Agent:1': 0.26; 'command': 0.26; 'header:X-Complaints-To:1': 0.26; '---': 0.28; 'loss,': 0.29; 'point': 0.33; 'received:comcast.net': 0.33; 'me?': 0.34; 'file': 0.34; 'expected': 0.35; 'skip:i 20': 0.36; 'data.': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'format': 0.39; 'to:addr:python.org': 0.40; 'address': 0.61; 'reach': 0.61; 'skip:w 30': 0.64; 'results': 0.66; '100%': 0.72; 'address,': 0.77; '100': 0.79; 'subject:,': 0.82; "'3',": 0.84; 'received,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 174.51.145.205 (Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:106899 > 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), ""))