Python Forum
Create a program that PING a list of IPs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a program that PING a list of IPs
#1
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)
Reply
#2
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
Reply
#3
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'
Reply
#4
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)).
Reply
#5
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
Reply
#6
(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
Reply
#7
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
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Program to find Mode of a list PythonBoy 6 1,059 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  Need some guidance on a script to ping a list of ip's cubangt 11 1,806 Aug-10-2023, 02:39 PM
Last Post: snippsat
  Delete strings from a list to create a new only number list Dvdscot 8 1,506 May-01-2023, 09:06 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,504 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  non-stop ping script kucingkembar 1 1,354 Aug-23-2022, 06:29 AM
Last Post: menator01
  how to easily create a list of already existing item CompleteNewb 15 3,517 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Win32\ping.exe windows pops up -very annoying... tester_V 9 3,181 Aug-12-2021, 06:54 AM
Last Post: tester_V
  Looking for discord bot to make loop ping for address ip tinkode 0 1,814 Jul-26-2021, 03:51 PM
Last Post: tinkode
  Create SQLite columns from a list or tuple? snakes 6 8,649 May-04-2021, 12:06 PM
Last Post: snakes
  Ping command using python 3.6.5 Martin2998 6 17,365 Apr-19-2021, 06:24 PM
Last Post: blazejwiecha

Forum Jump:

User Panel Messages

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