tplink-admin 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a0f36209999af60a08efdb12b2ec776f28bb0a2
4
+ data.tar.gz: 373a926d6947cfb012fa06b7b4d90c5fab48b7a0
5
+ SHA512:
6
+ metadata.gz: 85057ed1b913de5c74f93f824e7f6cc3ac9bd40d68adfb8898f6a8694ea6a4c8339312083283afc35684f8ac62e9fb9252aeb77774324645ec6a52d4a9ce9dfe
7
+ data.tar.gz: 673f895a6614a721b0ed96e20067b71d44bed1402ad744f21ac933edc3f5ff151dff6726e98597a208c97163a118c69596ac6559b316c9cfabe2f6d0ad715b75
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Stefan Wienert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # Tplink Admin
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/tplink-admin.svg)](https://badge.fury.io/rb/tplink-admin)
4
+
5
+ Ruby cli tool for tplink dsl router administration.
6
+ Based on [zealot128's tplink-cli](https://github.com/zealot128/tplink-cli) project, but more focused on simple management of TpLink routers.
7
+
8
+ This project is in alpha stage - tested on: TL-WR741N
9
+
10
+ ## How to install
11
+
12
+ ```
13
+ git clone https://github.com/muniz95/tplink-admin
14
+ cd tplink-admin && bundle install
15
+ gem build tplink-admin.gemspec && gem install tplink-admin-X.X.X.gem
16
+ ```
17
+
18
+ This gem will be avaliable at rubygems soon.
19
+
20
+ ## Commands
21
+
22
+ Create config file and open in $EDITOR
23
+
24
+ ```
25
+ tplink-admin config
26
+ ```
27
+
28
+ Reboot your router
29
+
30
+ ```
31
+ tplink-admin reboot
32
+ ```
33
+
34
+ All commands use or modify ``~/.tplinkadmin``. change the url / password of your router.
35
+
36
+ ## Other commands in progress
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require 'tplink-admin'
5
+
6
+ config = TplinkAdmin::Configuration.instance
7
+
8
+ TplinkAdmin::App.start
9
+
@@ -0,0 +1,9 @@
1
+ require 'tplink-admin/app'
2
+ require 'tplink-admin/version'
3
+ require 'tplink-admin/configuration'
4
+
5
+ module TplinkAdmin
6
+ def self.config
7
+ TplinkAdmin::Configuration.instance
8
+ end
9
+ end
@@ -0,0 +1,86 @@
1
+ require 'thor'
2
+ require 'tplink-admin/helpers/iniparse'
3
+ require 'tplink-admin/helpers/client'
4
+ require 'base64'
5
+
6
+ module TplinkAdmin
7
+ class App < Thor
8
+ package_name 'tpl'
9
+
10
+ desc "config", "Create config and edit with $EDITOR"
11
+ def config
12
+ Configuration.save
13
+ if !ENV['EDITOR'].to_s.empty? && !ENV['EDITOR'].nil?
14
+ exec "$EDITOR #{ENV['HOME']}/.tplinkadmin"
15
+ else
16
+ puts "$EDITOR is not set. Please type your editor:"
17
+ editor = STDIN.gets.chomp
18
+ exec "#{editor} #{ENV['HOME']}/.tplinkadmin"
19
+ end
20
+ end
21
+
22
+ # desc "status", "Status"
23
+ # def status
24
+ # payload = "[WAN_DSL_INTF_CFG#1,0,0,0,0,0#0,0,0,0,0,0]0,12\r\nstatus\r\nmodulationType\r\nX_TPLINK_AdslModulationCfg\r\nupstreamCurrRate\r\ndownstreamCurrRate\r\nX_TPLINK_AnnexType\r\nupstreamMaxRate\r\ndownstreamMaxRate\r\nupstreamNoiseMargin\r\ndownstreamNoiseMargin\r\nupstreamAttenuation\r\ndownstreamAttenuation\r\n[WAN_DSL_INTF_STATS_TOTAL#1,0,0,0,0,0#0,0,0,0,0,0]1,8\r\nATUCCRCErrors\r\nCRCErrors\r\nATUCFECErrors\r\nFECErrors\r\nSeverelyErroredSecs\r\nX_TPLINK_US_SeverelyErroredSecs\r\nerroredSecs\r\nX_TPLINK_US_ErroredSecs\r\n"
25
+ # response = Client.post '/cgi?1&5', binary: payload
26
+ # Iniparse.parse(response)["[1,0,0,0,0,0]0"].each do |key,value|
27
+ # puts(sprintf "%-40s %-20s", key, value)
28
+ # end
29
+ # Configuration.save
30
+ # end
31
+
32
+ # desc "hosts", "show hostnames that are connected"
33
+ # def hosts
34
+ # payload = "[LAN_HOST_ENTRY#0,0,0,0,0,0#0,0,0,0,0,0]0,4\r\nleaseTimeRemaining\r\nMACAddress\r\nhostName\r\nIPAddress\r\n"
35
+ # response = Client.post '/cgi?5', binary: payload
36
+ # Iniparse.new(response).parse.to_a.tap(&:pop).each do |_,data|
37
+ # Configuration.instance.hosts[data['MACAddress']] ||= {}
38
+ # Configuration.instance.hosts[data['MACAddress']][:host_name] = data['hostName']
39
+ # end
40
+ # Configuration.instance.hosts.each do |mac, host|
41
+ # puts(sprintf "%-16s %30s %30s", host[:ip_address], host[:host_name], host[:name])
42
+ # end
43
+
44
+ # Configuration.save
45
+ # end
46
+
47
+ # desc "traffic", "show traffic stats for individual hosts"
48
+ # def traffic
49
+ # payload = "[STAT_CFG#0,0,0,0,0,0#0,0,0,0,0,0]0,0\r\n[STAT_ENTRY#0,0,0,0,0,0#0,0,0,0,0,0]1,0\r\n"
50
+ # response = Client.post '/cgi?1&5', binary: payload
51
+ # structure = Iniparse.parse(response).to_a
52
+ # # remove first & last
53
+ # structure.shift; structure.pop
54
+
55
+ # Configuration.instance.hosts ||= {}
56
+ # Configuration.instance.hosts.each do |mac, host|
57
+ # host[:current_bytes] = 0
58
+ # end
59
+ # structure.each do |_, data|
60
+ # Configuration.instance.hosts[ data['macAddress'] ] ||= {}
61
+ # Configuration.instance.hosts[ data['macAddress'] ][:ip_address] = num2ip(data['ipAddress'])
62
+ # Configuration.instance.hosts[ data['macAddress'] ][:total_bytes] = data['totalBytes']
63
+ # Configuration.instance.hosts[ data['macAddress'] ][:current_bytes] = data['currBytes']
64
+ # end
65
+
66
+ # length = Configuration.instance.hostname_length ||= 16
67
+ # Configuration.instance.hosts.each do |mac, host|
68
+ # string = (host[:name] || host[:host_name] || "").chars.take(length).join
69
+ # puts(sprintf "%-16s %#{length}s %8.1f kb/10s %6.1f kb/s", host[:ip_address], string, host[:current_bytes] / 1024, host[:current_bytes] / 10024)
70
+ # end
71
+ # Configuration.save
72
+ # end
73
+
74
+ desc "reboot", "Reinicia o modem"
75
+ def reboot
76
+ result = Client.get "SysRebootRpm.htm?Reboot=Reboot"
77
+ puts result.status
78
+ end
79
+
80
+ private
81
+ def num2ip(num)
82
+ "#{((num >> 24) + 256) % 256}.#{(num >> 16 & 0xff)}.#{(num >> 8 & 0xff)}.#{(num & 0xff)}"
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,32 @@
1
+ require 'yaml'
2
+ module TplinkAdmin
3
+ class Configuration
4
+ attr_accessor :url
5
+ attr_accessor :username
6
+ attr_accessor :password
7
+ attr_accessor :hostname_length
8
+ attr_accessor :hosts
9
+
10
+ CONFIG_FILE = "#{ENV['HOME']}/.tplinkadmin"
11
+
12
+ def defaults
13
+ self.hosts ||= {}
14
+ self.hostname_length ||= 16
15
+ self.username ||= 'admin'
16
+ self.password ||= 'admin'
17
+ self.url ||= '192.168.0.1'
18
+ end
19
+
20
+ def self.instance
21
+ @instance ||= File.exists?(CONFIG_FILE) ? load_yml() : new.tap(&:defaults)
22
+ end
23
+
24
+ def self.load_yml
25
+ YAML.load_file(CONFIG_FILE)
26
+ end
27
+
28
+ def self.save
29
+ File.open(CONFIG_FILE, 'w+') { |f| f.write(instance.to_yaml) }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,52 @@
1
+ require 'tplink-admin/configuration'
2
+ require 'net/http'
3
+
4
+ module TplinkAdmin
5
+ class Client
6
+ def self.username
7
+ Configuration.instance.username
8
+ end
9
+
10
+ def self.password
11
+ Configuration.instance.password
12
+ end
13
+
14
+ def self.host
15
+ Configuration.instance.url
16
+ end
17
+
18
+ def self.post(path, binary: nil)
19
+ # curl 'http://192.168.178.1/cgi?1&5' -H 'Referer: http://192.168.178.1/' -H 'Cookie: Authorization=Basic cmVuc2hhbnJlbmhhaQ==' --data-binary $'[STAT_CFG#0,0,0,0,0,0#0,0,0,0,0,0]0,0\r\n[STAT_ENTRY#0,0,0,0,0,0#0,0,0,0,0,0]1,0\r\n' --compressed --verbose
20
+ http = Net::HTTP.new(host, 80)
21
+ request = Net::HTTP::Post.new(path)
22
+ request.basic_auth username, password
23
+ request.body = binary
24
+ request.content_type = 'text/plain'
25
+ request.add_field 'Referer', "http://#{host}/"
26
+ request.add_field 'Cookie', "Authorization=Basic #{password}"
27
+
28
+ res = http.request(request)
29
+ if res.code.to_i > 399
30
+ $stderr.puts "Invalid status code: #{res.code}"
31
+ $stderr.puts res.body
32
+ exit 1
33
+ end
34
+ res.body
35
+ end
36
+
37
+ def self.get(path)
38
+ uri = URI("http://#{host}/userRpm/#{path}")
39
+ # Redirecionamento de portas: http://192.168.1.1/userRpm/VirtualServerRpm.htm?Port=3383&Ip=192.168.1.104&Protocol=1&State=1&Commonport=0&Changed=0&SelIndex=0&Save=Save
40
+ # Reboot: http://192.168.0.2/userRpm/SysRebootRpm.htm?Reboot=Reboot
41
+ req = Net::HTTP::Get.new(uri)
42
+ req.basic_auth username, password
43
+
44
+ res = Net::HTTP.start(uri.hostname, uri.port) {|http|
45
+ http.request(req)
46
+ }
47
+
48
+ res
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,37 @@
1
+ module TplinkAdmin
2
+ class Iniparse
3
+ def initialize(string)
4
+ @string = string
5
+ @stack = {}
6
+ end
7
+
8
+ def parse
9
+ @string.split("\n").each do |line|
10
+ case line
11
+ when /^\[/ then new_group(line)
12
+ when /(.*)=(.*)/ then add_key_value($1, $2)
13
+ else raise NotImplementedError.new(line)
14
+ end
15
+ end
16
+ @stack
17
+ end
18
+
19
+ def self.parse(string)
20
+ new(string).parse
21
+ end
22
+
23
+ private
24
+
25
+ def new_group(group)
26
+ @current_item = {}
27
+ @stack[group] = @current_item
28
+ end
29
+
30
+ def add_key_value(key, value)
31
+ if value.to_s[/^\d+$/]
32
+ value = value.to_i
33
+ end
34
+ @current_item[key] = value
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module TplinkAdmin
2
+ VERSION = '0.0.6'
3
+ end
@@ -0,0 +1,37 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'tplink-admin/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "tplink-admin"
7
+ spec.version = TplinkAdmin::VERSION
8
+ spec.authors = ["Rodrigo Muniz"]
9
+ spec.email = ["rodrigo.temiski1995@gmail.com"]
10
+
11
+ spec.summary = %q{CLI interface for TpLink router administration}
12
+ spec.description = %q{manage your TpLink router easily with this tool}
13
+ spec.homepage = "https://github.com/muniz95/tplink-admin"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "bin"
26
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ # spec.add_dependency 'activesupport', '>= 3.2.0'
30
+ # spec.add_dependency 'colored'
31
+ spec.add_dependency 'thor'
32
+ # spec.add_dependency 'hashie'
33
+ # spec.add_dependency 'faraday'
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "bundler", "~> 1.10"
36
+ spec.add_development_dependency 'pry'
37
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tplink-admin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Rodrigo Muniz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: manage your TpLink router easily with this tool
70
+ email:
71
+ - rodrigo.temiski1995@gmail.com
72
+ executables:
73
+ - tplink-admin
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/tplink-admin
84
+ - lib/tplink-admin.rb
85
+ - lib/tplink-admin/app.rb
86
+ - lib/tplink-admin/configuration.rb
87
+ - lib/tplink-admin/helpers/client.rb
88
+ - lib/tplink-admin/helpers/iniparse.rb
89
+ - lib/tplink-admin/version.rb
90
+ - tplink-admin.gemspec
91
+ homepage: https://github.com/muniz95/tplink-admin
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: CLI interface for TpLink router administration
115
+ test_files: []