Python定期查询域名状态并完成记录的小脚本,python域名,对于一个站长来说,手里有


对于一个站长来说,手里有几个或十几个域名都是很正常的事情,想要及时统计这些域名的相关信息却是很费心费力的。一但忘记,可能就会错失一个好的网站。那如何来避免这样的问题发生呢?我利用目前正在学习的python语言完成了一个,关于Python定期查询域名状态并完成记录的小脚本。贴出源代码来和大家一起分享下。

import osimport timeimport sysdef _nscheck():a=[]a.insert(0,"--------------------------------------------------------------\n")a.insert(len(a),"opertime: "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+'\n')os.system('dig @202.106.0.20 cindamc.com.cn -t ns | grep ns >/tmp/tmpcheck')   f=open('/tmp/tmpcheck','r')b=f.readlines()for i in b[:]:if i[0]==';':continuea.insert(len(i),str(i))a.insert(len(a),"-------------------------------------------------------------\n")f.close()if len(a) == 3:return 0else:return adef _acheck():a=[]a.insert(len(a),"-------------------------------------------------------------\n")a.insert(len(a),"opertime: "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+'\n')os.system('dig @202.106.0.20 #网址 | grep #网址 >/tmp/tmpcheck')f=open('/tmp/tmpcheck','r')b=f.readlines()#print bfor i in b[:]:if i[0]==';':continuea.insert(len(a),i)a.insert(len(a),"-------------------------------------------------------------\n")f.close()if len(a) == 3:return 0else:return a#www.iplaypy.comdef _err():a=[]a.insert(len(a),"-------------------------------------------------------------\n")a.insert(len(a),"opertime: "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+'\n')a.insert(len(a),"                return  result error!!!!                     \n")a.insert(len(a),"-------------------------------------------------------------\n")return adef _result(a_ok,a_err,ns_ok,ns_err):result=open('/home/dnscheck/result.txt','w')result.write('-----------------------------------------------------------------\n')result.write('project               status                     conut\n')result.write('  A                   normal :'+str(a_ok)+'  error :'+str(a_err)+'                 '+str(a_ok+a_err)+'\n')result.write('  NS                  normal :'+str(ns_ok)+'  error :'+str(ns_err)+'                 '+str(ns_ok+ns_err)+'\n')result.write('-----------------------------------------------------------------\n')result.close()if __name__=='__main__':#init ns_ok numns_ok=0ns_err=0        #init a_ok numa_ok=0a_err=0os.system('rm -f /home/dnscheck/nscheck.txt')os.system('rm -f /home/dnscheck/acheck.txt')        os.system('rm -f /home/dnscheck/result.txt')_ns = open('/home/dnscheck/nscheck.txt','a+')_a  = open('/home/dnscheck/acheck.txt', 'a+')        try:while 1:if _nscheck() == 0:ns_err+=1_ns.writelines(_err())_ns.flush()else:ns_ok+=1_ns.writelines(_nscheck())_ns.flush()if _acheck() == 0: a_err+=1_a.writelines(_err())_a.flush()else:a_ok+=1_a.writelines(_acheck())_a.flush()                _result(a_ok,a_err,ns_ok,ns_err)time.sleep(60)except:_ns.close()_a.close()sys.exit()   

编橙之家文章,

评论关闭