]> git.somenet.org - pub/jan/netsec2.git/blob - showinfo.py
GITOLITE.txt
[pub/jan/netsec2.git] / showinfo.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import sys
5 from csv import reader, Sniffer
6 from os.path import exists
7 from argparse import ArgumentParser
8
9
10 def showinfo(f_in, source_ip, destination_ip):
11     ips = dict()
12
13     with open(f_in, 'r') as f:
14         csv = reader(f, delimiter=',', quotechar='"')
15
16         # Make sure that CSV file includes a header.
17         if not Sniffer().has_header(f.read(8192)):
18             sys.stderr.write("ERROR: CSV file has no header!\n")
19             sys.exit(2)
20         f.seek(0)
21
22         # Parse header of the input file
23         header = csv.next()
24         # Find field for source IP
25         try:
26             source_field = header.index("Source")
27         except ValueError:
28             sys.stderr.write("ERROR: Cannot find column 'Source' " +
29                              "in CSV file!\n")
30             sys.stderr.write("These are the values that were found: " +
31                              "%s\n" % ", ".join(header))
32             sys.exit(2)
33
34         # Find field for destination IP
35         try:
36             dest_field = header.index("Destination")
37         except ValueError:
38             sys.stderr.write("ERROR: Cannot find column 'Destination' " +
39                              "in CSV file!\n")
40             sys.stderr.write("These are the values that were found: " +
41                              "%s\n" % ", ".join(header))
42             sys.exit(2)
43
44         # Parse CSV and add values
45         pkts = 0
46         for row in csv:
47             if source_ip:
48                 if row[source_field] != source_ip:
49                     continue
50                 pkts += 1
51                 if row[dest_field] not in ips:
52                     ips[row[dest_field]] = 1
53                 else:
54                     ips[row[dest_field]] += 1
55             if destination_ip:
56                 if row[dest_field] != destination_ip:
57                     continue
58                 pkts += 1
59                 if row[source_field] not in ips:
60                     ips[row[source_field]] = 1
61                 else:
62                     ips[row[source_field]] += 1
63
64     if len(ips) == 0:
65         print "ERROR: IP address does not occur in CSV file!"
66         return
67
68     print "Number of packets: %d" % pkts
69     print "Number of connecting partners: %d" % len(ips)
70     ip = None
71     tmp = None
72     for i in ips.iterkeys():
73         if int(ips[i]) > tmp:
74             ip = i
75             tmp = int(ips[i])
76     print "Most connections: %s" % ip
77     print "Number of packets for %s: %d" % (ip, ips[ip])
78
79
80 def _main():
81     # Argument handling
82     parser = ArgumentParser(description="Show connection information for an " +
83                                         "IP address.",
84                             epilog='Example: %s --input ' % sys.argv[0] +
85                                    'file.csv --source-ip 192.168.0.1')
86     parser.add_argument("--input", type=str, required=True,
87                         help="Input CSV file.")
88     group = parser.add_mutually_exclusive_group(required=True)
89     group.add_argument("--source-ip", type=str, help="Source IP address.")
90     group.add_argument("--destination-ip", type=str,
91                        help="Destination IP address.")
92
93     args = parser.parse_args()
94     if not exists(args.input):
95         sys.stderr.write("ERROR: Input file '%s' " % args.input +
96                          "does not exist!\n")
97         sys.exit(2)
98
99     showinfo(args.input, args.source_ip, args.destination_ip)
100
101
102 if __name__ == '__main__':
103     _main()