473,790 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file compare the hard way please help

2 New Member
Hi all this is my first post and I’m sorry I’m a noob.

I’ve been working on this for a couple of days and I cant seem to get it. I’m very sure that this is probably a very simple problem but it eludes me.

I need to do this in python on a Linux box. Here is the sequence of events.

Open a tcpdump file named something like “webdump.txt”, here is a sample of that file

11:30:07.830643 00:b0:64:19:86: f0 > 01:00:0c:cc:cc: cc snap ui/C len=35
0x0000: 0100 0ccc cccc 00b0 6419 86f0 0022 aaaa ........d....". .
0x0010: 0300 000c 2004 0100 0100 0500 0002 0005 ............... .
0x0020: 0400 0300 05a5 0004 000a 00b0 6419 86f0 ............d.. .
0x0030: 0001 42dc 861d 805c 0000 1400 ..B....\....
11:30:07.830722 00:b0:64:19:86: f0 > 01:00:0c:00:00: 00 snap ui/C len=69
0x0000: 0100 0c00 0000 00b0 6419 86f0 0050 aaaa ........d....P. .
0x0010: 0300 b064 0003 0084 0000 0100 0ccc cccc ...d........... .
0x0020: 00b0 6419 86f0 0032 aaaa 0300 000c 2004 ..d....2....... .
0x0030: 0100 0100 0500 0002 0005 0400 0300 05a5 ............... .
0x0040: 0004 000a 00b0 6419 86f0 0000 0000 0000 ......d........ .
0x0050: 0000 0000 0000 0000 0000 30db e516 ..........0...

break the file up into each packet being in its own array, list, or container (for people who don’t work with tcpdump the packets start with the time stamp, and as you notice packets can be anywhere from a few lines to many lines) also removing the line end \n \t.
Next open another tcpdump file and find a match for each packet (that are now in arrays) in the first file, in the second file, if there is a match print match successful if there isn’t a match print the packet and match not found. I would have used filecmp but the information in file 1 and file 2 may be in a different order.
So far I have opened the file and put each packet into a stack. With

f = open('webdump.t xt','rd')
for line in f.read().split( '11:'):
stack = [line]
# stack.remove('\ n')
print stack
f.close()
This doesn’t take the \n \t off (which I'm not sure is absolutely important as long as i can make a match)
and it also requires me to change the code every time i run it unless i only run my dumps in the 11 hour of the day. It also removes the 11: from the packet to look something like this:

['']
['30:07.830643 00:b0:64:19:86: f0 > 01:00:0c:cc:cc: cc snap ui/C len=35\n\t0x000 0: 0100 0ccc cccc 00b0 6419 86f0 0022 aaaa ........d....". .\n\t0x0010: 0300 000c 2004 0100 0100 0500 0002 0005 ............... .\n\t0x0020: 0400 0300 05a5 0004 000a 00b0 6419 86f0 ............d.. .\n\t0x0030: 0001 42dc 861d 805c 0000 1400 ..B....\\....\n ']
['30:07.830722 00:b0:64:19:86: f0 > 01:00:0c:00:00: 00 snap ui/C len=69\n\t0x000 0: 0100 0c00 0000 00b0 6419 86f0 0050 aaaa ........d....P. .\n\t0x0010: 0300 b064 0003 0084 0000 0100 0ccc cccc ...d........... .\n\t0x0020: 00b0 6419 86f0 0032 aaaa 0300 000c 2004 ..d....2....... .\n\t0x0030: 0100 0100 0500 0002 0005 0400 0300 05a5 ............... .\n\t0x0040: 0004 000a 00b0 6419 86f0 0000 0000 0000 ......d........ .\n\t0x0050: 0000 0000 0000 0000 0000 30db e516 ..........0...\ n']
Oct 11 '06 #1
2 2319
bartonc
6,596 Recognized Expert Expert
Start by using

stack = line.split()
Oct 11 '06 #2
zmann1976
2 New Member
Figured it out with a bunch of help

Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. def getPackets(fileobj):
  4.     packets = []
  5.  
  6.     for line in fileobj:
  7.         #match the tcpdump timestamp
  8.         pattern = '[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9] '
  9.         # if line matches the pattern
  10.         if re.match(pattern, line):
  11.             # remove the timestamp
  12.             line = re.sub(pattern, '', line)
  13.             # add the line to the end of the list          
  14.             packets.append(line)
  15.         else: 
  16.             # otherwise append to the end of the last
  17.             # item in the list
  18.             try: packets[len(packets)-1] += line
  19.             # exception shouldn't occur, unless 
  20.             # the input file is bad 
  21.             except: pass
  22.     return packets
  23.  
  24. list1 = getPackets(open('file1.txt'))
  25. list2 = getPackets(open('file2.txt'))
  26.  
  27. # loop through list1, checking 
  28. # if each of its elements is in
  29. # list2
  30.  
  31. for packet in list1:
  32.     if packet in list2:
  33.         print 'Match Successful'
  34.     else:
  35.         print 'Match Unsuccessful'
  36.         print packet
Oct 12 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
1929
by: Wilsoch | last post by:
Long story short: My Access developer is letting me down. He doesn't really know VB and he can't figure out how to do what I need. Situation: Access database that will be used locally on individual user's machines. What I need: I need code to make Access look for a "security" file located on the user's hard drive. If it finds the file, it allows the user to continue
46
2544
by: dawn | last post by:
Hi all, I am now working on a C program under Unix. The requirement for the program is that: A file name is passed to program as a parameter. The program will Find files under a specified directory. The matched file must have the same content with the given file. It does not matter whether the filenames are the same. It is easy to find file that has the same name with given file, but may be hard to find the files that with the same...
7
2675
by: Drew Berkemeyer | last post by:
I've encounted a pretty strange problem and I'm not quite sure what to make of it. I have a web service that consumes an XML file as well as a few other parameters. This web service works fine if I use the web test interface. It also works fine if I call it from an ASP.NET page that has a text box where the XML is pasted and then passed on. However, I get an exception if I use an <input type="file"> control on the ASP page that allows...
9
2190
by: paczkow | last post by:
Dear Python Community, I am an engineering and I am experiencing some trouble. Having output data from other software I want to use it. To achieve this I decided to use Python since this language is the best known for me. So.. for my future Python script, the input data are in form of: 1 1233.2E-3 2123.2323 2E+2 3453.3E+1 1233.2E-3 2123.2323 2E+2 3453.3E+1
1
1456
by: Tlholo | last post by:
Here are the requirements for the project. I have also included the other project that I am working on and is also giving me some problems. i need code examples. Project 1 This program actually has 4 parts that I have to do. There is a master database and an excel file that has to be downloaded from the server once a week 1.design a piece of code that will make this program run or download the file from the server once a week, let’s...
8
2599
by: Perl Beginner | last post by:
I am new to Perl and new to this site. I have the same question that I keep seeing, but not finding an answer…why doesn’t the compare function work? I’ve been going at this for a while. My code is (I’m using ActviePerl 5.8.8 Build 822 on Microsoft WinXP): use File::Compare; if (compare("$file1" , "$file") == 0) { print REPORT_FILE "Pass - Files are the same\n"; } else {print REPORT_FILE "Fail - Files are different\n"};
26
7143
by: neha_chhatre | last post by:
can anybody tell me how to compare two float values say for example t and check are two variables declared float how to compare t and check please help me as soon as possible
6
26332
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this one table. My problem is I have two buttons I want to use this code for for the two buttons would put the data in different tables. I have tried copying and changing a few things and nothing will work for me. The code is set up in a module and then I...
2
1237
by: NEBozman | last post by:
I am writing my own script for a simple calculator that determines if a number is prime, and its prime factors. Currently my code looks like: Private Sub primebutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles primebutton.Click Dim oWrite As System.IO.StreamWriter oWrite = File.CreateText("C:\TempFactors.txt") TestLabel.Text = "" 'Determines perfect square qualifications on...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.