def Numsum(num): sum=0 if len(num) > 0: for i in range(len(num)): if num[i] > '0' and num[i] < '9': sum=sum+int(num[i]) print "sum=%s" % sum else: sys.exit(1)
if not os.path.exists(refuse_ip): os.mknod(refuse_ip)
def nginx_protect(ip): f=open(refuse_ip,'r+') cmd='iptables -A INPUT -p tcp --dport 80 -s '+ip+' -j DROP' cmd2='at now + 1 minutes << EOF\niptables -D INPUT -p tcp --dport 80 -s '+ip+' -j DROP\nsed -i "s/'+ip+'//" /etc/nginx/refuse_nginx\nEOF' for i in f: if ip not in i: f.write(ip) os.popen(cmd) os.popen(cmd2) SendMail(ip) print("ip 限制成功")
list=[] nginx_file="/var/log/nginx/access.log" f=file(nginx_file).readlines() for i in f: nginx_ip=i.split()[0] list.append(nginx_ip) nginxip=set(list) for j in nginxip: num=list.count(j) if num > 10: nginx_protect(j) print "IP:%s NUM:%s" % (j,num)
list=[] cmd='ps aux' sum=0 status=os.popen(cmd).readlines() for i in status: a=i.split()[5] list.append(a) for i in list[1:-1]: sum=sum+int(i) print "%s:%sB" % (list[0],sum)
Com_insert="show global status like 'Com_insert';" Com_update="show global status like 'Com_update';" Com_select="show global status like 'Com_select';" Com_delete="show global status like 'Com_delete';" Open_tables="show global status like 'Open_tables';" Qcache_hits="show global status like 'Qcache_hits';"
8.自定义密码长度,生成随机密码 #!/usr/bin/python # coding=utf8 import random
list=[] def Passwd(num): for i in range(int(num)): a=random.randrange(0,int(num)) if i == a: b1=random.randint(0,9) list.append(str(b1)) else: b2=chr(random.randint(65,90)) list.append(b2) b3=''.join(list) return b3
if __name__ == '__main__': num=raw_input("生成密码的长度为:") password=Passwd(num) print password
9.python实现mysql的zabbix监控脚本
#!/usr/bin/python #coding=utf8
import MySQLdb import sys
host="localhost" user="wjq" passwd="123456" db="test" Com_insert="show global status like 'Com_insert';" Com_update="show global status like 'Com_update';" Com_select="show global status like 'Com_select';" Com_delete="show global status like 'Com_delete';" Open_tables="show global status like 'Open_tables';" Qcache_hits="show global status like 'Qcache_hits';"
def getConn(host,user,passwd,db): try: a = MySQLdb.connect(host,user,passwd,db) return a except: print "数据库连接失败"