Class: TPLinkCommander

Inherits:
Object
  • Object
show all
Defined in:
lib/tp_link_commander.rb

Instance Method Summary collapse

Constructor Details

#initialize(local_ip, login, pass) ⇒ TPLinkCommander

Returns a new instance of TPLinkCommander.



6
7
8
9
10
# File 'lib/tp_link_commander.rb', line 6

def initialize(local_ip,,pass)
  @local_ip=local_ip
  @login=
  @pass=pass
end

Instance Method Details

#change_ipObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tp_link_commander.rb', line 41

def change_ip
  if !get_status.nil?
    ip=@wan_ip
    new_ip='0.0.0.0'
    loop do
      change_mac(@next_mac)
      sleep 15
      loop do
        get_status
        new_ip=@wan_ip
        sleep 5
        if new_ip!='0.0.0.0'
          print "[#{ip} -> #{new_ip}] "
        end
        break if new_ip!='0.0.0.0'
      end
      break if new_ip!=ip
    end
  else
    puts "Authorization Failed!"
  end
end

#change_mac(new_mac) ⇒ Object



37
38
39
# File 'lib/tp_link_commander.rb', line 37

def change_mac(new_mac)
  get("userRpm/MacCloneCfgRpm.htm?mac1=#{new_mac}&wan=1&Save=%25E5%2584%25B2%25E5%25AD%2598")
end

#get(url) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/tp_link_commander.rb', line 14

def get(url)
  url = URI("http://#{@local_ip}/#{url}")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Get.new(url)
  request["Authorization"] = get_auth
  response = http.request(request)
end

#get_authObject



11
12
13
# File 'lib/tp_link_commander.rb', line 11

def get_auth
  "Basic #{Base64.encode64("#{@login}:#{@pass}").gsub(/[^A-Za-z0-9=]/,"")}"
end

#get_statusObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tp_link_commander.rb', line 24

def get_status
  response=get("userRpm/StatusRpm.htm")
  result=response.read_body
  if result =~ /#errorbody/
    nil
  else
    params=result.gsub(/^[\S\s]+wanPara\s=\snew\sArray\(|\);[\S\s]+$/,"").split(/"?\s*,\s*"?/)
    @mac=params[1]
    @next_mac=next_hex(@mac)
    @wan_ip=params[2]
    params
  end
end

#next_hex(hex) ⇒ Object



21
22
23
# File 'lib/tp_link_commander.rb', line 21

def next_hex(hex)
  ("0x#{hex.gsub(/\-/,"")}".to_i(16)+1).to_s(16).upcase.scan(/../).join("-")
end