Python script:
21,0-1 all#!/usr/bin/python# -*- coding: utf-8 -*-#此脚本用于检测域名解析是否正常import osnamelist = {' www.51cto.com ': ' 218.11.0.91 ', ' www.51talk.com ': ' 60.205.82.82 '}mail = [' [email protected] ', ' [email protected] ']def check_domain (): "' uses Nslook domain name resolution and compares with dictionary ip, if parsing exception sends an email to the specified recipient ' ' 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: for j in mail: os.popen ("echo "% s domain name resolution exception, please confirm! ' | mail -s '%s Domain name resolution exception ' %s ' % (i,i,j)) #os. Popen The i,i,j in parentheses after the percent semicolon is passed to the shell command with the specified python variable check_domain ()
Summary:
In the above Python script, some of the variables in Python need to be passed to the shell reference, and here are a few ways to learn the following
Example 1:
#!/usr/bin/python#-*-coding:utf-8-*-import osvar = ' Test ' os.environ[' var '] = str (var) #environ的键值必须是字符串os. system (' EC Ho $var ')
Example 2:
#!/usr/bin/python#-*-coding:utf-8-*-import osvar = ' test ' os.system ("echo%s"% Var)
Example 3:
#!/usr/bin/python#-*-coding:utf-8-*-import osvar = ' Test ' output = Os.popen ("echo%s"% var) print output.read (). strip ()
This article is from the "Just do myself" blog, Be sure to keep this source http://liubin0505star.blog.51cto.com/5550456/1884233
Use Python script to monitor specified domain name resolution