[Tutor] Search text file, perform action if a given item found
in file
Eric
eric at digitalert.net
Tue Sep 14 08:28:52 CEST 2004
Here is the result of the first part of my script. I figure it may help
out another newbie someday. Thanks for the help though.
I would not have made it this far without it.
import os
o=os.popen("netstat -an")
PORT = int(raw_input("What tcp/udp should I watch for?: "))
for l in o:
try:
if l.split()[1].endswith(str(PORT)):
print "\a\a\a\aHere is a match!"
else:
print "Nothing"
except IndexError:
print "Index Excpetion"
raw_input("Press enter to close")
Kent Johnson wrote:
> Eric,
>
> It's a difference between netstat on Windows and FreeBSD. The windows
> version includes a few header lines that don't split the way you
> expect - the resulting list doesn't have two elements:
> >>> import os
> >>> f=os.popen('netstat -an')
> >>> for l in f:
> ... print len(l.split())
> ...
> 0
> 2
> 0
> 6
> 4
> 4
> etc...
>
> You could check the length of the split before you do further
> processing, or you could catch the exception and continue the loop.
>
> Kent
More information about the Tutor
mailing list