How to make MT to login on a web page, then ativate links

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)

Put this or similar script on some of the computer inside your network and tel it access those links frequently, for instance twice in timeout period.

well, this is what i can’t do. Lets say that i do not have access to pcs and i may not do such things on pcs, all i have is one mashine as a gateway. Plus, a machine with this script might be down when it is necessary to acomplish all of the above

A Router can’t open links for you. You must ask your ISP what to do in this situation.

I’m just using a separate machine. Anyways, Microtik rules.
Normis, Mictrotik isn’t ‘just’ a router, it’s Microtik.

this is just a login, i’m not sure how exactly to do it in mikrotik:

I did a work around for that in following way in windows box of course
but if you can do that in mikrotik it will be best.

get a network frame capture tool,
capture the exact packet that is sent from your pc to login the page
and also the packet request for that link.

save them in mikrotik box in a text file,(this is the dificult part)
open a session to destinatino and dispatch that packet content.

I’m not sure how exactly you can dispatch a packet, but it is usually
a hacker trick to spoof. if you could do that, please update the forum
and let us know how u could do it.