Hello.
I'm in a special situation here. In order to have internet access, i must login into the web site of my isp, then activate 2 links, which represent internet and intranet bandwidths.
I need my MT routerOS 2.9 to do it automatically when login expires (or for some reason connection disappears), and to change connection bandwidth at particular time (which confirm to the pay plan of ISP).
The connection to ISP is made over ADSL.
I have found a script in Ruby (ruby-lang.org) that does all i need in my special situation, and maybe i could get my hands on a PHP script too.
Can I do it on my MT?
The ruby script is below:
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
=begin
=end
module MT
URL_MTS = "http://192.168.169.66:8080/"
MT_MDIX_SERVICE = "PrSer4"
MT_TESTNIGH_SERVICE = "testnigh"
MT_64_SERVICE = "PrSer1"
GET /serviceStart/refresh/home?service=testnigh&group=2 HTTP/1.1
GET /serviceStart/refresh/home?service=PrSer1&group=2 HTTP/1.1
class LiveCookie
attr_reader :cookies
def initialize
@cookies = Hash.new
end
def add_raw(raw_cookie)
raw_cookie =~ /(\w+)=([^|;]*)/
@cookies[$1] = $2
end
def dump
str = ""
@cookies.each { |k, v|
str += "#{k}=#{v}; "
}
str.chomp("; ")
end
alias << add_raw
end
class Service
class MTSLoginFailure < StandardError; end
def urlencode(kv)
str = ""
kv.each { |k, v|
str += "&#{k}=#{v}"
}
URI.encode(str)
end
def initialize(username, password)
@live_cookie = LiveCookie.new
@username = username
@password = password
end
def show_headers(resp)
resp.each { |k, v|
puts "#{k}: #{v}"
}
end
def check_location(resp)
resp.each { |k, v|
if (k == 'location') then
return v
end
}
nil
end
def store_cookies(resp)
resp.each { |k, v|
if k == 'set-cookie' then
v.split(/, (?=\w+=)/).each { |cookie|
@live_cookie << cookie
}
end
}
end
def init
url = URL_MTS
while (1) do
puts "trying: #{url}"
resp, rd = do_GET(url)
url = check_location(resp)
break if url.nil?
end
end
def do_POST(rel_url, form)
uri = URI.parse(URL_MTS + rel_url)
http = Net::HTTP.new(uri.host, uri.port)
req = uri.path
req += "?" + uri.query if uri.query
resp, rd = http.post(req, urlencode(form),
@live_cookie.cookies)
show_headers(resp)
end
def start_service(service)
url = URL_MTS + "serviceStart/refresh/home?service=#{service}&group=2"
do_GET(url)
end
def stop_service(service)
url = "serviceStop/refresh/home?service=#{service}&group=2"
confirm_stop = {
'confirmed' => 'true',
'submitButton' => 'OK+'
}
do_POST(url, confirm_stop)
end
def login
url = "user/refresh/home?confirmed=true&submitButton=OK+"
login_credentials = {
'username' => @username,
'password' => @password
}
do_POST(url, login_credentials)
end
def logoff
url = "accountLogoff/home"
confirm_logoff = {
'confirmed' => 'true',
'submitButton' => 'OK+'
}
do_POST(url, confirm_logoff)
end
def do_GET(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
req = uri.path
req += "?" + uri.query if uri.query
resp, rd = http.get(req, { "Cookie" => @live_cookie.dump })
show_headers(resp)
store_cookies(resp)
[ resp, rd ]
end
end
end
=begin
mts = MT::Service.new('username_xyz', 'password_xyz')
mts.init
mts.logoff; sleep 2 # FIXME:
mts.login
mts.start_service(MT::MT_MDIX_SERVICE)
mts.start_service(MT::MT_TESTNIGH_SERVICE)
=end
if $0 == FILE
username = ARGV[0]
password = ARGV[1]
mts = MT::Service.new(username, password)
mts.init
mts.logoff; sleep 2 # FIXME:
mts.login
ARGV[2...ARGV.size].each { |service|
case (service)
when "TESTNIGH"
mts.start_service(MT::MT_TESTNIGH_SERVICE)
when "MDIX"
mts.start_service(MT::MT_MDIX_SERVICE)
when "64"
mts.start_service(MT::MT_64_SERVICE)
else
puts "Undefined service #{service}..."
end
}
end
\
- so this script is used on a linux machine. As you can see, the service MT_MDIX_SERVICE has to be ALWAYS active (for intranet), and the internet services: MT_TESTNIGH_SERVICE and MT_64_SERVICE are services that control different bandwidths (correcponding to isp billing plan, MT_TESTNIGH_SERVICE is higher bandwidth connection with a low const from midnight till 8:00; then, the MT_64_SERVICE has to be activated - a low bandwidth and low cost connection).
the script can be run on linux by cron:
// at 0:05 o'clock
ruby srvdi.rb MDIX TESTNIGH
// and at 7:55
ruby srvdi.rb MDIX 64
(MDIX is intranet; TEXTNIGH - high bandwidth ; 64 - low bandwidth)