|
程序主要用来指定22端口的连接地址,达到安全目的。
事前,请在iptables里添加22端口的指定地址如下:
-A INPUT -s 14.221.1.193/32 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
程序代码如下:
#!/bin/env python
#write by andy.zhang
import os
of=file('/etc/sysconfig/iptables','r')
old_ip=''
while True:
line=of.readline()
if '/32' in line and '--dport 22' in line:
''' print len(line) '''
fir=0
for i in range(len(line)):
if line[i] in '0123456789':
fir=i
break
for i in range(len(line)):
if line[i] == '/':
old_ip= line[fir:i].lstrip().rstrip()
break
if line =='' or old_ip != '':
break
of.close()
if old_ip == '':
'''print "Setting Is Erroy!Please check IPTABLES!!!" '''
exit
if os.system('dig +short yhzxq.eicp.net >/tmp/new_ip.txt') != 0:
''' print 'IP Error,Exit' '''
exit
of_new_ip=file('/tmp/new_ip.txt','r')
new_ip=of_new_ip.readline().lstrip().rstrip()
of_new_ip.close()
'''
print old_ip,'\t',new_ip,'\t',fir
'''
if old_ip != new_ip:
of=file('/etc/sysconfig/iptables','r')
new_file=file('/etc/sysconfig/new_iptables','w')
while True:
line=of.readline()
if old_ip in line:
wline=line.replace(old_ip,new_ip)
print wline,'\n'
new_file.write(wline)
else:
print line,'\n'
new_file.write(line)
if line == '':
new_file.flush()
new_file.close()
of.close()
break
os.system('rm -f /etc/sysconfig/iptables')
os.system('mv /etc/sysconfig/new_iptables /etc/sysconfig/iptables')
os.system('/etc/init.d/iptables restart')
现在问题来了,很多时候是正常的,也就是能换IP地址,或程序因为新旧IP地址一致而不用重启IPTABLES的。
但是,有时程序会把iptables换成都是一个IP地址的刷了很多进去,也即是新IP不停的重复着。
我不知哪里出问题了。现来请教各位。
|
|