0

The below code is written in a python file.

When I run this via Shell it eliminates duplicate IP TABLE entries.

But when I run this via a cron job scheduled for every 30 seconds, it doesn't work.

  stream = os.popen("iptables-save | awk ' !x[$0]++' | iptables-restore")
Jayaram18
  • 9
  • 1
  • 8
  • Are you saving the output from the cron job? Are there any errors? Is the cron job running as `root`? – larsks May 24 '22 at 10:12
  • cron job is running as root – Jayaram18 May 24 '22 at 10:13
  • cron job basically invokes the python script and performs iptable rule addition and deletion . – Jayaram18 May 24 '22 at 10:14
  • `cron` does not have that kind of granularity. The best you can get is once per minute (and even that is unreliable). – tripleee May 24 '22 at 10:18
  • Why are you running this from Python anyway if you can get it to work in the shell? – tripleee May 24 '22 at 10:18
  • It is to automate the process of removing iptable duplicate entries. – Jayaram18 May 24 '22 at 10:23
  • try escaping that dollar sign and not using exclamation mark ::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: stream = os.popen("……. | mawk -F'^\$' '\_\_[ \$-\_ ]--==-\_' | …..") – RARE Kpop Manifesto May 24 '22 at 10:47
  • @RAREKpopManifesto I'm completely new to this. Can you please rephrase the above command ? – Jayaram18 May 24 '22 at 11:11
  • that's the same as your command : i'm checking !x[$0]++, but since sometimes the exclamation mark ( "!" ) gets whacky behavior when double quoted, this is an alternate version of the same thing. ++ or --, it's the same thing, since it only prints when zero, meaning first time. And you double performance by setting FS as "^$" instead of wasting time splitting fields you don't need. – RARE Kpop Manifesto May 24 '22 at 11:16
  • "\_" is a variable i never defined, but "-_" is forcing numeric evaluation of that variable, forcing it be defined as negative zero "-0", which is just 0, therefore, what the system sees for my code is ::::::::::::::::::::::::::::::::::::: mawk -F'^$' '__[ $0 ]-- == 0 ' – RARE Kpop Manifesto May 24 '22 at 11:19
  • stream = os.popen("iptables-save |awk ' x[\$0]++'| iptables-restore") tried this but still not working. – Jayaram18 May 24 '22 at 11:22
  • You can automate a shell script just as well. Running it under Python just wastes a process. (But I see you have a new question now; https://stackoverflow.com/questions/72373766/how-to-eliminate-duplicate-ip-table-entries-through-python-program/72373904#72373904) – tripleee May 25 '22 at 08:04
  • I _think_ fundamentally the problem here is that `os.popen` is not a good way to do this. Try `subprocess.run()` instead. But you haven't described how it doesn't work or what you expect `stream` to contain. – tripleee May 25 '22 at 08:07
  • @jayram18 : iptables-save |awk ' x[\$0]++'| iptables-restore") didn't work because you forgot to negate the truth logic. what yours end up printing are *ONLY* the ones with duplicated entries, but EVERY extra duplicated copy is also being printed - i.e. there are N copies of something, yours would print N-1 copies of it – RARE Kpop Manifesto May 27 '22 at 06:59
  • i mean don't get me wrong - that code has its unique usefulness in locating and help removing byte-identical but randomly named and randomly placed files in your folders. – RARE Kpop Manifesto May 27 '22 at 07:01

0 Answers0