一、手动监控客户端是否存活
项目思维
服务器端192.168.72.130:
root@kali:~# cd /root/python/snmp/
root@kali:~/python/snmp# ls
celie.txt h_dic.pkl m_handle.py snmpclient2.py snmpserver.py tab.py tab.pyc
root@kali:~/python/snmp# cat celie.txt
192.168.72.129 xuweibo
root@kali:~/python/snmp# cat m_handle.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
from datetime import datetime#导入时间模块
import pickle#导入pickle模块
f = file('h_dic.pkl','rb')#使用rb读取模式打开pickle文件
host_status = pickle.load(f)#使用pickle的load方式打开文件,变成字典
for h,m in host_status.items():#在字典中循环元素
if len(m) != 0:#如果时间值不为空,则进入
old_time = m[-1][0]#取时间值出来
time_diff = (datetime.now() - old_time).seconds#当前时间减去从pickle文件中读取的值,为时间差值
if time_diff > 30:#如果时间差大于30秒,则进入
print 'No data received from %s for %s ,please check!' %(h,time_diff)
else:
print h,(datetime.now() - old_time).seconds#打印主机ip,时间差值
root@kali:~/python/snmp# cat h_dic.pkl
(dp0
S'192.168.72.129'
p1
(lp2
(cdatetime
datetime
p3
(S'\x07\xe1\x08\x16\x149\x1b\x02\xd0F'
p4
tp5
Rp6
S'up'
p7
tp8
a(g3
(S'\x07\xe1\x08\x16\x149#\x03\xeag'
p9
tp10
Rp11
S'up'
p12
tp13
a(g3
(S'\x07\xe1\x08\x16\x149*\x01Fd'
p14
tp15
Rp16
S'up'
p17
tp18
a(g3
(S"\x07\xe1\x08\x16\x14:'\x06\x9di"
p19
tp20
Rp21
S'up'
p22
tp23
a(g3
(S'\x07\xe1\x08\x16\x15\x0c\x16\x00=\x9f'
p24
tp25
Rp26
S'up'
p27
tp28
a(g3
(S'\x07\xe1\x08\x16\x15\x0c\x16\te\x8c'
p29
tp30
Rp31
S'up'
p32
tp33
a(g3
(S'\x07\xe1\x08\x16\x15\r\x04\x08W\x91'
p34
tp35
Rp36
S'up'
p37
tp38
a(g3
(S'\x07\xe1\x08\x16\x16\x16\x05\r~\xcb'
p39
tp40
Rp41
S'up'
p42
tp43
a(g3
(S'\x07\xe1\x08\x16\x16\x16\r\x07D\x1d'
p44
tp45
Rp46
S'up'
p47
tp48
a(g3
(S'\x07\xe1\x08\x16\x16\x16-\t\xcea'
p49
tp50
Rp51
S'up'
p52
tp53
a(g3
(S'\x07\xe1\x08\x16\x16\x167\r\xac\x04'
p54
tp55
Rp56
S'up'
p57
tp58
a(g3
(S'\x07\xe1\x08\x16\x16\x17\x15\x0e\t\xfe'
p59
tp60
Rp61
S'up'
p62
tp63
a(g3
(S'\x07\xe1\x08\x16\x16\x17\x16\x05C\xa6'
p64
tp65
Rp66
S'up'
p67
tp68
a(g3
(S'\x07\xe1\x08\x16\x16\x17\x16\n\xd1*'
p69
tp70
Rp71
S'up'
p72
tp73
a(g3
(S'\x07\xe1\x08\x16\x16\x17\x17\x00\xfa^'
p74
tp75
Rp76
S'up'
p77
tp78
as.root@kali:~/python/snmp#
as.root@kali:~/python/snmp# ls
celie.txt h_dic.pkl m_handle.py snmpclient2.py snmpserver.py tab.py tab.pyc
root@kali:~/python/snmp# cat snmpclient2.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import socket
host,port = '192.168.72.130',18000
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#调用IPv4协议
s.connect((host,port))#连接主机与端口
s.send("up")#客户端给服务器端发送数据“up”
s.close()
root@kali:~/python/snmp# cat snmpserver.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import datetime#导入时间戳
import SocketServer
import pickle
pfile = 'h_dic.pkl'#定义pickle文件
#读取目录下的celie.txt文件
host_status = {}#新建字典,使用IP地址作为KEY值。作用是来判断每个客户端IP多久与服务器通信一次的
f = open('celie.txt')#调用策略文档,在里面的ip地址就可以通过,并发送信息
while True:
line = f.readline().split()
if len(line) == 0:break
print line[0]#打印第一个IP地址信息
host_status[line[0]] = []#给字典第一个设置为空,这样后面只要直接追加值就ok了
f.close()
class myMonitorHandler(SocketServer.BaseRequestHandler):
'''This is the Monitor server'''
def handle(self):
recv_data = self.request.recv(1024)#接收客户端数据
if self.client_address[0] == '192.168.72.130':#如果IP为本机IP地址,就重新写入pickle文件信息
f2 = file(pfile,'w')#使用pickle模块可写模式打开文件f2
pickle.dump(host_status,f2)#使用pickle带参数为字典名与文件名
f2.close()#关闭文件f2
if self.client_address[0] in host_status.keys():#如果存在字典中的ip地址信息,就返回对应客户端发送的Ip、时间戳、信息
#self.client_address为数组('192.168.72.129', 49109)的值。只要当中的IP地址,因此取self.client_address[0]
#把host_status字典中的self.client_address[0]值即IP地址值赋值有两个值,因此新建个列表,存取两个值时间戳与接收的信息
#如:{'192.168.72.129': [(datetime.datetime(2017, 8, 20, 21, 29, 59, 415054), 'up')]}
#host_status[self.client_address[0]] = [(datetime.datetime.now(),recv_data)]
#直接把元组append进字典
host_status[self.client_address[0]].append((datetime.datetime.now(),recv_data))
print 'From %s : %s %s' %(self.client_address,datetime.datetime.now(),recv_data)#打印客户端地址、操作的时间戳值与接收的数据
#print host_status
else:#不存在字典中,则如下提示信息
print "sorry, ip %s is not in the monitor list" % self.client_address[0]
#打印出192.168.72.129 [(datetime.datetime(2017, 8, 20, 22, 1, 6, 705498), 'up')]
for t,m in host_status.items():
print t,m
if __name__ == "__main__":#当自己运行时调用什么什么;当被其他程序调用时调用什么什么,如果被其他程序调用了,下面代码不执行
host,port = '',18000
server = SocketServer.ThreadingTCPServer((host,port),myMonitorHandler)#调用TCP的多线程
server.serve_forever()
root@kali:~/python/snmp#
root@kali:~/python/snmp# ls
celie.txt h_dic.pkl m_handle.py snmpclient2.py snmpserver.py tab.py tab.pyc
root@kali:~/python/snmp# cat tab.py
#pyhton startup file
import sys
import readline
import rlcompleter
import atexit
import os
#tab completion
readline.parse_and_bind('tab:complete')
#history file
histfile = os.path.join(os.environ['HOME'],'.pythonhistory')
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file,histfile)
del os, histfile, readline, rlcompleter
root@kali:~/python/snmp#
客户端192.168.72.129
root@72129clent:~# cd /root/python/snmp/
root@72129clent:~/python/snmp# ls
snmpclenit.py tab.py
root@72129clent:~/python/snmp# cat snmpclenit.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import socket
host,port = '192.168.72.130',18000
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#调用IPv4协议
s.connect((host,port))#连接主机与端口
s.send("up")#客户端给服务器端发送数据“up”
s.close()
root@72129clent:~/python/snmp# cat tab.py
#pyhton startup file
import sys
import readline
import rlcompleter
import atexit
import os
#tab completion
readline.parse_and_bind('tab:complete')
#history file
histfile = os.path.join(os.environ['HOME'],'.pythonhistory')
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file,histfile)
del os, histfile, readline, rlcompleter
root@72129clent:~/python/snmp#
客户端192.168.72.1
实际运行情况:
服务器端192.168.72.130,先运行
root@kali:~/python/snmp# clear
root@kali:~/python/snmp# ls
celie.txt h_dic.pkl m_handle.py snmpclient2.py snmpserver.py tab.py tab.pyc
root@kali:~/python/snmp# python snmpserver.py
192.168.72.129
Traceback (most recent call last):
File "snmpserver.py", line 53, in <module>
server = SocketServer.ThreadingTCPServer((host,port),myMonitorHandler)#调用TCP的多线程
File "/usr/lib/python2.7/SocketServer.py", line 419, in __init__
self.server_bind()
File "/usr/lib/python2.7/SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
root@kali:~/python/snmp# lsof -i :18000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python 22071 root 3u IPv4 65818 0t0 TCP *:18000 (LISTEN)
root@kali:~/python/snmp# kill -9 220071
-bash: kill: (220071) - No such process
root@kali:~/python/snmp# kill -9 22071
root@kali:~/python/snmp# lsof -i :18000
root@kali:~/python/snmp# python snmpserver.py
192.168.72.129
在celie.txt白名单中的客户端192.168.72.129运行情况
root@72129clent:~/python/snmp# ls
snmpclenit.py tab.py
root@72129clent:~/python/snmp# python snmpclenit.py