Created
October 14, 2011 19:11
-
-
Save oogali/1288028 to your computer and use it in GitHub Desktop.
Unified diff of strings in Ruby (Two nmap scans)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'diff/lcs' | |
require 'diff/lcs/string' | |
require 'diff/lcs/hunk' | |
scan = [ | |
%x[nmap -nsP -oG - 192.168.1.0/24].split(/\n/).map! { |e| e.chomp }, | |
%x[nmap -nsP -oG - 192.168.1.0/24].split(/\n/).map! { |e| e.chomp } | |
] | |
output = '' | |
oldhunk = hunk = nil | |
len = 0 | |
diffs = Diff::LCS.diff scan[0], scan[1] | |
diffs.each do |diff| | |
begin | |
hunk = Diff::LCS::Hunk.new(scan[0], scan[1], diff, 1, len) | |
len = hunk.file_length_difference | |
next unless oldhunk | |
if hunk.overlaps?(oldhunk) then | |
hunk.unshift(oldhunk) | |
else | |
output << oldhunk.diff(:unified) | |
end | |
ensure | |
oldhunk = hunk | |
output << "\n" | |
end | |
end | |
output << oldhunk.diff(:unified) | |
output << "\n" | |
puts output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment