Skip to content

Instantly share code, notes, and snippets.

@technion
Created November 6, 2014 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technion/b762fc8e15c9c7133152 to your computer and use it in GitHub Desktop.
Save technion/b762fc8e15c9c7133152 to your computer and use it in GitHub Desktop.
Check file for changes
#!/usr/bin/env ruby
# technion@lolware.net
require 'openssl'
require 'json'
# Alter the below to contain name of file to monitor
Check_File = 'testfile'
# Call a mail function here if desired
Fail_Execute = 'ls'
def gethash
# Gets a hash of the file and turns it into JSON
fdata = File.read(Check_File)
digest = OpenSSL::Digest::SHA256.digest(fdata).unpack("H*").join
thehash = { filehash: digest }
return thehash.to_json
end
if File.exist? 'hashes.json'
# Existing hash to check
puts "Parsing file"
f = File.read('hashes.json')
j = JSON.parse(f)
current = gethash
testhash = JSON.parse(current)
if j['filehash'] != testhash['filehash']
puts "WE HAVE A DIFFERENT FILE"
update = File.open 'hashes.json', 'w+'
update.puts current
update.close
system Fail_Execute
else
puts "No changes to file found"
end
else
# Create a blank file
d = gethash
j = File.open 'hashes.json', 'w+'
j.puts d
j.close
puts "New File Created"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment