python --- 监控客户端是否存活
发布日期:2021-05-12 23:05:38 浏览次数:10 分类:精选文章

本文共 7607 字,大约阅读时间需要 25 分钟。

���������������������

������������������������������������������SNMP������������������������������������������������������������������������������������������������������������������������SNMP���������������������������������������������������������������

������������

1. ������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

2. ������������

  • ���������������������SNMP������������������������������������������������
  • ������������������������pickle������������������������������������������������������������������������������
  • ���������������������Python���datetime������������������������������������������������
  • ���������������������������������������������������������������������������������

3. ������������

  • ������������������������������������������������������������������������������������������������������������������IP���������������������"up"���������
  • ������������������������������������������������������������������������pickle���������������������������������������������������������������������������������������������������������������
  • ������������������������������������pickle������������������������������������������������������������������������������������������������������������������������������

������������

1. ���������������

snmpclenit.py

#!/usr/bin/python-- coding:utf-8 --import sockethost, port = '192.168.72.130', 18000s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect((host, port))s.send('up')s.close()
  • script Explain���
    • ������socket������������������������
    • ������������������������������������
    • ������"up"���������
    • ���������������

2. ������������������

snmpserver.py

#!/usr/bin/python-- coding:utf-8 --import datetimeimport pickleimport socketfrom socketserver import threading# ������pickle������pfile = 'h_dic.pkl'# ���������������������������IP������with open('celie.txt') as f:    for line in f:        host = line.strip()        host_status[host] = []class MyMonitorHandler(SocketServer.BaseRequestHandler):    def handle(self):        data = self.request.recv(1024)        if not data:            return                client_ip = self.client_address[0]                # ������IP���������������IP        if client_ip == '192.168.72.130':            # ������pickle������            with open(pfile, 'w') as f:                pickle.dump(host_status, f)            print(f"���������������{pfile}")        else:            # ���������������������������            if client_ip in host_status:                # ���������������������                current_time = datetime.datetime.now()                # ������������������������������������                if host_status[client_ip]:                    last_time = host_status[client_ip][-1][0]                    time_diff = (current_time - last_time).seconds                    print(f"From {client_ip}������(t).{time_diff}���")                else:                    print(f"From {client_ip}������������������")                # ������������������                host_status[client_ip].append((current_time, data.decode()))                print(f"From {client_ip}������������������������{data.decode()}")            else:                print(f"���������IP {client_ip} ���������������������")if __name__ == "__main__":    host, port = '192.168.72.130', 18000    # ������TCPServer������������������������    server = SocketServer.ThreadingTCPServer((host, port), MyMonitorHandler)    server.serve_forever()
  • script Explain���
    • ������socketserver���������������������������������������
    • ������MyMonitorHandler������������������������
    • ���������������������IP������������������host_status���������
    • ������������IP���������������������pickle���������
    • ������������IP������������������������������������������������������������������������������������������������

3. ������������

m_handle.py

#!/usr/bin/python-- coding:utf-8 --import socketfrom datetime import datetimeimport pickleimport sysdef trigger_dump(host, port):    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    try:        s.connect((host, port))        s.send('up')        s.close()    except Exception as e:        print(f"���������������{str(e)}")trigger_dump('192.168.72.130', 18000)# ������������������celie.txt���������������������������with open('celie.txt') as f:    for line in f:        host = line.strip()        print(f"���{host}������������������������")# ������hibruccbolic.pkl������������������f = file('h_dic.pkl', 'rb')try:    host_status = pickle.load(f)except Exception as e:    print(f"������haha.pkl���������������{str(e)}")    sys.exit(1)# ���������������������������������������������������for h in host_status:    m = host_status[h]    if not m:        print(f"������������������{h}")        continue    old_time = m[-1][0]    current_time = datetime.now()    time_diff = (current_time - old_time).seconds    if time_diff > 30:        print(f"No data received from {h} for {time_diff} seconds, please check!")    else:        print(f"{h}���{time_diff}���")if __name__ == '__main__':    # ������client.py������    if __name__ == '__main__':        trigger_dump('192.168.72.130', 18000)    # ������������������    # ������������������������IP������    target_ip = sys.argv[1] if len(sys.argv) > 1 else None    if target_ip:        trigger_dump(host=target_ip, port=18000)    # ���������������    with file('h_dic.pkl', 'rb') as f:        host_status = pickle.load(f)    for h, m in host_status.items():        if not m:            continue        old_time = m[-1][0]        current_time = datetime.now()        time_diff = (current_time - old_time).seconds        if time_diff > 30:            print(f"No data received from {h} for {time_diff} seconds, please check!")        else:            print(f"{h}���{time_diff}���")
  • script Explain���
    • ������trigger_dump������������������������������������������"up"���������
    • ���������������������IP���������������������������������������������
    • ������������������������������������������������������������������������������������������������
    • ���������������������������������������

������������

1. ���������������

���������������������snmpclenit.py���������������������������������

./snmpclenit.py

���������������������������������������������������"up"���������

2. ������������������

���������������������������������������snmpserver.py������

python snmpserver.py 192.168.72.129

���������IP������������celie.txt������������IP���������������

3. ���������������������

������tail -f snmpserver.log������������������������������������������������������������������������������������

4. ������������������

���������������������m_handle.py������������������IP���������

python m_handle.py 192.168.72.129

������������������������������������������������������������������������������������������������

5. ���������������������

���������������������������������������������������������������������������������h_dic.pkl������������������������������������������������������������������snmpserver.py���������������������������������

������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������snmpserver.py���������������������������������������������������������
  • ������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������SNMP������������������������������������������������������

上一篇:python --- paramiko模块
下一篇:python-----读取Pickle文件信息计算出与当前的时间间隔

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月09日 16时19分54秒