4

I need to write a simple script which will display in real time the contents of /proc/net/xt_recent/PORTSCAN. This file is constantly changing and I want to read it in an infinite loop and display as accurately as possible the "instantaneous" state. A sample contents of my file looks like this:

src=123.45.67.89 ttl: 64 last_seen: 4298265432 oldest_pkt: 1 4298265432
src=132.45.67.89 ttl: 64 last_seen: 4298265432 oldest_pkt: 1 4298265432
src=231.45.67.89 ttl: 64 last_seen: 4298265432 oldest_pkt: 1 4298265432

Can I use the standard

line = f.readline()

Or is there some better way in to read this file. My concern is to avoid any possible inconsistencies, while reading a file which is changing (lines being added, removed, etc)

Martin Vegter
  • 136
  • 9
  • 32
  • 56
  • I suggest you to read http://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python since the problem seems to be very similar – frans Jan 22 '14 at 19:43
  • Do you want to do something with the lines or do you just want to display them? What os are you on? – RickyA Jan 22 '14 at 19:43
  • yes, I will need to process the lines. My OS is Debian, in case it is relevant. – Martin Vegter Jan 22 '14 at 19:48

2 Answers2

3

David Beazley had a great talk on Generator Tricks for Systems Programmers.

In particular, check out Processing Infinite Data section. You can use his code to follow a file in real-time like tail -f in Unix.

Jeff Bauer
  • 13,890
  • 9
  • 51
  • 73
1

Great thing about python, is the numerous packages available from people "who had the same problem as you".

For "watching" files and acting on changes, see: https://pypi.python.org/pypi/watchdog/0.7.0

For a more general "shell" wrapper solution, see: https://pypi.python.org/pypi/plumbum The latter allows you to do from plumbum.cmd import tail and then just run tail['/proc/net/xt_recent/PORTSCAN', '-1'] inside your loop as if you are in the shell.

tutuDajuju
  • 10,307
  • 6
  • 65
  • 88