利用python为运维人员写一个监控脚本
作者:东京$ 时间:2021-01-19 16:12:37
前言:
一直想写一个监控方面的脚本,然后想到了运维这方面的,后来就写了个脚本,下面话不多说了,来一起看看详细的介绍吧。
准备:
psutil模块(基本使用方法可以参考这篇文章:https://www.jb51.net/article/65044.htm)
正文:
import os
import time
import re
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import socket
import psutil
while True:
def jianca():
print('[+]Write a port to a file')
querys=os.popen('netstat -an').read()
wsd=open('netstat.txt','w')
wsd.write(querys)
wsd.close()
jianca()
def swsd():
global usd,ow
wsd=open('netstat.txt','r')
swd=wsd.read()
odf=re.findall('(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d):(3389)',swd)
usd=odf[0]
print('[+]Query the IP address of a remote connection')
df=usd[0],usd[1],usd[1],usd[3]
wdst=".".join(df)
ow=wdst+":"+usd[4]
print(usd[0],'.',usd[1],'.',usd[2],'.',usd[3]+":",usd[4])
swsd()
def ipdw():
global wdf,ip,timsd
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
dw=s.connect(('8.8.8.8',80))
ip=s.getsockname()[0]
print('[+]loacl IP:',ip)
wdf=os.popen('tasklist').read()
timsd=time.strftime('%Y-%m-%d',time.localtime(time.time()))
ipdw()
def yunwei():
global cput,cp
cput=psutil.cpu_times()
cp=psutil.disk_io_counters()
yunwei()
def stm():
serder="搜狐邮箱@sina.cn"
revw="收件箱@qq.com"
zhengwen='[+]Query the IP address of a remote connection''{}\n' \
'[+]loacl IP:{}\n' \
'[+]A program running in the background:{}\n' \
'[+]The user / system / idle time of statistical CPU:{}\n' \
'[+]Disk I/O usage{}\n' \
'[+]Last send time:{}\n' .format(ow,ip,wdf,cput,cp,timsd)
msg=MIMEText(zhengwen)
msg['From']=Header('你的搜狐邮箱@sina.cn')
msg['TO']=Header('收件箱@qq.com','utf-8')
sub="实时监控"
msg['subject']=Header(sub,'utf-8')
try:
smtp=smtplib.SMTP()
smtp.connect('smtp.sina.cn',25)
smtp.login('搜狐邮箱@sina.cn','登录密码')
smtp.sendmail(serder,revw,msg.as_string())
print('[+]发送出')
except Exception as g:
print('[-]发送失败,原因:',g)
stm()
time.sleep(3600)
实现原理:首先获取端口状态,然后写人一个txt用正则提取出想要的IP和端口。
然后获取正在后台运行的程序。在获取CPU和磁盘I/O的内存,然后通过邮箱发送‘
到指定收件箱。
运行截图:
来源:http://www.cnblogs.com/haq5201314/p/8642123.html
标签:python,运维,监控脚本
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用PyQt5中QLabel组件实现亚克力磨砂效果
2023-12-13 18:33:04
![](https://img.aspxhome.com/file/2023/5/72125_0s.jpg)
python 使用值来排序一个字典的方法
2022-02-05 00:25:05
去掉前面的0的sql语句(前导零,零前缀)
2011-09-30 11:28:19
Python Playwright的使用详解
2021-10-10 03:30:44
![](https://img.aspxhome.com/file/2023/8/81578_0s.jpg)
[关注细节的最佳方案]有效期时间格式的展现
2009-10-30 18:51:00
![](https://img.aspxhome.com/file/UploadPic/200910/30/validity1-63s.gif)
请问[\\u4E00-\\u9FA5]是什么正则表达式码
2009-07-10 13:10:00
python之如何查找多层嵌套字典的值
2021-12-05 08:57:07
Java正则表达式基础语法详解
2023-07-10 14:42:14
如何使用postman(新手入门)
2023-07-15 11:56:46
![](https://img.aspxhome.com/file/2023/5/58165_0s.jpg)
用Python编程实现语音控制电脑
2022-01-06 22:53:57
数据库主键的故事
2008-05-31 07:50:00
python中Pexpect的工作流程实例讲解
2021-05-04 15:51:28
asp截取指定英汉混合字符串_支持中文
2011-04-19 10:39:00
一篇文章教你用Python绘画一个太阳系
2022-12-16 14:43:16
![](https://img.aspxhome.com/file/2023/0/86370_0s.png)
Pandas对数值进行分箱操作的4种方法总结
2023-08-09 02:52:04
![](https://img.aspxhome.com/file/2023/2/64232_0s.png)
多个应用共存的Django配置方法
2021-06-28 03:25:37
![](https://img.aspxhome.com/file/2023/3/82593_0s.jpg)
Django实现静态文件缓存到云服务的操作方法
2023-05-26 07:52:54
asp如何在线查询本地机的文件?
2010-06-22 21:19:00
python pip源配置,pip配置文件存放位置的方法
2023-01-25 09:51:46
Django的分页器实例(paginator)
2023-06-23 03:22:35