python脚本:
21,0-1 All
#!/usr/bin/python
# -*- coding: utf-8 -*-
#此脚本用于检测域名解析是否正常
import os
namelist = {'www.51cto.com':'218.11.0.91','www.51talk.com':'60.205.82.82'}
mail = ['bliu@test.cn','smcao@test.cn']
def check_domain():
'''使用nslook域名解析并与字典ip对比,如果解析异常发邮件给指定收件人'''
for i in namelist:
address = os.popen("nslookup %s | grep -v '#53' | awk -F':' '/^Address/{print $2}'" % i).read().strip()
if (address == namelist[i]):
pass
else:
os.system('echo "%s域名解析异常,解析的地址为%s,$(date)" >> /var/log/dns_error.log'%(i,address))
for j in mail:
os.popen("echo '%s域名解析异常,请确认!'| mail -s '%s域名解析异常' %s" %(i,i,j))
#os.popen括号中百分号后面的i,i,j分别是将python的指定变量传到shell命令中
check_domain()
小结:
上述python脚本中,需要将python中的某些变量传递给shell中引用,在此总结学习到的几种方法如下
实例1:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
var = 'test'
os.environ['var'] = str(var) #environ的键值必须是字符串
os.system('echo $var')
实例2:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
var = 'test'
os.system("echo %s" % var)
实例3:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
var = 'test'
output = os.popen("echo %s" % var)
print output.read().strip()
转载于:https://blog.51cto.com/liubin0505star/1884233