Python 3 自动化运维之dnspython dns轮询监控

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34777982/article/details/82501734
#dns轮询
import dns.resolver
import os
import http.client

#定义域名ip列表
iplist = []
#定义业务域名
domain_app ='www.baidu.com'

#域名解析函数
def get_ipList(domain=''):
	#解析A记录
	try:
		A = dns.resolver.query(domain, 'A')
	except Exception as e:
		print('dns resolver error'+str(e))
		return
	for i in A.response.answer:
		for j in i.items:
			iplist.append(j)
	return True


def checkip(ip):
	check_url = str(ip) +':80'
	get_content = ''
	#定义http连接超时5秒
	http.client.socket.setdefaulttimeout(5) 
	#创建连接对象
	conn = http.client.HTTPConnection(check_url)

	try:
		#发起url请求 添加host主机头部
		conn.request('GET', '/', headers={'Host':domain_app})
		r = conn.getresponse()
		#获取url页面前15字符
		get_content = r.read(15)
	finally:
		if get_content== '<!doctype html>':
			print(str(ip) + '[OK]')
		else:
			print(str(ip) + '[error]')



if __name__ == '__main__':
	if get_ipList(domain_app) and len(iplist)>0:
		for ip in iplist:
			checkip(ip)
	else:
		print('dns resolver error')

猜你喜欢

转载自blog.csdn.net/qq_34777982/article/details/82501734
今日推荐