python腳本: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()
小結:shell
上述python腳本中,須要將python中的某些變量傳遞給shell中引用,在此總結學習到的幾種方法以下
dom
實例1:ide
#!/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:dns
#!/usr/bin/python # -*- coding: utf-8 -*- import os var = 'test' output = os.popen("echo %s" % var) print output.read().strip()