Python Forum

Full Version: Create a program that PING a list of IPs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Could somebody help me creating a program that Ping a list of IP adresses and save thta result of that Pinf in a text file in order to have a historic record

I made tis code but it is not working as expected


import os
import csv
import time
import datetime
import logging

def check_ping(hostname):
    response = os.system("fping -r 10 -q " + hostname + " >/dev/null")
    if response == 0:
        check_ping = "[OK]"
    else:
        check_ping = "[Error]"
 
    return check_ping

with open('ip-source.txt') as file:
    dump = file.read()
    dump = dump.splitlines()

    
    for ip in dump:
        
        os.system('cls')
        print('Pinging now:', ip)
        print('-'*60)
        os.system('ping -n 2 {}'.format(ip))

        print('-'*60)
        time.sleep(5)
Should not be using os.system() anymore.
Use subprocess.
doc Wrote:The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle.
Can write a example.

ip2.txt:
Output:
google.com python.org
import subprocess

with open('ip2.txt') as f,open('ip_out.txt', 'wb') as f_out:
    for adress in f:
        adress = adress.strip()
        response = subprocess.run(['ping', adress], capture_output=True)
        print(response.stdout.decode())
        f_out.write(response.stdout)
ip_out.txt:
Output:
Pinging google.com [2a00:1450:400f:80b::200e] with 32 bytes of data: Reply from 2a00:1450:400f:80b::200e: time=25ms Reply from 2a00:1450:400f:80b::200e: time=38ms Reply from 2a00:1450:400f:80b::200e: time=39ms Reply from 2a00:1450:400f:80b::200e: time=36ms Ping statistics for 2a00:1450:400f:80b::200e: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 25ms, Maximum = 39ms, Average = 34ms Pinging python.org [45.55.99.72] with 32 bytes of data: Reply from 45.55.99.72: bytes=32 time=117ms TTL=51 Reply from 45.55.99.72: bytes=32 time=125ms TTL=51 Reply from 45.55.99.72: bytes=32 time=143ms TTL=51 Reply from 45.55.99.72: bytes=32 time=141ms TTL=51 Ping statistics for 45.55.99.72: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 117ms, Maximum = 143ms, Average = 131ms
I'm getting this error on run

Traceback (most recent call last):
File "C:/Users/edi_c/AppData/Local/Programs/Python/Python38-32/IPv3.py", line 7, in <module>
print(response.stdout.decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 302: invalid start byte'
You have saved file with BOM (Byte-order mark)
This is text editor that you use that dos this.
In plain notepad choose all files and utf-8,now save as ip2.txt.

Notepad ++ is okay for quick editing of files.
Under format is it give a clear message what's used when save a file Use UTF-8(without BOM(Byte-order mark)).
I saved it with NotePad++ using UTF-8 without BOM and still receiving the same error

could you send me your text file thta you use in the program to try it
(Mar-24-2020, 10:46 PM)skaailet Wrote: [ -> ]I saved it with NotePad++ using UTF-8 without BOM and still receiving the same error
That's strange you should not get that error,try save to different filename.
Here is the file ip2.txt.

If read a file like this you can see the BOM character.
with open('ipbom.txt', 'r') as f:
    print(f.read()[0:14])
Output:
google.com
Okay.
with open('ip2.txt', 'r') as f:
    print(f.read()[0:14])
Output:
google.com pyt
How can I create a script that Pings a List of IP and saves the output in other text file
I have one code but it displays an error

import subprocess


with open('IP-list.txt','r') as f, open('ip_out.txt', 'wb') as f_out:
     
        
        
    for adress in f:
        adress = adress.strip()
        response = subprocess.run(['ping', adress], capture_output=True)
        print(response.stdout.decode("utf-8"))
        f_out.write(response.stdout)
Error:
Traceback (most recent call last): File "C:\Users\edi_c\OneDrive\Escritorio\floating-ping.py", line 14, in <module> print(response.stdout.decode("utf-8")) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 311: invalid start byte
Have you tested with the file i linked to ip2.txt.
You most push the download link,not copy the text because then is not my file.