Python Forum
Receiving this error in my "response" and causes script to return wrong status
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Receiving this error in my "response" and causes script to return wrong status
#1
I have a script which i thought was working correctly until i purposely added some invalid IP's into my list so that i can validate the results file.
The "response" value is always this = Access denied. Option -c requires administrative privileges. and never has the true ping response. So now i have a solution that i thought was working and in fact not really working as all results returned are "Up" based on the condition.

So i have 2 questions:
#1 - what can be done to prevent that error, since i do not have admin rights on my work pc.
#2 - If there is no way around that error based on the script code below, is there any other method of pinging over 4000 IP's in a timely manner?

This script run against all 4000 ip's in under 4 minutes.. which currently is ok processing time.. IF it can be done more efficiently I'm all for improvements. I have another post about trying the icmp method, but cant seem to get that working and dont want to mess with the below yet.

The above message is returned in the "response" but still allows the script to run and return results.
response = os.popen(f"ping -c 1 {ip} ").read() using this method returns the message above and takes about 4 minutes to run thru 4000 ip's, but returns false positives.
response = os.popen(f"ping /n 1 {ip} ").read() using this method no message and takes about 21 minutes to run thru 4000 ip's but this time returns valid results.



import os
import time
from datetime import datetime

now = datetime.now() # current date and time
date_time = now.strftime("%m/%d/%Y,%H:%M:%S")

start = time.time()

# DIRECTORY WHERE ALL IP LISTS RESIDE
directory = 'Lists'

# RETURN LIST OF ALL FILES IN THE "DIRECTORY" PROVIDED ABOVE.
for entry in os.scandir(directory):
    
    # THIS WILL LOOP THROUGH ALL TEXT FILES IN DIRECTORY
    if entry.is_file() and entry.name.endswith('.txt'):
        
        # THIS WILL ONLY LOOK FOR AND RETURN FILES WITH IP_LIST IN THE NAME SINCE THOSE ARE THE ONES WITH THE SPECIFIC IP'S
        if 'ip_list' in entry.name:
            
            # FILE PATH FOR EACH FILE TO PROCESS(EX. Lists/Register_ip_list.txt)
            pt = directory + '/' + entry.name
            
            # THIS WILL PARSE OUT THE VALUE BEFORE THE FIRST '_' TO SET THE VARIABLE AND USE LATER FOR SAVING THE RESULTS.
            parts = entry.name.split('_')
            
            # OPEN EACH TEXT FILE THAT CONTAINS IPS TO PROCESS
            with open(pt) as file:
                park = file.read()
                park = park.splitlines()
                #print(" {park}  \n")
                
                # THIS IS TO CLEAR THE FILE BEFORE WRITING TO IT(WONT NEED THIS ONCE WE START INSERTING THE DATE INTO A DATABASE)
                file_to_clear = open("Lists/" + parts[0] +"_ip_output.txt",'w')
                file_to_clear.close()
                
            # PING EACH IP IN FILE LIST
            for ip in park:
                response = os.popen(f"ping -c 1 {ip} ").read()
                
                # SAVE PING STATUS INTO FILE FOR DASHBOARD (IP,STATUS,DATETIME)
                if("Request timed out." or "unreachable") in response:
                        #print(response)
                        f = open("Lists/" + parts[0] +"_ip_output.txt","a")
                        f.write(str(ip) + ',down,'+ date_time +'\n')
                        f.close() 
                else:
                        #print(response)
                        f = open("Lists/" + parts[0] +"_ip_output.txt","a")  
                        f.write(str(ip) + ',up,'+ date_time +'\n')
                        f.close() 
            # PRINT COMPLETE RESULTS TO CONSOLE WINDOW (DEBUGGING PURPOSES)
            # with open("Lists/" + parts[0] +"_ip_output.txt") as file:
            #     output = file.read()
            #     f.close()
            #     print(output)
            # THIS CLEARS THE FILE AFTER ITS WRITTEN TO THE CONSOLE
            #with open("Lists/ip_output.txt","w") as file:    
            #                pass

# CALCULATED THE RUNTIME OF THE SCRIPT (FUTURE UPDATES TO INCLUDE CAPTURING RUNTIMES IN SEPARATE FILE FOR REVIEW, TO INCLUDE FILENAME, ROW COUNT AND DURATION)
end = time.time()
print(end - start)
Reply


Messages In This Thread
Receiving this error in my "response" and causes script to return wrong status - by cubangt - Aug-11-2023, 03:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Wrong type error rowan_bradley 6 1,317 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,627 Mar-27-2023, 07:38 AM
Last Post: buran
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,646 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 1,489 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Receiving snmp traps with more than one Community String ilknurg 0 2,278 Jan-19-2022, 09:02 AM
Last Post: ilknurg
  return out of loops syntax error felixf 7 3,507 Nov-03-2020, 01:00 PM
Last Post: perfringo
  ERROR: Command errored out with exit status 1 calesii 3 7,224 Oct-23-2020, 05:39 PM
Last Post: snippsat
  SystemError: error return without exception set!!! faryad13 3 3,773 Oct-23-2020, 02:32 PM
Last Post: ATARI_LIVE
  Coding error- Not sure where I have put error markers against the code that is wrong Username9 1 1,761 Sep-28-2020, 07:57 AM
Last Post: buran
  Empty response to request causing .json() to error t4keheart 1 10,157 Jun-26-2020, 08:35 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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