Created
November 6, 2014 01:28
-
-
Save technion/b762fc8e15c9c7133152 to your computer and use it in GitHub Desktop.
Check file for changes
This file contains 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
#!/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