finding all the packets associated with particular IP address

1 view (last 30 days)
say we have a matrix of nx4 .
like
no. src_ip dest_ip port
1 172.20.1.1 65.34.29.92 443
2 162.37.82.93 172.20.1.1 80
3. 162.37.76.29 172.20.1.2 534
4. 65.43.29.88 172.20.112.12 764
we have to find all the packets with all the corresponding column values associated with an ip , say 65.34.xx.xx .
any solutions ...please

Accepted Answer

Geoff
Geoff on 16 Apr 2012
Man, this is what AWK was built for! But never mind. To do it in MatLab, use regular expressions:
packethist = {
1 '72.20.1.1' '65.34.29.92' 443
2 '162.37.82.93' '172.20.1.1' 80
3 '162.37.76.29' '172.20.1.2' 534
4 '65.43.29.88' '172.20.112.12' 764 };
rows = find( ~cellfun(@isempty, regexp(packethist(:,2), '^65\.43\.')) );
Explanation:
I pass your second column to regexp and use the matching criteria '^65\.43\.' which looks for the text '65.43.' at the beginning of the string.
The regexp call returns a cell array containing a match list (the index where the matching text begins), or an empty cell (if no match).
I then pass that into cellfun which maps the isempty function over the regexp result array. That tells me which cells are empty, so I negate it (with ~) and run find to return the row indices.
You can then grab the filtered rows as follows:
packethist(rows, :)
Of course, you don't need the find call. That's just to get row numbers. The result of the cellfun call is a logical array, which you can also use as an index:
matches = ~cellfun(@isempty, regexp(packethist(:,2), '^65\.43\.'));
packethist(matches, :)
Hehe, I keep editing to expand this answer. One more thing... If you also want to include the destination IP, then compute the same kind of matches logical array as above for the destination column and combine them:
srcMatch = ...
destMatch = ...
matches = srcMatch | destMatch; % Either source or dest
matches = srcMatch & destMatch; % Both source and dest

More Answers (2)

Walter Roberson
Walter Roberson on 16 Apr 2012
strcmp() or ismember()

Karan
Karan on 17 Apr 2012
what ifi want to make the search string inputted by the user , in form of ip address , how do i convert it to the actual searc string in the above expression
say we want to search for 65.43.xx.xx and the user input is 65.43.21.13
then how do we make the user input to '^65\.43\.' as required in the search expressioon

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!