第1章 系统基础信息详解 第二节
环境配置
1、RHEL6.4 SERVER IP:10.10.10.130
1、实用的IP地址处理模块IPY
1) 安装IPY模块
[root@python ~]# tar -zxvf IPy-0.81.tar.gz
[root@python IPy-0.81]# python setup.py install
2、通过指定网段输出该网段的IP个数及所有IP地址清单
[root@python ~]# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from IPy import IP ---载入IPY模块
>>> ip = IP('10.10.10.0/24') ---设置ip地址段
>>> print ip.len() ---输出ip的个数
256
>>> for x in ip: print(x) ---输出IP地址清单
...
10.10.10.0
10.10.10.1
10.10.10.2
10.10.10.3
10.10.10.4
10.10.10.5
10.10.10.6
10.10.10.7
10.10.10.8
10.10.10.9
...........
3、IP类几种常见方法,包括解析名称、IP类型、IP转换等。
[root@python ~]# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from IPy import IP
>>> ip = IP('10.10.10.221')
>>> ip.reverseNames() -----反向解析地址格式
['221.10.10.10.in-addr.arpa.']
>>> ip.iptype() ---私有地址网段类型
'PRIVATE'
>>> IP('112.80.248.73').iptype() ---公网地址类型
'PUBLIC'
>>> IP('112.80.248.73').int() ---将公网地址转换为整型
1884354633
>>> IP('112.80.248.73').strHex() ---将公网地址转换为十六进制
'0x7050f849'
>>> IP('112.80.248.73').strBin() ---将公网地址转换为二进制
'01110000010100001111100001001001'
>>> print (IP('0x7050f849')) ---打印输出转换后的十六进制值
112.80.248.73
4、根据IP与子网掩码产生网段格式
>>> from IPy import IP
>>> print (IP('10.10.10.0').make_net('255.255.255.0'))
10.10.10.0/24
>>> print(IP('10.10.10.0/255.255.255.0',make_net=True))
10.10.10.0/24
>>> print (IP('10.10.10.0-10.10.10.255',make_net=True))
10.10.10.0/24
5、通过strNormal指定不同wantprefixlen参数值以定制不同输出类型网段
>>> IP('10.10.10.0/24').strNormal(0)
'10.10.10.0'
>>> IP('10.10.10.0/24').strNormal(1)
'10.10.10.0/24'
>>> IP('10.10.10.0/24').strNormal(2)
'10.10.10.0/255.255.255.0'
>>> IP('10.10.10.0/24').strNormal(3)
'10.10.10.0-10.10.10.255'
注意:以上的strNormal所带的0到3的参数
6、多网络计算方法详解
>>> IP('10.10.10.0/24') < IP('10.10.0.0/24') ---判断
False
>>> '10.10.10.2' in IP('10.10.10.0/24')
---判断是否10.10.10.2是否属于10.10.10.0网段内
True
7、判断两个网段是否重叠使用ipy模块下的overlaps参数
>>> IP('10.10.10.0/24').overlaps('10.10.0.0/24') ---“0”代表不重叠
0
>>> IP('10.10.0.0/23').overlaps('10.10.1.0/24') ---“1”代表重叠
1
8、根据输入的IP或子网返回网络、子网掩码、广播、反向解析、子网数和IP类型信息
#!/usr/bin/python
from IPy import IP
ip_s = raw_input('please input an IP or net-range:') #:请你输入IP地址或者网段
ips = IP(ip_s) #:将IP(ip_s)的值赋予给IPS变量
if len(ips) > 1: #:判断是否为一个网络
print('net: %s' % ips.net()) #:是为一个网络,则输出网络地址
print('netmask: %s' % ips.netmask()) #:输出子网掩码地址
print('broadcase: %s' % ips.broadcase()) #:输出网络广播地址
print('reverse address: %s' % ips.reverseNames()[0]) #:输出地址反向解析
print('subnet: %s' % len(ips)) #:输出网络子网数
else: #:否则输出地址
print('reverse address: %s' %ips.reverseNames()[0]) #:输出IP反向解析
print('hexadecimal: %s' % ips.strHex()) #:输出十六进制地址
print('binary ip: %s' % ips.strBin()) #:输出二进制地址
print('iptype: %s' % ips.iptype())
#:输出地址类型,如:PRIVATE 、PUBLIC、LOOPBACK等
9、分别输入网段输出结果信息
[root@python 1]# python simplel.py
please input an IP or net-range: 10.10.10.130
reverse address: 130.10.10.10.in-addr.arpa.
hexadecimal: 0xa0a0a82
binary ip: 00001010000010100000101010000010
iptype: PRIVATE
10、DNS处理模块dnspython
1) 下载dnspyton
[root@python python ]# wget http://www.dnspython.org/kits/1.9.4/dnspython-1.9.4.tar.gz
--2016-01-29 09:05:16-- http://www.dnspython.org/kits/1.9.4/dnspython-1.9.4.tar.gz
Resolving www.dnspython.org... 54.231.141.18
Connecting to www.dnspython.org|54.231.141.18|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://120.52.72.51:80/www.dnspython.org/c3pr90ntcsf0/kits/1.9.4/dnspython-1.9.4.tar.gz [following]
--2016-01-29 09:05:20-- http://120.52.72.51/www.dnspython.org/c3pr90ntcsf0/kits/1.9.4/dnspython-1.9.4.tar.gz
Connecting to 120.52.72.51:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 120126 (117K) [application/x-gzip]
Saving to: “dnspython-1.9.4.tar.gz”
100%[===================================================================================================================>] 120,126 479K/s in 0.2s
2016-01-29 09:05:21 (479 KB/s) - “dnspython-1.9.4.tar.gz” saved [120126/120126]
2) 安装dnspython
[root@python python]# tar zxvf dnspython-1.9.4.tar.gz
[root@python python]# cd dnspython-1.9.4
[root@python dnspython-1.9.4]# python setup.py install
11、使用DNS轮循业务的域名进行可用性的监控
1) A记录
#!/usr/bin/python
import dns.resolver
domain = raw_input('Please input a domain: ') #请输入域名地址
A = dns.resolver.query(domain, 'A') #指定查询A记录
for i in A.response.answer: #通过遍历response.answer方法获取查询回应信息
for j in i.items: #遍历后返回信息
print j.address
输出结果:
[root@python 1]# python simplel1.py
Please input a domain: server.example.com
10.10.10.221
2) MX记录
#!/usr/bin/python
import dns.resolver
domain = raw_input('Please input a domain: ')
MX = dns.resolver.query(domain, 'MX')
for i in MX:
print 'MX preference =',i.preference
输出结果:
[root@python 1]# python simplel2.py
Please input a domain: example.com
MX preference = 10
3) NS记录
#!/usr/bin/python
import dns.resolver
domain = raw_input('Please input a domain: ')
ns = dns.resolver.query(domain, 'NS')
for i in ns.response.answer:
for j in i.items:
print j.to_text()
输出结果:
root@python 1]# python simplel3.py
Please input a domain: example.com
example.com.
4) CNAME记录
#!/usr/bin/python
import dns.resolver
domain = raw_input('Please input a domain: ')
cname = dns.resolver.query(domain, 'CNAME')
for i in cname.response.answer:
for j in i.items:
print j.to_text()
输出结果:
[root@python 1]# python simplel4.py
Please input a domain: ftp.example.com
10.10.10.221.example.com.
5) DNS域名轮循业务监控
#!/usr/bin/python
import dns.resolver
import os
import httplib
iplist[]
appdomain = 'ftp.example.com'
def get_iplist(domain=""):
try:
A = dns.resolver.query(domain, 'A')
except Exception,e:
print "dns resolver error:" +str(e)
return
for i in A.response.answer:
for j in i.items:
iplist.append(j.address)
return True
def checkip(ip):
checkurl = ip+":80"
getcontent = ""
httplib.socket.setdefaulttimeout(5)
conn=httplib.HTTPConnection(checkurl)
try:
conn.request("GET","/",headers = {"Host": appdomain})
r = conn.getcontent()
getcontent = r.read(15)
finally:
if getcontent=="<!doctype html>":
print ip + "[OK]"
else:
print ip + "[Error]"
if __name__=="__main__":
if get_iplist(appdomain) and len(iplist) > 0:
for ip in iplist
checkip(ip)
else:
print "dns resolver error."