checkout listen port

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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Date : 2023-06-16
# @Author : jwgod
# @Version : 1.0.0
# 检测本机指定端口是否还在监听
# 运行:check_listen_port.py -p 8080

import socket
import argparse
import mail163
import local_ip as lip


def check_listen_port(port):
ip = lip.get_IP()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
r = s.connect_ex(('127.0.0.1', port))
if r != 0:
# print('%s port %s is down' % (ip, str(port)))
mail163.mail('jwgod@163.com', 'mail_aler', '%s port %s is down' % (ip, str(port)))
s.close()


def add_args():
p = argparse.ArgumentParser()
p.add_argument('-p', '--port', nargs='+', type=int, help='本机监听的端口号')
return p.parse_args()


def main():
args = add_args()
for p in args.port:
check_listen_port(p)


if __name__ == '__main__':
main()

local ip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Date : 2023-06-16
# @Author : jwgod
# @Version : 1.0.0

import socket

r_ip = '192.168.64.129' # 没有公网IP,先用内网网关对付


def get_IP():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((r_ip, 0))
ip = s.getsockname()[0]
s.close()
return ip

if __name__=='__main__':
ip=get_IP()
print(ip)

mail163

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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# python mail.py jwgod@163.com mail_aler xxxx-is-down

import smtplib
import sys
from email.mime.text import MIMEText
from email.header import Header

smtp_server = 'smtphz.qiye.163.com'
mail_user = 'jwgod@163.com'
password = 'xxxxxxxxx'


def mail(to, subject, txt):
msg = MIMEText(txt, 'html', _charset='utf-8')
msg['From'] = mail_user
msg['To'] = to
msg['Subject'] = Header(subject, 'utf-8').encode()
try:
s = smtplib.SMTP_SSL(smtp_server, port=465)
s.login(mail_user, password)
s.sendmail(mail_user, to, msg.as_string())
s.quit()
except smtplib.SMTPException as e:
print("Error: %s" % e)


def main():
if len(sys.argv) != 4:
print('$1 to_user $2 Subject $3 Content')
print(len(sys.argv))
sys.exit(1)
else:
mail(sys.argv[1], sys.argv[2], sys.argv[3])


if __name__ == '__main__':
main()