Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script hangs, no errors

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
0
0
US
I got 3 scripts.
1st script scans subnets and prints out Active and not Active IPs.
2d script opens file with Active IPs and finds corresponding Active Hosts names.
3d script opens file with the Active Hosts names and pulls out some stuff from it.

When I ran each script separately, I have no errors or problems but when put all three of them in to one script it hangs on the second script with no errors and never starts third part of the script.
############################################################################################
[sub]import socket
import sys

pfile = open('C:/Python_TEST/IP_Scan/Lists/IP_Active1.txt')
h_active = open("C:/Python_TEST/IP_Scan/Lists/IP_Host_Active.txt","w")

sys.stdout = h_active

while True:
IP = pfile.readline()
try:
host = socket.gethostbyaddr(IP.rstrip())
#(IP.rstrip())
#host = socket.gethostbyaddr(IP.rstrip())
print (IP,file=h_active)
print(IP,host)
except Exception as e:
print(IP,"NULL")

pfile.close()
h_active.close()[/sub]
 
Thank you for the reply.
I just started learning Python I found this snippet and really do not fully understand.
I understand "try:" and "except:" will allow the code run even if an error,I'm confused about "while True:"
Do you know how to fix this?
Thank you.
 
Finally I got it.
Wanted to share with you good people this simple snippet.
I just added "if" loop that will break at the EOF.

[sup]pflie = open('C:/Scripts/somefile_toread.txt')
h_active = open("C:/Scripts/somefile_towrite.txt","w")

sys.stdout = h_active

while True:
IP = pflie.readline()
if (""==IP) :
print ("EOF")
break
else :
try:
host = socket.gethostbyaddr(IP.rstrip())
print(IP,host)

h_active.write("IP,host")
except Exception as e:

print(IP,Exception)

pflie.close()
h_active.close()[/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top