-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogmyip.rb
More file actions
executable file
·81 lines (70 loc) · 2.59 KB
/
logmyip.rb
File metadata and controls
executable file
·81 lines (70 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#
# Log consultant's IP during engagement
# KING SABRI | @KINGSABRI
#
require 'open-uri'
require 'fileutils'
require 'logger'
require 'libnotify'
if ARGV.size.zero?
puts "[!] #{__FILE__} < /log/directory/path/ >"
exit 0
else
Process.setproctitle("LogMyIP")
@wait = 60 * 10 # Check My IP each (default: 10 minuts)
path = File.expand_path(ARGV[0], File.dirname(__FILE__))
@log_path = "#{path}/logmyip"
FileUtils.mkdir_p(@log_path) unless Dir.exist?(@log_path)
#
#-> Loggers setup
#
# Service Logs
@logger_service = Logger.new("#{@log_path}/logmyip.log", 'daily', 30) # Daily rotation, keep 30 days rotations
@logger_service.datetime_format = '%Y-%m-%d | %H:%M:%S'
@logger_service.formatter = proc { |s, d, p, m| "[#{d} | #{s}] #{m}" }
# IP logs
@logger_ip = Logger.new("#{@log_path}/ip.log", 'daily', 30)
@logger_ip.level = Logger::INFO
@logger_ip.datetime_format = '%Y-%m-%d | %H:%M:%S'
@logger_ip.formatter = proc { |s, d, p, m| "[#{d}] #{m}" }
# Setup desktop notification
@notify = Libnotify.new(:icon_path => :"network-wired-activated", :timeout => 5, :urgency => :critical)
end
def logmyip(wait)
tries ||= 20
ips = []
begin
loop do
ip = open('https://api.ipify.org').read
@logger_service.debug("IP Check!\n")
unless ips.last == ip
@logger_ip.info("#{ip}\n")
@notify.update(:summary => "<h5>LogMyIP | IP Updated!</h5>", :body => "<strong>#{ip}</strong>")
end
ips << ip
sleep wait
end
rescue SystemExit, Interrupt
@logger_service.debug("Shutting down #{__FILE__}.\n")
@notify.update(:summary => "<h5>LogMyIP | Service</h5>", :body => "<strong>Shutting down...</strong>")
rescue SocketError => e
if (tries -= 1) > 0
@logger_service.error("#{e.message}\n")
@notify.update(:summary => "<h5>LogMyIP | Service</h5>", :body => "<strong>Connection refused!</strong>")
sleep @wait
retry
else
@logger_service.fatal("Too many connection failure | Shutting down #{__FILE__}.\n")
@notify.update(:summary => "<h5>LogMyIP | Service</h5>", :body => "<strong>Connection refused!<br>Shutting down</strong>")
exit 0
end
rescue IOError => e
@logger_service.error("#{e.message}\n")
@notify.update(:summary => "<h5>LogMyIP | Service</h5>", :body => "<strong>IOError</strong>")
rescue Exception => e
@logger_service.fatal("#{e.message}\n")
@logger_service.fatal(e.backtrace.join("\n"))
@notify.update(:summary => "<h5>LogMyIP | Service</h5>", :body => "<strong>Unknown error</strong>")
end
end
logmyip(@wait)