tplink-cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0816499e16e9eb2edaa7ea6b29ca6e9117912a05
4
+ data.tar.gz: 4282298d8167cc427c5e3477036d3988ec0f2f36
5
+ SHA512:
6
+ metadata.gz: 0b7ca8aa5f8c7c133445d87cd4e3ff70df5a8ea0bd933db0f944cff7ccbe5e1b96854dbebff75bb42349e58643039e79e4e1c3aa77cba7cdf18a0086f1cc0eec
7
+ data.tar.gz: 12510eb709b1617e486334db3ec0aebc5a20fc444c4e92782606a7d2885fac6209f08586d65d9fa7ea05639dd66dc9e4dd280a6f8176a30e0070d1a76a86c566
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
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,42 @@
1
+ # Tplink Cli
2
+
3
+ Ruby cli tool for tplink dsl modem.
4
+
5
+ Total alpha + WIP - tested on: 0.6.0 2.10 TD-W8970B
6
+
7
+ Most useful for traffic statistics for individual hosts.
8
+
9
+ ```
10
+ gem install tplink-cli
11
+ ```
12
+
13
+
14
+ ## Commands
15
+
16
+ Create config file and open in $EDITOR
17
+
18
+ ```
19
+ tplink-cli config
20
+ ```
21
+
22
+ Show DSL connection infos
23
+
24
+ ```
25
+ tplink-cli status
26
+ ```
27
+
28
+
29
+ Show traffic consumption of all connected hosts (traffic control need to be activated in Router menu (statistics))
30
+
31
+ ```
32
+ tplink-cli traffic
33
+ ```
34
+
35
+ Resolve hostnames of connected mac-addresses (saves to config file, so traffic command will display that hostnames for the next commands)
36
+
37
+ ```
38
+ tplink-cli hosts
39
+ ```
40
+
41
+ All commands use & modify ``~/.tplinkcli``. change the url / password of your router.
42
+ You can also add ``:name: "..."`` attributes to hosts, if you want to overwrite the ``host_name`` in traffic view
@@ -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-cli'
5
+
6
+ config = TplinkCli::Configuration.instance
7
+
8
+ TplinkCli::App.start
9
+
@@ -0,0 +1,11 @@
1
+ require 'tplink-cli/app'
2
+ require 'tplink-cli/version'
3
+ require 'tplink-cli/configuration'
4
+
5
+ module TplinkCli
6
+ def self.config
7
+ TplinkCli::Configuration.instance
8
+ end
9
+
10
+
11
+ end
@@ -0,0 +1,86 @@
1
+ require 'thor'
2
+ require 'tplink-cli/helpers/iniparse'
3
+ require 'tplink-cli/helpers/client'
4
+ require 'base64'
5
+
6
+ module TplinkCli
7
+ class App < Thor
8
+ package_name 'tpl'
9
+
10
+
11
+ desc "config", "Create config and edit with $EDITOR"
12
+ def config
13
+ Configuration.save
14
+ exec "$EDITOR #{ENV['HOME']}/.tplinkcli"
15
+ end
16
+
17
+
18
+ desc "status", "Status"
19
+ def status
20
+ 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"
21
+ response = Client.post '/cgi?1&5', binary: payload
22
+ Iniparse.parse(response)["[1,0,0,0,0,0]0"].each do |key,value|
23
+ puts(sprintf "%-40s %-20s", key, value)
24
+ end
25
+ Configuration.save
26
+ end
27
+ # desc 'config [show|edit|reset]', 'write/edit Configuration'
28
+ # subcommand "config", Config
29
+
30
+ # desc 'stundenzettel [process|status]', 'Redmine timetracking'
31
+ # subcommand "stundenzettel", PludoniCli::CLI::Stundenzettel
32
+
33
+ # register PludoniCli::CLI::Cap, 'cap', 'cap [*tasks]', 'Capistrano commands on live env'
34
+ # register(ProjectStatus, 'status', 'status [WORKDIR]', 'Project Status / Git updates')
35
+
36
+ # desc "dev_update [database|assets]", "Datenbankdump / Assets aus dem letzten Backup reinkopieren"
37
+ # subcommand 'dev_update', PludoniCli::CLI::DevUpdate
38
+ desc "hosts", "show hostnames that are connected"
39
+ def hosts
40
+ 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"
41
+ response = Client.post '/cgi?5', binary: payload
42
+ Iniparse.new(response).parse.to_a.tap(&:pop).each do |_,data|
43
+ Configuration.instance.hosts[data['MACAddress']] ||= {}
44
+ Configuration.instance.hosts[data['MACAddress']][:host_name] = data['hostName']
45
+ end
46
+ Configuration.instance.hosts.each do |mac, host|
47
+ puts(sprintf "%-16s %30s %30s", host[:ip_address], host[:host_name], host[:name])
48
+ end
49
+
50
+ Configuration.save
51
+ end
52
+
53
+ desc "traffic", "show traffic stats for individual hosts"
54
+ def traffic
55
+ 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"
56
+ response = Client.post '/cgi?1&5', binary: payload
57
+ structure = Iniparse.parse(response).to_a
58
+ # remove first & last
59
+ structure.shift; structure.pop
60
+
61
+ Configuration.instance.hosts ||= {}
62
+ Configuration.instance.hosts.each do |mac, host|
63
+ host[:current_bytes] = 0
64
+ end
65
+ structure.each do |_, data|
66
+ Configuration.instance.hosts[ data['macAddress'] ] ||= {}
67
+ Configuration.instance.hosts[ data['macAddress'] ][:ip_address] = num2ip(data['ipAddress'])
68
+ Configuration.instance.hosts[ data['macAddress'] ][:total_bytes] = data['totalBytes']
69
+ Configuration.instance.hosts[ data['macAddress'] ][:current_bytes] = data['currBytes']
70
+ end
71
+
72
+ length = Configuration.instance.hostname_length ||= 16
73
+ Configuration.instance.hosts.each do |mac, host|
74
+ string = (host[:name] || host[:host_name] || "").chars.take(length).join
75
+ 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)
76
+ end
77
+ Configuration.save
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,30 @@
1
+ require 'yaml'
2
+ module TplinkCli
3
+ class Configuration
4
+ attr_accessor :url
5
+ attr_accessor :password
6
+ attr_accessor :hostname_length
7
+ attr_accessor :hosts
8
+
9
+ CONFIG_FILE = "#{ENV['HOME']}/.tplinkcli"
10
+
11
+ def defaults
12
+ self.hosts ||= {}
13
+ self.hostname_length ||= 16
14
+ self.password ||= 'admin'
15
+ self.url ||= '192.168.0.1'
16
+ end
17
+
18
+ def self.instance
19
+ @instance ||= File.exists?(CONFIG_FILE) ? load_yml() : new.tap(&:defaults)
20
+ end
21
+
22
+ def self.load_yml
23
+ YAML.load_file(CONFIG_FILE)
24
+ end
25
+
26
+ def self.save
27
+ File.open(CONFIG_FILE, 'w+') { |f| f.write(instance.to_yaml) }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,53 @@
1
+ require 'tplink-cli/configuration'
2
+ # require 'curb'
3
+ require 'net/http'
4
+
5
+ module TplinkCli
6
+ class Client
7
+ def self.password
8
+ Base64.urlsafe_encode64 Configuration.instance.password
9
+ end
10
+
11
+ def self.host
12
+ Configuration.instance.url
13
+ end
14
+
15
+ def self.post(path, binary: nil)
16
+ # 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
17
+ http = Net::HTTP.new(host, 80)
18
+ request = Net::HTTP::Post.new(path)
19
+ request.body = binary
20
+ request.content_type = 'text/plain'
21
+ request.add_field 'Referer', "http://#{host}/"
22
+ request.add_field 'Cookie', "Authorization=Basic #{password}"
23
+
24
+ res = http.request(request)
25
+ if res.code.to_i > 399
26
+ $stderr.puts "Invalid status code: #{res.code}"
27
+ $stderr.puts res.body
28
+ exit 1
29
+ end
30
+ res.body
31
+ end
32
+
33
+ def self.get(path)
34
+ http = Curl.get("http://#{host}#{path}") do |curl|
35
+ curl.headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'
36
+ curl.headers['Accept-Language'] = 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,zh-CN;q=0.2'
37
+ curl.headers['Accept-Encoding'] = 'gzip, deflate, sdch'
38
+ curl.headers['Content-Type'] = 'text/plain'
39
+ curl.headers['Accept'] = '*/*'
40
+ curl.headers['Referer'] = "http://#{host}/"
41
+ curl.headers['Cookie'] = "Authorization=Basic #{password}"
42
+ curl.headers['Connection'] = 'keep-alive'
43
+ end
44
+ if http.status.to_i > 399
45
+ $stderr.puts "Invalid statuscode: #{http.status}"
46
+ $stderr.puts http.body
47
+ exit 1
48
+ end
49
+ http.body
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ module TplinkCli
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 TplinkCli
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,38 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'tplink-cli/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "tplink-cli"
7
+ spec.version = TplinkCli::VERSION
8
+ spec.authors = ["Stefan Wienert"]
9
+ spec.email = ["stwienert@gmail.com"]
10
+
11
+ spec.summary = %q{cli-tools for tplink routers development}
12
+ spec.description = %q{cli-tools for tplink routers development}
13
+ spec.homepage = ""
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 'curb'
33
+ # spec.add_dependency 'hashie'
34
+ # spec.add_dependency 'faraday'
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "bundler", "~> 1.10"
37
+ spec.add_development_dependency 'pry'
38
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tplink-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Wienert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: colored
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: curb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: cli-tools for tplink routers development
112
+ email:
113
+ - stwienert@gmail.com
114
+ executables:
115
+ - tplink-cli
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/tplink-cli
126
+ - lib/tplink-cli.rb
127
+ - lib/tplink-cli/app.rb
128
+ - lib/tplink-cli/configuration.rb
129
+ - lib/tplink-cli/helpers/client.rb
130
+ - lib/tplink-cli/helpers/iniparse.rb
131
+ - lib/tplink-cli/version.rb
132
+ - tplink-cli.gemspec
133
+ homepage: ''
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.2.2
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: cli-tools for tplink routers development
157
+ test_files: []