this needs to be executable
[tests.git] / system / template-qemu / iptables.py
1 #!/usr/bin/python
2
3 import sys
4 import re
5
6 def main ():
7     fin=open(sys.argv[1])
8     fou=open(sys.argv[2],"w")
9     ip=sys.argv[3]
10     
11     found=False
12     lo_matcher=re.compile("\A(?P<left>.+)\s+-i\s+lo\s+-j\s+ACCEPT")
13     ip_matcher=re.compile("--(source|destination) %s"%ip)
14     for line in fin.readlines():
15         attempt=lo_matcher.match(line)
16         if attempt:
17             fou.write(line)
18             # open-up for this IP
19             fou.write("%s --source %s -j ACCEPT\n"%(attempt.group('left'),ip))
20             fou.write("%s --destination %s -j ACCEPT\n"%(attempt.group('left'),ip))
21             found=True
22         else:
23             attempt = ip_matcher.match(line)
24             # do not rewrite old lines for this ip
25             if not attempt:
26                 fou.write(line)
27
28     fin.close()
29     fou.close()
30     if found : return 0
31     else : return 1
32
33 if __name__ == '__main__':
34     main()
35