internet_tracer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in internet_tracer.gemspec
4
+ gemspec
@@ -0,0 +1,18 @@
1
+ internet_tracer
2
+ ===============
3
+
4
+ Let's suppose your internet connection is very unstable.
5
+ Likely, after losing the connection you want to be notified when back online. Just run internet_tracer and do something without digress!
6
+
7
+
8
+ Usage examples
9
+ --------------
10
+
11
+ Using public some public IP resolution service: `internet-trace -u http://www.myip.ru/get_ip.php?loc=`.
12
+ By default, you will be notified when the first IP address found on a page is non-equal to `0.0.0.0`.
13
+
14
+ It can also be used with DD-WRT router status page: `internet-trace -u http://192.168.0.1 -w`.
15
+
16
+
17
+ It is very easy to customize, just use `internet-trace -h`.
18
+ Tip: I highly recommend using `--debug` option until you finished with params configuration.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'internet_tracer'
4
+ InternetTracer.run
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'internet_tracer'
4
+ InternetTracer.run
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "internet_tracer/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "internet_tracer"
7
+ s.version = InternetTracer::VERSION
8
+ s.authors = ["MOZGIII"]
9
+ s.email = ["mike-n@narod.ru"]
10
+ s.homepage = "http://github.com/MOZGIII/internet_tracer"
11
+ s.summary = %q{Get notification when your internet is back!}
12
+ s.description = %q{Notifies you when your DD-WRT router resores the internet connection.}
13
+
14
+ #s.rubyforge_project = "internet_tracer"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,118 @@
1
+ require "internet_tracer/version"
2
+ require "internet_tracer/detector"
3
+ require "internet_tracer/core_ext/string"
4
+ require "optparse"
5
+ require "ostruct"
6
+
7
+ module InternetTracer
8
+
9
+ class App
10
+
11
+ def detector
12
+ @detecor ||= Detector.new
13
+ end
14
+
15
+ def initialize
16
+ ["INT", "TERM"].each do |signame|
17
+ trap(signame) do
18
+ puts "Caught SIG#{signame}"
19
+ exit
20
+ end
21
+ end
22
+ end
23
+
24
+ def run
25
+ options = OpenStruct.new
26
+ options.verbose = false
27
+ options.notify = true
28
+ optparse = OptionParser.new do |opts|
29
+ opts.banner = "Usage: internet-trace [options]"
30
+ opts.separator "Example usage: internet-trace -l \"/#{'\\<div id="wan_ip"\\>.*?'}/i\" -u \"http://router.local/status\""
31
+
32
+ opts.separator ""
33
+ opts.separator "Regexp options:"
34
+
35
+ opts.on( '-l', '--left REGEXP', 'Allows to set left regexp', " Default: #{detector.regexp_left.inspect}" ) do |regexp|
36
+ detector.set_regexp(:left => regexp.to_regexp)
37
+ end
38
+
39
+ opts.on( '-r', '--right REGEXP', 'Allows to set right regexp', " Default: #{detector.regexp_right.inspect}" ) do |regexp|
40
+ detector.set_regexp(:right => regexp.to_regexp)
41
+ end
42
+
43
+ opts.on( '-m', '--main REGEXP', 'Allows to set main regexp (used for the new IP address value)', " Default: #{detector.regexp_main.inspect}" ) do |regexp|
44
+ detector.set_regexp(:main => regexp.to_regexp)
45
+ end
46
+
47
+ opts.separator ""
48
+ opts.separator "Specific options:"
49
+
50
+ opts.on( '-u', '--url URL', 'Specifies the checked page url', " Default: #{detector.status_url}" ) do |url|
51
+ detector.status_url = url
52
+ end
53
+
54
+ opts.on( '-p', '--period SECONDS', 'How frequently should checks happen, in seconds', " Default: #{detector.check_period}" ) do |seconds|
55
+ detector.check_period = seconds.to_i
56
+ end
57
+
58
+ opts.on( '--no-notify', 'Disable notification on success (only notifies if "notify-send" command is available)' ) do
59
+ options.notify = false
60
+ end
61
+
62
+ opts.separator ""
63
+ opts.separator "Other options:"
64
+
65
+ opts.on( '--[no-]verbose', 'Run verbosely' ) do |v|
66
+ options.verbose = v
67
+ end
68
+
69
+ opts.on( '--debug', 'Debug mode' ) do
70
+ options.verbose = true
71
+ detector.debug = true
72
+ end
73
+
74
+ opts.separator ""
75
+ opts.separator "Presets:"
76
+
77
+ opts.on( '-w', '--dd-wrt', 'Use preset for DD-WRT' ) do
78
+ detector.set_regexp(:left => /\<span id="ipinfo"\>.*?/, :right => //, :main => /[0-9]{1,3}(\.[0-9]{1,3}){3}/)
79
+ end
80
+
81
+ opts.separator ""
82
+ opts.separator "Common options:"
83
+
84
+ # No argument, shows at tail. This will print an options summary.
85
+ # Try it and see!
86
+ opts.on_tail("-h", "--help", "Show this message") do
87
+ puts opts
88
+ exit
89
+ end
90
+
91
+ # Another typical switch to print the version.
92
+ opts.on_tail("-v", "--version", "Show version") do
93
+ puts VERSION
94
+ exit
95
+ end
96
+ end
97
+ optparse.parse!
98
+
99
+ puts detector if detector.debug
100
+
101
+ ip = detector.wait_for_normal_ip do |wrong_ip|
102
+ puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} - No luck this time... (got: #{wrong_ip})" if options.verbose
103
+ end
104
+
105
+ puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} - Ok, we're happy!" if options.verbose
106
+ puts "New IP is: #{ip}"
107
+
108
+ # Using notify-send to send notification, should give no problem even without libnotify
109
+ system("notify-send", "Internet in now connected!", "Your IP is: #{ip}") if options.notify
110
+ end
111
+
112
+ end
113
+
114
+ def self.run
115
+ App.new.run
116
+ end
117
+
118
+ end
@@ -0,0 +1,8 @@
1
+ require 'yaml'
2
+ class String
3
+ def to_regexp
4
+ str = self
5
+ str = "/#{str}/" unless str =~ /\A\/.*\/.*\z/
6
+ YAML.load("!ruby/regexp #{str}")
7
+ end unless method_defined?(:to_regexp)
8
+ end
@@ -0,0 +1,69 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module InternetTracer
5
+ class Detector
6
+ attr_accessor :status_url, :regexp_main, :regexp_left, :regexp_right, :check_period, :debug
7
+
8
+ def initialize
9
+ @status_url = "http://192.168.0.1/"
10
+ @check_period = 20
11
+
12
+ # Default is IP address regexp
13
+ @regexp_main = /[0-9]{1,3}(\.[0-9]{1,3}){3}/
14
+
15
+ # Empty regexps to catch the first IP address on the page by default
16
+ @regexp_left = //
17
+ @regexp_right = //
18
+
19
+ @debug = false
20
+ end
21
+
22
+ def wait_for_normal_ip(check_period = @check_period)
23
+ loop do
24
+ begin
25
+ ip = get_real_ip
26
+ rescue
27
+ puts "Rescued from: #{$!}" if @debug
28
+ end
29
+
30
+ break ip if ip && ip != "0.0.0.0"
31
+ yield(ip) if block_given?
32
+ sleep check_period
33
+ end
34
+ end
35
+
36
+ def get_real_ip
37
+ match = Net::HTTP.get(URI.parse(@status_url)).match(search_regexp)
38
+ raise "IP address was not found on the page!" unless match
39
+ match[:value]
40
+ end
41
+
42
+ def set_regexp(values = {})
43
+ values = { :main => values } if values.kind_of?(Regexp) || values.kind_of?(String)
44
+ @regexp_left = values[:left] if values[:left]
45
+ @regexp_main = values[:main] if values[:main]
46
+ @regexp_right = values[:right] if values[:right]
47
+ self
48
+ end
49
+
50
+
51
+ def search_regexp
52
+ /#{@regexp_left}(?<value>#{@regexp_main})#{@regexp_right}/
53
+ end
54
+
55
+ def to_s
56
+ puts "#<#{self.class.to_s}:"
57
+ puts " URL: #{@status_url}"
58
+ puts " Check period: #{@check_period}"
59
+ puts
60
+ puts " Main regexp: #{@regexp_main.inspect}"
61
+ puts " Left regexp: #{@regexp_left.inspect}"
62
+ puts " Right regexp: #{@regexp_right.inspect}"
63
+ puts
64
+ puts " Resulting regexp: #{search_regexp.inspect}"
65
+ puts ">"
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,3 @@
1
+ module InternetTracer
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: internet_tracer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - MOZGIII
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-01 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Notifies you when your DD-WRT router resores the internet connection.
15
+ email:
16
+ - mike-n@narod.ru
17
+ executables:
18
+ - internet-trace
19
+ - internet-tracer
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitignore
24
+ - Gemfile
25
+ - README.md
26
+ - Rakefile
27
+ - bin/internet-trace
28
+ - bin/internet-tracer
29
+ - internet_tracer.gemspec
30
+ - lib/internet_tracer.rb
31
+ - lib/internet_tracer/core_ext/string.rb
32
+ - lib/internet_tracer/detector.rb
33
+ - lib/internet_tracer/version.rb
34
+ homepage: http://github.com/MOZGIII/internet_tracer
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.10
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Get notification when your internet is back!
58
+ test_files: []