使用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 = [‘[email protected]‘,‘[email protected]‘]

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:
                        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()
时间: 2024-12-13 23:06:10

使用python脚本监控指定域名解析的相关文章

python多线程监控指定目录

import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\\TEMP",tempfile.gettempdir()] def start_monitor(path_to_watch): h_directory = win32file.CreateFile(path_to_watch, win32con.GENERIC_READ , win32con.FIL

shell vs python脚本监控http请求

各写一个shell和python脚本来监控http请求,并在服务不可用的时候重启服务. 监控的连接为: http://192.168.1.101:5022/product http://192.168.1.101:5024/module shell脚本如下,配合crontab计划任务每一分钟执行一次检查: #!/bin/bash # This shell is used to moniter 192.168.1.101 port 5022 & 5024 date  #在crontab里用来记录l

Redis之使用python脚本监控队列长度

编辑python脚本redis_conn.py #!/usr/bin/env python #ending:utf-8 import redis def redis_conn(): pool = redis.ConnectionPool(host="192.168.56.11",port=6379,db=3,password=123456) conn = redis.Redis(connection_pool=pool) data = conn.llen("system-lo

使用python脚本监控weblogic

1.python的脚本如下: 1 ############################################################################### 2 #created on 2013-07-09 3 #author : zhaolijun 4 #used to get weblogic server runtime infomation 5 #wls_ver:weblogic 10.3.5.0 6 #########################

python脚本监控docker容器

脚本功能: 监控CPU使用率 监控内存使用状况 监控网络流量 #!/usr/bin/env  python # --*-- coding:UTF-8 --*-- import  sys import  tab import  re import  os import  time from docker  import Client import  commands keys_container_stats_list = ['blkio_stats', 'precpu_stats', 'netwo

使用Python脚本获取指定格式文件列表的方法

在Python环境下获取指定后缀文件列表的方式. 来源stackoverflow 这里简单以*.txt的作为例子. 使用glob(推荐) 1 import glob, os 2 os.chdir("/mydir") 3 for file in glob.glob("*.txt"): 4 print(file) 简单实用os.listdir 1 import os 2 for file in os.listdir("/mydir"): 3 if f

python脚本监控磁盘空间

写了个python小程序,监控磁盘空间,前面部分网上也有很多,写博客的目的是记录下来,已供自己后面使用,思路就是用pexpect 这个模块,ssh到不同的机器上,查到磁盘空间,最后对查到的结果进行处理,然后存到mysql数据中.以下是代码: #coding=utf8 import pexpect import getpass,os,sys import re,datetime, time def ssh_command (user, host, password, command): ssh_n

python脚本 监控MySQL slave 状态

#!/usr/bin/python#!gbk import osimport sys mysqlbase = '/usr/bin/mysql'host = 'ip'user = 'root'passw = 'xxxxxx'info = os.popen(mysqlbase+' -h %s -u%s -p%s -e "show slave status\G"|grep -E "Slave_IO_Running|Slave_SQL_Running|Seconds_Behind_M

Python测试进阶——(5)Python程序监控指定进程的CPU和内存利用率

参考: https://www.cnblogs.com/yueminghai/p/6632871.html https://www.cnblogs.com/xiaobeibei26/p/6481707.html 原文地址:https://www.cnblogs.com/ratels/p/11057912.html