import
os
import
sys
import
smtplib
import
pandas as pd
import
re
import
time
from
email.mime.text
import
MIMEText
def
ping():
global
ip
ip
=
'192.168.0.0'
backinfo
=
os.system(
'ping -c 1 -w 1 %s'
%
ip)
if
backinfo:
pring(
"ip无法ping通"
)
print
(time.strftime(
'%Y-%m-%d %H:%M:%S'
,time.localtime(time.time())))
print
(
'----------------------------------------------'
);
checkcount();
else
:
print
(
'ip正常'
)
print
(time.strftime(
'%Y-%m-%d %H:%M:%S'
,time.localtime(time.time())))
print
(
'----------------------------------------------'
);
checkcountrestore();
def
goToma(notice):
msg_from
=
"111@qq.com"
pwe
=
"11111"
to
=
"111@qq.com"
subjedt
=
"这是PYthon发送的提醒邮件"
content
=
notice
msg
=
MIMEText(content)
msg[
"Subject"
]
=
subjedt
msg[
"From"
]
=
msg_from
msg[
"To"
]
=
to
try
:
ss
=
smtplib.SMTP_SSL(
"smtp.exmail.qq.com"
,
465
)
ss.login(msg_from, pwe)
ss.sendmail(msg_from, to, msg.as_string())
print
(
"发送成功!"
)
except
Exception as e:
print
(
"发送失败!详情"
,e)
def
getcount():
path
=
r
"/root/ping/count.txt"
f
=
open
(path,
"r"
, encoding
=
"utf-8"
)
str1
=
f.read()
recount
=
r
'replacecount=(.*?);'
count
=
re.findall(recount,str1)
count
=
int
(count[
0
]);
f.close()
return
count
def
checkcount():
count
=
getcount()
if
count
=
=
0
:
goToma(
'error ip:'
+
ip
+
'ping不通了'
);
stc1
=
"replacecount=0;"
stc2
=
"replacecount=1;"
writecount(stc1,stc2)
else
:
exit();
def
checkcountrestore():
count
=
getcount()
if
count
=
=
1
:
stc1
=
"replacecount=1;"
stc2
=
"replacecount=0;"
writecount(stc1,stc2)
goToma(
"success ip:"
+
ip
+
"已恢复正常"
)
else
:
exit();
def
writecount(st1,st2):
path
=
r
"/root/ping/count.txt"
f
=
open
(path,
"r"
, encoding
=
"utf-8"
)
str1
=
f.read()
str2
=
str1.replace(st1,st2)
ff
=
open
(path,
"w"
)
ff.write(str2)
ff.flush()
if
__name__
=
=
'__main__'
:
ping()