前言:
一直想写一个监控方面的脚本,然后想到了运维这方面的
后来就写了个脚本。
0x001准备:
psutil模块
0x02正文:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
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.xvn7640.cn/
http://www.zus2206.cn/
http://www.muu1638.cn/
http://www.sxw8975.cn/
http://www.tfu0259.cn/
http://www.sfi6595.cn/
http://www.jxb0956.cn/
http://www.dwk7340.cn/
http://www.zlq5247.cn/
http://www.tud8565.cn/
http://www.fsy9769.cn/
http://www.bin6562.cn/
http://www.xtz2685.cn/
http://www.wjm3484.cn/
http://www.itb0047.cn/
http://www.tuj3041.cn/
http://www.uzu8211.cn/
http://www.cte8448.cn/
http://www.ciw9360.cn/
http://www.fam2103.cn/
http://www.his6102.cn/
http://www.nrr1653.cn/
http://www.fyc7738.cn/
http://www.tqz4909.cn/
http://www.dfr2203.cn/
http://www.eno7779.cn/
http://www.ybj8747.cn/
http://www.ywb6045.cn/
http://www.xtl7183.cn/
http://www.rwi9374.cn/
http://www.gky0942.cn/
http://www.ice7855.cn/
http://www.koi7857.cn/
http://www.dmi4893.cn/
http://www.yxp3496.cn/
http://www.xkk9615.cn/
http://www.ixa0880.cn/
http://www.gfw0394.cn/
http://www.ucl3937.cn/
http://www.eqv5313.cn/
http://www.qyd5295.cn/
http://www.mau6270.cn/
http://www.ecc2991.cn/
http://www.qdj0796.cn/
http://www.sfx6922.cn/
http://www.huh0545.cn/
http://www.kgg2505.cn/
http://www.dhg3119.cn/
http://www.azd6793.cn/
http://www.vuf7734.cn/
http://www.ums9455.cn/
http://www.dli5822.cn/
http://www.rik3314.cn/
http://www.arf0717.cn/
http://www.dsx1888.cn/
http://www.dsd3012.cn/
http://www.pur5137.cn/