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


Groups > comp.lang.python > #106657 > unrolled thread

Python,ping,csv

Started bySmith <smith@a-team.it>
First post2016-04-08 09:25 +0200
Last post2016-04-12 00:01 +0000
Articles 7 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#106657 — Python,ping,csv

FromSmith <smith@a-team.it>
Date2016-04-08 09:25 +0200
SubjectPython,ping,csv
Message-ID<o1JNy.43731$pt.31548@tornado.fastwebnet.it>
Hello to all,
I have this little script that pings certain ip addresses.
Considering that I am a newbie to the Python programming language, can 
you help me change these lines in order to put the output into a csv file?
Sorry for unclear English
Thanks in advance


import subprocess

for ping in range(1,254):
     address = "10.24.59." + str(ping)
     res = subprocess.call(['ping', '-c', '3', address])
     if res == 0:
         print ("ping to", address, "OK")
     elif res == 2:
         print ("no response from", address)
     else:
         print ("ping to", address, "failed!")

[toc] | [next] | [standalone]


#106667

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2016-04-08 05:18 -0400
Message-ID<mailman.75.1460107100.2253.python-list@python.org>
In reply to#106657
On Fri, Apr 8, 2016 at 3:25 AM, Smith <smith@a-team.it> wrote:
> Hello to all,
> I have this little script that pings certain ip addresses.
> Considering that I am a newbie to the Python programming language, can you
> help me change these lines in order to put the output into a csv file?
> Sorry for unclear English
> Thanks in advance
>
>
> import subprocess
>
> for ping in range(1,254):
>     address = "10.24.59." + str(ping)
>     res = subprocess.call(['ping', '-c', '3', address])
>     if res == 0:
>         print ("ping to", address, "OK")
>     elif res == 2:
>         print ("no response from", address)
>     else:
>         print ("ping to", address, "failed!")
> --
> https://mail.python.org/mailman/listinfo/python-list

What do you want in your csv file?  Show a sample output

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays

[toc] | [prev] | [next] | [standalone]


#106772

FromJason Friedman <jsf80238@gmail.com>
Date2016-04-09 21:29 -0600
Message-ID<mailman.0.1460259273.424.python-list@python.org>
In reply to#106657
> for ping in range(1,254):
>     address = "10.24.59." + str(ping)
>     res = subprocess.call(['ping', '-c', '3', address])
>     if res == 0:
>         print ("ping to", address, "OK")
>     elif res == 2:
>         print ("no response from", address)
>     else:
>         print ("ping to", address, "failed!")

Note that with Python 3.3+ you can simplify slightly:

from ipaddress import IPv4Network
for address in IPv4Network('10.24.59.0/24').hosts():
    res = subprocess.call(['ping', '-c', '3', address])
    ...

[toc] | [prev] | [next] | [standalone]


#106867

FromSmith <smith@a-team.it>
Date2016-04-11 10:22 +0200
Message-ID<19JOy.43875$pt.16946@tornado.fastwebnet.it>
In reply to#106772
Il 10/04/2016 05:29, Jason Friedman ha scritto:
>> for ping in range(1,254):
>>      address = "10.24.59." + str(ping)
>>      res = subprocess.call(['ping', '-c', '3', address])
>>      if res == 0:
>>          print ("ping to", address, "OK")
>>      elif res == 2:
>>          print ("no response from", address)
>>      else:
>>          print ("ping to", address, "failed!")
>
> Note that with Python 3.3+ you can simplify slightly:
>
> from ipaddress import IPv4Network
> for address in IPv4Network('10.24.59.0/24').hosts():
>      res = subprocess.call(['ping', '-c', '3', address])
>      ...
>
Thanks a lot ;-)

[toc] | [prev] | [next] | [standalone]


#106869

FromSmith <smith@a-team.it>
Date2016-04-11 11:24 +0200
Message-ID<L3KOy.43878$pt.32317@tornado.fastwebnet.it>
In reply to#106772
Il 10/04/2016 05:29, Jason Friedman ha scritto:
>> for ping in range(1,254):
>>      address = "10.24.59." + str(ping)
>>      res = subprocess.call(['ping', '-c', '3', address])
>>      if res == 0:
>>          print ("ping to", address, "OK")
>>      elif res == 2:
>>          print ("no response from", address)
>>      else:
>>          print ("ping to", address, "failed!")
>
> Note that with Python 3.3+ you can simplify slightly:
>
> from ipaddress import IPv4Network
> for address in IPv4Network('10.24.59.0/24').hosts():
>      res = subprocess.call(['ping', '-c', '3', address])
>      ...
>


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])


[toc] | [prev] | [next] | [standalone]


#106898

FromJason Friedman <jsf80238@gmail.com>
Date2016-04-11 19:17 -0600
Message-ID<mailman.41.1460423871.15650.python-list@python.org>
In reply to#106869
> 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), ""))

[toc] | [prev] | [next] | [standalone]


#106899

FromJason Friedman <jsf80238@gmail.com>
Date2016-04-12 00:01 +0000
Message-ID<mailman.42.1460430308.15650.python-list@python.org>
In reply to#106869
> 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), ""))




[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web