Привет. У меня тут особый случай. Чтобы получить доступ к интернету, мне нужно войти на сайт моего интернет-провайдера, затем активировать две ссылки, которые представляют собой полосы пропускания интернета и локальной сети. Мне нужно, чтобы мой MT-роутер на базе RouterOS 2.9 делал это автоматически после истечения срока действия сессии (или по какой-либо другой причине исчезновения соединения) и менял полосу пропускания в определенное время (чтобы соответствовать тарифному плану моего провайдера). Соединение с провайдером осуществляется через ADSL. Я нашел скрипт на Ruby (ruby-lang.org), который делает всё, что мне нужно в моём случае, и, возможно, я смогу найти и PHP-скрипт. Можно ли это сделать на моем MT?
Скрипт Ruby ниже:
#!/usr/bin/env ruby
require ‘net/http’
require ‘uri’
=begin
=end
module MT
URL_MTS = “”
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:
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)
Скрипт Ruby ниже:
#!/usr/bin/env ruby
require ‘net/http’
require ‘uri’
=begin
=end
module MT
URL_MTS = “”
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:
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)
