Use Python script to monitor specified domain name resolution

Source: Internet
Author: User
Tags python script

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.