Advertisement
autumnrd

Netmonitoring

Mar 24th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.84 KB | None | 0 0
  1. #!/usr/local/bin/python2.7
  2.  
  3. from dpkt.ethernet import *
  4. from dpkt.ip import *
  5. from dpkt.tcp import *
  6. import pcap
  7. import socket
  8. import sys
  9. import curses
  10. import time
  11. import datetime
  12. import traceback
  13.  
  14. #------- init curses -------
  15. stdscr = curses.initscr()
  16. stdscr.nodelay(1)
  17. curses.noecho()
  18. window_size = stdscr.getmaxyx()
  19.  
  20. #------- var -------
  21. sorted_out = {}
  22. ip_list_current_in = {}
  23. ip_list_amount_in = {}
  24. ip_list_current_out = {}
  25. ip_list_amount_out = {}
  26. ip_list_current = {}
  27. ip_list_amount = {}
  28.  
  29. ts = ''
  30. eth_hdr = ''
  31.  
  32. sort_direct = True
  33. sort_sym = ['','']
  34. mbit = 0
  35. tmb = 0
  36. uns = 1
  37. direct_traff = 'incoming'
  38. strips_status = 'on'
  39. strips_color = 0
  40.  
  41. total_amount_all = 0
  42. total_fill_mb = 0
  43. total_fill_b = 0
  44.  
  45. start_time = datetime.datetime.now()
  46. current_unixtime = time.time()
  47. chanel_width = 209715 #10Mbit : 209715 100Mbit : 2097152 exam. 10mbit [10*1024*1024 / 50]
  48.  
  49. #------- network, list of ip', single host
  50. ip_adress = '192.168.1.'
  51. #ip_adress = '192.168.1.111, 192.168.1.112, 192.168.1.113, 192.168.1.140, 192.168.1.142'
  52.  
  53. #------- interface name for listen
  54. iface_name = ''
  55.  
  56. #------- net - for network, list - list ip's comma separated, host - for single host
  57. net_type = 'net'
  58.  
  59. try:
  60.     for pkt in pcap.pcap(iface_name):
  61.  
  62.         press_key = stdscr.getch()
  63.         if press_key == ord('q'):
  64.             curses.endwin()
  65.             sys.exit()
  66.         elif press_key == ord('m'):
  67.             if mbit == 1:
  68.                 if sort_direct == True:
  69.                     sort_sym = [' +','']
  70.                     sort_direct = False
  71.                 elif sort_direct == False:
  72.                     sort_sym = [' -','']
  73.                     sort_direct = True
  74.             elif mbit == 0:
  75.                 sort_sym = [' -','']
  76.                 sort_direct = True
  77.             mbit = 1
  78.             tmb = 0
  79.             uns = 0
  80.         elif press_key == ord('t'):
  81.             if tmb == 1:
  82.                 if sort_direct == True:
  83.                     sort_sym = ['',' +']
  84.                     sort_direct = False
  85.                 elif sort_direct == False:
  86.                     sort_sym = ['',' -']
  87.                     sort_direct = True
  88.             elif tmb == 0:
  89.                 sort_sym = ['',' -']
  90.                 sort_direct = True
  91.             tmb = 1
  92.             mbit = 0
  93.             uns = 0
  94.         elif press_key == ord('u'):
  95.             sort_sym = ['','']
  96.             uns = 1
  97.             tmb = 0
  98.             mbit = 0
  99.         elif press_key == ord('d'):
  100.             stdscr.clear()
  101.             if direct_traff == 'incoming':
  102.                 direct_traff = 'outgoing'
  103.             else:
  104.                 direct_traff = 'incoming'
  105.         elif press_key == ord('s'):
  106.             if strips_status == 'off':
  107.                 strips_status = 'on'
  108.                 strips_color = 0
  109.             else:
  110.                 strips_status = 'off'
  111.                 strips_color = 262144  #curses.A_REVERSE
  112.        
  113.         if str(pkt) != 'None':
  114.             ts = pkt[0]
  115.             eth_hdr = Ethernet(pkt[1])
  116.        
  117.             if eth_hdr.type == ETH_TYPE_IP:
  118.                 ip_hdr = eth_hdr.data
  119.  
  120.                 if ip_hdr.p == IP_PROTO_TCP or ip_hdr.p == IP_PROTO_UDP:
  121.                     tcp_hdr = ip_hdr.data
  122.                     data_len = len(eth_hdr.data) * 8
  123.                    
  124.                     ip_dst = socket.inet_ntoa(ip_hdr.dst)
  125.                     ip_src = socket.inet_ntoa(ip_hdr.src)
  126.                    
  127.                     if net_type == 'list':
  128.                         ip_dst_find = ip_adress.find(ip_dst)
  129.                         ip_src_find = ip_adress.find(ip_src)
  130.                     else:
  131.                         ip_dst_find = ip_dst.find(ip_adress)
  132.                         ip_src_find = ip_src.find(ip_adress)
  133.                    
  134.                     if ip_dst_find >= 0 and ip_src_find == -1 :
  135.                    
  136.                         if net_type == 'host':
  137.                             ip_dst = socket.inet_ntoa(ip_hdr.src)
  138.                             ip_src = socket.inet_ntoa(ip_hdr.dst)
  139.                        
  140.                         ip_find = ip_list_current_in.has_key(ip_dst)
  141.                         if ip_find == False:
  142.                             dict_tmp = {ip_dst:data_len}
  143.                             ip_list_current_in.update(dict_tmp)
  144.                             ip_list_amount_in.update(dict_tmp)
  145.                         elif ip_find == True:
  146.                             count_data = ip_list_current_in.get(ip_dst) + data_len
  147.                             dict_tmp = {ip_dst:count_data}
  148.                             ip_list_current_in.update(dict_tmp)
  149.                             count_data = ip_list_amount_in.get(ip_dst) + data_len
  150.                             dict_tmp = {ip_dst:count_data}
  151.                             ip_list_amount_in.update(dict_tmp)
  152.                             count_data = 0
  153.  
  154.                     if ip_dst_find == -1 and ip_src_find >= 0 :
  155.                    
  156.                         if net_type == 'host':
  157.                             ip_dst = socket.inet_ntoa(ip_hdr.src)
  158.                             ip_src = socket.inet_ntoa(ip_hdr.dst)
  159.                            
  160.                         ip_find = ip_list_current_out.has_key(ip_src)
  161.                         if ip_find == False:
  162.                             dict_tmp = {ip_src:data_len}
  163.                             ip_list_current_out.update(dict_tmp)
  164.                             ip_list_amount_out.update(dict_tmp)
  165.                         elif ip_find == True:
  166.                             count_data = ip_list_current_out.get(ip_src) + data_len
  167.                             dict_tmp = {ip_src:count_data}
  168.                             ip_list_current_out.update(dict_tmp)
  169.                             count_data = ip_list_amount_out.get(ip_src) + data_len
  170.                             dict_tmp = {ip_src:count_data}
  171.                             ip_list_amount_out.update(dict_tmp)
  172.                             count_data = 0
  173.  
  174.             if direct_traff == 'incoming':
  175.                 ip_list_current = ip_list_current_in
  176.                 ip_list_amount = ip_list_amount_in
  177.             elif direct_traff == 'outgoing':
  178.                 ip_list_current = ip_list_current_out
  179.                 ip_list_amount = ip_list_amount_out
  180.        
  181.             if (ts - current_unixtime) > 1:
  182.                 stdscr.addstr(0, 0, ' ip'.ljust(16, ' ') + '   filling channel'.ljust(54, ' ') + '  mbit/s' + '     total Mb')
  183.                 stdscr.addstr(1, 0, ''.ljust(93,'-'))
  184.                 i = 0
  185.  
  186.                 if mbit == 1:
  187.                     sorted_out = sorted(ip_list_current.iteritems(), key=lambda (k,v): (v,k), reverse = sort_direct)
  188.  
  189.                 if tmb == 1:
  190.                     sorted_out = sorted(ip_list_amount.iteritems(), key=lambda (k,v): (v,k), reverse = sort_direct)
  191.  
  192.                 if uns == 1:
  193.                     sorted_out = ip_list_current.iteritems()
  194.  
  195.                 for ip, channel_fill in sorted_out:
  196.                     chanel_fill_mb = round(float(ip_list_current.get(ip)) / 1000 /1000, 4)
  197.                     total_amount_mb = round(float(ip_list_amount.get(ip)) / 1024 /1024 /8, 2)
  198.                     total_amount_all = total_amount_all + total_amount_mb
  199.  
  200.                     star_count = int(ip_list_current.get(ip)/chanel_width)
  201.                     if star_count > 50:
  202.                         star_count = 50
  203.                     star_fill = ''.ljust(star_count, '>')
  204.  
  205.                     stdscr.addstr(i + 2, 0, ''.ljust(94, ' '))
  206.            
  207. #------- strips -------
  208.                     if i < (window_size[0] - 15):
  209.                         if i % 2 == 0:
  210.                             stdscr.addstr(i + 2, 0, ' ' + str(ip).ljust(16, ' ') +  ' [' + star_fill.ljust(50, ' ') + ']' +  ' [' + str(chanel_fill_mb).ljust(7, ' ') + ']  [' + str(total_amount_mb).ljust(8, ' ') + '] ', strips_color)
  211.                         elif i % 2 > 0:
  212.                             stdscr.addstr(i + 2, 0, ' ' + str(ip).ljust(16, ' ') +  ' [' + star_fill.ljust(50, ' ') + ']' + ' [' + str(chanel_fill_mb).ljust(7, ' ') + ']  [' + str(total_amount_mb).ljust(8, ' ') + '] ', curses.A_NORMAL)
  213.                         i = i + 1    
  214.  
  215.                     total_fill_mb = total_fill_mb + chanel_fill_mb
  216.                     total_fill_b = total_fill_b + ip_list_current.get(ip)
  217.  
  218.                     dict_tmp = {ip:0}
  219.                     ip_list_current.update(dict_tmp)
  220.                
  221.                 star_count = int(total_fill_b/chanel_width)
  222.                 if star_count > 50:
  223.                     star_count = 50
  224.                 star_fill = ''.ljust(star_count, '>')
  225.  
  226.                 stdscr.addstr(i + 2, 0, ''.ljust(93,'-'))
  227.                 stdscr.addstr(i + 3, 0, ' total'.ljust(16, ' ') + '  [' + star_fill.ljust(50, ' ') + ']' + ' [' + str(total_fill_mb).ljust(7, ' ') + ']  [' + str(total_amount_all).ljust(8, ' ') + '] ')
  228.  
  229.                 stdscr.addstr(i + 4, 0, ''.ljust(93,' '))
  230.                 stdscr.addstr(i + 5, 0, ''.ljust(93,'-'))
  231.                 stdscr.addstr(i + 6, 0, (' start [' + start_time.strftime("%d %B %Y %H:%M:%S") +  ']  elapsed [' + str(datetime.datetime.now() - start_time) + ']').ljust(72, ' ') + 'direction [' + direct_traff + '] ')
  232.  
  233.                 stdscr.addstr(i + 7, 0, ''.ljust(93,'-'))
  234.                 stdscr.addstr(i + 8, 0, (' [d] direction  [s] strips').ljust(93, ' '))
  235.                 stdscr.addstr(i + 9, 0, (' [m' + sort_sym[0] + '] sort by mbit/s  [t' + sort_sym[1] + '] sort by total Mb  [u] unsorted').ljust(76, ' ') + '[q] quit'.rjust(16, ' '))
  236.  
  237. #           stdscr.addstr(i+10, 0, ' eth: ' + str(len(eth_hdr.data)) + ' header: ' + str(ip_hdr.len) + '  port: ' + str(tcp_hdr.dport).ljust(93, ' '))
  238.                 total_fill_mb = 0
  239.                 total_fill_b = 0
  240.                 total_amount_all = 0
  241.                 current_unixtime = time.time()
  242. except:
  243.      curses.endwin()
  244.      traceback.print_exc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement