[Tutor] Search text file, perform action if a given item found in file

Eric eric at digitalert.net
Thu Sep 9 20:26:00 CEST 2004


Thanks for the help..

I'm writing this just for Windows for the time being.

Here is the output of a netstat -an


Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING
  TCP    127.0.0.1:1028         0.0.0.0:0              LISTENING
  TCP    127.0.0.1:1051         127.0.0.1:1052         ESTABLISHED
  TCP    127.0.0.1:1052         127.0.0.1:1051         ESTABLISHED
  TCP    127.0.0.1:1116         127.0.0.1:1117         ESTABLISHED
  TCP    127.0.0.1:1117         127.0.0.1:1116         ESTABLISHED
  TCP    172.16.0.2:139         0.0.0.0:0              LISTENING
  TCP    192.168.0.250:139      0.0.0.0:0              LISTENING
  TCP    192.168.0.250:1049     192.168.0.1:22         ESTABLISHED
  TCP    192.168.0.250:1050     192.168.0.1:22         ESTABLISHED
  UDP    0.0.0.0:445            *:*
  UDP    0.0.0.0:500            *:*
  UDP    0.0.0.0:4500           *:*
  UDP    127.0.0.1:123          *:*
  UDP    127.0.0.1:1039         *:*
  UDP    127.0.0.1:1900         *:*
  UDP    172.16.0.2:123         *:*
  UDP    172.16.0.2:137         *:*
  UDP    172.16.0.2:138         *:*
  UDP    172.16.0.2:1900        *:*
  UDP    192.168.0.250:123      *:*
  UDP    192.168.0.250:137      *:*
  UDP    192.168.0.250:138      *:*
  UDP    192.168.0.250:1900     *:*



After taking your advice I have come up with this so far...


import os

o=os.popen('netstat -an')
for l in o:
        print l.split()



and this is the output...



[]
['Active', 'Connections']
[]
['Proto', 'Local', 'Address', 'Foreign', 'Address', 'State']
['TCP', '0.0.0.0:135', '0.0.0.0:0', 'LISTENING']
['TCP', '0.0.0.0:445', '0.0.0.0:0', 'LISTENING']
['TCP', '127.0.0.1:1028', '0.0.0.0:0', 'LISTENING']
['TCP', '127.0.0.1:1051', '127.0.0.1:1052', 'ESTABLISHED']
['TCP', '127.0.0.1:1052', '127.0.0.1:1051', 'ESTABLISHED']
['TCP', '172.16.0.2:139', '0.0.0.0:0', 'LISTENING']
['TCP', '192.168.0.250:139', '0.0.0.0:0', 'LISTENING']
['TCP', '192.168.0.250:1049', '192.168.0.1:22', 'ESTABLISHED']
['TCP', '192.168.0.250:1050', '192.168.0.1:22', 'ESTABLISHED']
['UDP', '0.0.0.0:445', '*:*']
['UDP', '0.0.0.0:500', '*:*']
['UDP', '0.0.0.0:4500', '*:*']
['UDP', '127.0.0.1:123', '*:*']
['UDP', '127.0.0.1:1039', '*:*']
['UDP', '127.0.0.1:1900', '*:*']
['UDP', '172.16.0.2:123', '*:*']
['UDP', '172.16.0.2:137', '*:*']
['UDP', '172.16.0.2:138', '*:*']
['UDP', '172.16.0.2:1900', '*:*']
['UDP', '192.168.0.250:123', '*:*']
['UDP', '192.168.0.250:137', '*:*']
['UDP', '192.168.0.250:138', '*:*']
['UDP', '192.168.0.250:1900', '*:*']


I'll mess around with the rest of it later tonite.


> Eric,
>
> First, I suggest you read the output of popen directly, you don't have 
> to pipe it to a temp file. If you read it by lines, then you can 
> process each line looking for the data you want.
>
> For example:
> >>> import os
> >>> o=os.popen('netstat -an')
> >>> for l in o:
> ...   print l,
> ...
>
> Active Connections
>
>   Proto  Local Address          Foreign Address        State
>   TCP    0.0.0.0:25             0.0.0.0:0              LISTENING
>   TCP    0.0.0.0:135            0.0.0.0:0              LISTENING
>   TCP    0.0.0.0:445            0.0.0.0:0              LISTENING
>   TCP    0.0.0.0:1052           0.0.0.0:0              LISTENING
> etc...
> >>> o.close()
>
> Next you probably want to split() the line to divide it into fields. 
> Then you can look at the specific fields. You might be able to use 
> endswith() or find() to search for the port; in my example 
> endswith(':25') for example would find the line with port 25. The 
> details depend on what your data looks like.
>
> Give this a try and let us know how far you get. Also it would be 
> helpful to see an example of the output of netstat on your computer, 
> it is different on Windows and MacOSX.
>
> Kent



More information about the Tutor mailing list