Below is a simple PHP API for a simple hotspot add/edit/disable/enable user. It can be modified to add alot more functionality:
<?php
/*
* RouterOS API
* Based on the code of SpectatorCN at http://forum.mikrotik.com/t/remote-control-of-traffic-shaping-over-ssh-telnet/29513/1
* Modified by Ali Damji
* Free to modify, distribute, do whatever.
*
*/
# Basic Functions
function routeros_connect($host, $username, $password) {
global $fp;
$header1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x41).chr(0x4E).chr(0x53).chr(0x49).chr(0xFF).chr(0xF0);
$header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
$fp=fsockopen($host,23);
fputs($fp,$header1);
usleep(125000);
fputs($fp,$header2);
usleep(125000);
write_to_telnet($fp,$username."+ct");
write_to_telnet($fp,$password);
read_from_telnet($fp);
}
function routeros_cmd($command) {
global $fp;
write_to_telnet($fp,"$command");
$rez = read_from_telnet($fp);
echo $rez;
}
# API Related
function ros_new_hotspot_user($username,$password,$email,$limit_kb,$limit_up) {
routeros_cmd("/ip hotspot user add name=$username password=$password email=$email limit-bytes-total=$limit_kb limit-uptime=$limit_up");
}
function ros_edit_hotspot_user($username,$password,$email,$limit_kb,$limit_up) {
routeros_cmd("/ip hotspot user set $username password=$password email=$email limit-bytes-total=$limit_kb limit-uptime=$limit_up");
}
function ros_disable_hotspot_user($username) {
routeros_cmd("/ip hotspot user disable $username");
}
function ros_enable_hotspot_user($username) {
routeros_cmd("/ip hotspot user enable $username");
}
function ros_hotspot_user_detail($username="") {
if ($username) routeros_cmd("/ip hotspot user print detail where name=$username");
if (!$username) routeros_cmd("/ip hotspot user print detail");
}
# Telnet Related
function write_to_telnet($fp, $text){
fputs($fp,$text."\r\n");
usleep(1250);
return true;
}
function read_from_telnet($fp){
$output = "";
$count = 0;
do{
$char =fread($fp, 1);
$output .= $char;
if($char==">") $count++;
if($count==1) break;
} while(1==1);
$output=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$output);
$o=explode("\n",$output);
for($i=1;$i<=count($o)-2;$i++) $op.=$o[$i]."\n";
return $op;
}
?>
thaak
September 16, 2009, 1:19pm
2
Some changes to allow for multiline scripts and brackets.
Also an example of how to send a script to multiple servers
<?php
header("Content-Type: text/plain");
/*
* RouterOS API
* Based on the code of SpectatorCN at http://forum.mikrotik.com/t/remote-control-of-traffic-shaping-over-ssh-telnet/29513/1
* Modified by Ali Damji
* Free to modify, distribute, do whatever.
*
*/
define(TimeOut,125000);
# Basic Functions
function routeros_connect($host, $username, $password) {
global $fp;
$header1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x41).chr(0x4E).chr(0x53).chr(0x49).chr(0xFF).chr(0xF0);
$header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
$fp=fsockopen($host,23);
fputs($fp,$header1);
usleep(125000);
fputs($fp,$header2);
usleep(125000);
write_to_telnet($fp,$username."+ct");
write_to_telnet($fp,$password);
read_from_telnet($fp);
}
function routeros_cmd($command) {
global $fp;
//$command = str_replace(";\n",';',$command);
//echo $command."\n";
$commands = explode("\n",$command);
reset($commands);
foreach ($commands as $cmd)
{
echo $cmd."\n";
flush();
write_to_telnet($fp,trim($cmd));
echo read_from_telnet($fp)."\n";
flush();
}
return $rez;
}
# Telnet Related
function write_to_telnet($fp, $text){
fputs($fp,$text."\r\n");
usleep(TimeOut);
return true;
}
function read_from_telnet($fp){
$output = "";
$count = 0;
$count2 = 0;
do{
$char =fread($fp, 1);
$output .= $char;
if($char==">") $count++;
if($count==1) break;
if($char==".") $count2++;
if($count2==3) break;
} while(1==1);
$output=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$output);
$o=explode("\n",$output);
for($i=1;$i<=count($o)-2;$i++) $op.=$o[$i]."\n";
return $op;
}
$cmd = '#Something to possibly add
#/user aaa set use-radius=yes
#/user aaa set default-group=write
#/radius del [:find ]
#/radius remove [/radius find ]
#/radius add address=172.20.1.1 secret=radius timeout=00:00:05 service=login
/ip firewall mangle remove [/ip firewall mangle find]
/ip firewall address-list remove [/ip firewall address-list find]
/system script remove start_shaping
/system script remove stop_shaping
/queue tree remove [/queue tree find]
/queue simple remove [/queue simple find]
/ip firewall connection tracking set enabled=yes
/ip firewall mangle
:foreach intId in=[/interface wireless find] do={ \
:local intname [/interface wireless get $intId name]; \
:local foundint 0
:if ([:find $intname "Grid" -1] > -1) do={:set foundint 1}
:if ([:find $intname "grid" -1] > -1) do={:set foundint 1}
:if ($foundint = 0) do={ \
add chain=prerouting in-interface=$intname action=jump jump-target=markconn
}
}
/ip firewall address-list
add address=172.20.5.115 list=game_sites comment="COD4 (Centurion)"
add address=172.20.9.4 list=game_sites comment="Soldier of Fortune 2"
add address=172.20.13.50 list=game_sites comment="poker server"
add address=172.20.14.202 list=game_sites comment="UT2004"
add address=172.20.14.202 list=game_sites comment="DoD"
add address=172.20.4.226 list=game_sites comment="Bf2"
add address=172.20.2.1 list=game_sites comment="WOW"
add address=172.20.14.219 list=game_sites comment="WOW"
add address=172.20.4.227 list=game_sites comment="WOW"
add address=172.20.1.26 list=game_sites comment="Rfactor"
add address=172.20.11.138 list=game_sites comment="Quake3"
add address=172.20.7.30 list=game_sites comment="Quake3"
add address=172.20.248.104 list=game_sites comment="Quake3"
add address=172.20.1.26 list=game_sites comment="CSS"
add address=172.20.3.50 list=game_sites comment="CSS"
add address=172.20.20.139 list=game_sites comment="CSS"
add address=172.20.5.132 list=game_sites comment="CoD"
add address=172.20.7.29 list=game_sites comment="CoD"
add address=172.20.8.163 list=game_sites comment="CoD"
add address=172.16.1.10 list=game_sites comment="CoD"
add address=172.20.4.226 list=game_sites comment="CoD"
add address=172.20.10.59 list=game_sites comment="CoD"
add address=172.20.16.234 list=game_sites comment="CoD"
add address=172.20.3.50 list=game_sites comment="BattleNet"
add address=172.20.1.26 list=game_sites comment="BattleNet"
/ip firewall address-list
add address=172.20.5.132 comment="Torrent tracker" list=http_sites
add address=172.20.1.1 comment="PTAWUG Wiki" list=http_sites
add address=172.16.1.1 comment="JAWUG Wiki" list=http_sites
/ip firewall address-list
add address=172.20.14.178 comment="Lab Hi" list=lv_sites_hi
add address=172.20.20.36 comment="Andrew Hi" list=lv_sites_hi
add address=172.20.16.108 comment="Tim Hi" list=lv_sites_hi
add address=172.20.12.83 comment="Paul1 Hi" list=lv_sites_hi
add address=172.20.12.85 comment="Paul2 Hi" list=lv_sites_hi
add address=172.20.20.35 comment="Andrew Med" list=lv_sites_med
add address=172.20.16.109 comment="Tim Med" list=lv_sites_med
/ip firewall mangle
#Not sure why this is here as no rules using ingress-priority :)
add chain=prerouting action=set-priority comment="Change DSCP to WMM priority" new-priority=from-dscp passthrough=yes
add chain=postrouting action=jump jump-target=matchconn comment="Mark for shaping"
add chain=prerouting in-interface=ether1 action=jump jump-target=markconn comment="Mark router output to be treated as traffic from edge"
add chain=output action=jump jump-target=markconn comment="Mark router output to be treated as traffic from edge"
add chain=matchconn action=mark-connection new-connection-mark=filetrans dst-port=22 protocol=tcp dscp=2 passthrough=yes comment="Move From ssh if scp"
add chain=matchconn action=change-dscp new-dscp=46 connection-mark=voip passthrough=yes comment="voip"
add chain=matchconn action=mark-packet new-packet-mark=dscp_46 connection-mark=voip passthrough=no comment="voip"
add chain=matchconn action=change-dscp new-dscp=7 connection-mark=critical passthrough=yes comment="critical"
add chain=matchconn action=mark-packet new-packet-mark=dscp_7 connection-mark=critical passthrough=no comment="critical"
add chain=matchconn action=change-dscp new-dscp=6 connection-mark=games passthrough=yes comment="games"
add chain=matchconn action=mark-packet new-packet-mark=dscp_6 connection-mark=games passthrough=no comment="games"
add chain=matchconn action=change-dscp new-dscp=5 connection-mark=managment passthrough=yes comment="managment"
add chain=matchconn action=mark-packet new-packet-mark=dscp_5 connection-mark=managment passthrough=no comment="managment"
add chain=matchconn action=change-dscp new-dscp=4 connection-mark=high passthrough=yes comment="high"
add chain=matchconn action=mark-packet new-packet-mark=dscp_4 connection-mark=high passthrough=no comment="high"
add chain=matchconn action=change-dscp new-dscp=3 connection-mark=normal passthrough=yes comment="normal"
add chain=matchconn action=mark-packet new-packet-mark=dscp_3 connection-mark=normal passthrough=no comment="normal"
add chain=matchconn action=change-dscp new-dscp=2 connection-mark=filetrans passthrough=yes comment="filetrans"
add chain=matchconn action=mark-packet new-packet-mark=dscp_2 connection-mark=filetrans passthrough=no comment="filetrans"
add chain=matchconn action=change-dscp new-dscp=1 connection-mark=bulk passthrough=yes comment="bulk"
add chain=matchconn action=mark-packet new-packet-mark=dscp_1 connection-mark=bulk passthrough=no comment="bulk"
add chain=matchconn action=change-dscp new-dscp=46 connection-mark=lvhi passthrough=yes comment="lvhi"
add chain=matchconn action=mark-packet new-packet-mark=dscp_46 connection-mark=lvhi passthrough=no comment="lvhi"
add chain=matchconn action=change-dscp new-dscp=5 connection-mark=lvmed passthrough=yes comment="lvmed"
add chain=matchconn action=mark-packet new-packet-mark=dscp_5 connection-mark=lvmed passthrough=no comment="lvmed"
#Something strange
add chain=matchconn action=change-dscp new-dscp=1 connection-state=invalid passthrough=yes comment="Invalid connections bulk"
add chain=matchconn action=mark-packet new-packet-mark=dscp_1 connection-state=invalid passthrough=no comment="Invalid connections bulk"
add chain=matchconn action=log log-prefix=test comment="Why the rule below is being hit" disabled=yes
add chain=matchconn action=jump jump-target=markconn comment="Should not be hit as everything should have a connection mark at this point"
add chain=markconn action=jump jump-target=markconngames dst-address-list=game_sites
add chain=markconn action=mark-connection new-connection-mark=voip passthrough=no dscp=46 comment="VOIP DSCP 46"
add chain=markconn action=mark-connection new-connection-mark=bulk passthrough=no protocol=tcp p2p=all-p2p comment="P2P"
add chain=markconn action=mark-connection new-connection-mark=critical passthrough=no dst-port=179 protocol=tcp comment="BGP"
add chain=markconn action=mark-connection new-connection-mark=critical passthrough=no protocol=icmp comment="ICMP"
add chain=markconn action=mark-connection new-connection-mark=critical passthrough=no dst-port=53 protocol=tcp comment="DNS TCP"
add chain=markconn action=mark-connection new-connection-mark=critical passthrough=no dst-port=53 protocol=udp comment="DNS UDP"
add chain=markconn action=mark-connection new-connection-mark=critical passthrough=no protocol=ospf comment="OSPF"
add chain=markconngames action=mark-connection new-connection-mark=games passthrough=no dst-port=34447 protocol=tcp comment="rFactor"
add chain=markconngames action=mark-connection new-connection-mark=games passthrough=no dst-port=34297,34397 protocol=udp comment="rFactor"
add chain=markconngames action=mark-connection new-connection-mark=games passthrough=no dst-port=7777-7778 protocol=tcp comment="UT2004"
add chain=markconngames action=mark-connection new-connection-mark=games passthrough=no dst-port=8085-8087 protocol=tcp comment="WOW"
add chain=markconngames action=mark-connection new-connection-mark=games passthrough=no dst-port=6112 protocol=tcp comment="Warcraft III"
add chain=markconngames action=mark-connection new-connection-mark=games passthrough=no dst-port=28960 protocol=udp comment="COD4"
add chain=markconngames action=mark-connection new-connection-mark=games passthrough=no dst-port=27000-27065 protocol=udp comment="HL2 (CSS)"
add chain=markconngames action=mark-connection new-connection-mark=games passthrough=no dst-port=7777-7778 protocol=udp comment="UT2004"
add chain=markconngames action=mark-connection new-connection-mark=games passthrough=no dst-port=40888 protocol=udp comment="Poker"
add chain=markconn action=mark-connection new-connection-mark=filetrans passthrough=no dst-port=22 protocol=tcp dscp=2 comment="SCP"
add chain=markconn action=mark-connection new-connection-mark=filetrans passthrough=no dst-port=23 protocol=tcp comment="FTP"
add chain=markconn action=mark-connection new-connection-mark=managment passthrough=no dst-port=9291 protocol=tcp comment="WINBOX"
add chain=markconn action=mark-connection new-connection-mark=managment passthrough=no dst-port=22 protocol=tcp comment="ssh"
add chain=markconn action=mark-connection new-connection-mark=managment passthrough=no dst-port=23 protocol=tcp comment="Telnet"
add chain=markconn action=mark-connection new-connection-mark=managment passthrough=no dst-port=161-162 protocol=udp comment="SNMP"
add chain=markconn action=mark-connection new-connection-mark=managment passthrough=no dst-port=123 protocol=udp comment="NTP"
add chain=markconn action=mark-connection new-connection-mark=managment passthrough=no dst-port=1812-1813 protocol=udp comment="RADIUS"
add chain=markconn action=mark-connection new-connection-mark=high passthrough=no protocol=tcp packet-size=0-64 tcp-flags=ack comment="Make small ack packets go quicker"
add chain=markconn action=mark-connection new-connection-mark=high passthrough=no dst-port=1935 protocol=tcp comment="Flash - Video"
add chain=markconn action=mark-connection new-connection-mark=high passthrough=no dst-port=1234 protocol=tcp comment="VLC - Video"
add chain=markconn action=mark-connection new-connection-mark=high passthrough=no dst-port=80 dst-address-list=http_sites protocol=tcp connection-limit=0,32 comment="HTTP"
add chain=markconn action=mark-connection new-connection-mark=high passthrough=no dst-port=443 dst-address-list=http_sites protocol=tcp comment="HTTPs"
add chain=markconn action=mark-connection new-connection-mark=high passthrough=no dst-port=8000-8001 protocol=tcp comment="Shoutcast/Icecast"
add chain=markconn action=mark-connection new-connection-mark=high passthrough=no dst-port=6667 protocol=tcp comment="IRC"
add chain=markconn action=mark-connection new-connection-mark=filetrans passthrough=no dscp=2 comment="Maximize throughput"
add chain=markconn action=mark-connection new-connection-mark=normal passthrough=no comment="Not P2P & not Defined"
add chain=markconn action=mark-connection new-connection-mark=lvhi passthrough=no dst-address-list=lv_sites_hi protocol=tcp comment="lv traffic hi to"
add chain=markconn action=mark-connection new-connection-mark=lvhi passthrough=no src-address-list=lv_sites_hi protocol=tcp comment="lv traffic hi from"
add chain=markconn action=mark-connection new-connection-mark=lvmed passthrough=no dst-address-list=lv_sites_med protocol=tcp comment="lv traffic med to"
add chain=markconn action=mark-connection new-connection-mark=lvmed passthrough=no src-address-list=lv_sites_med protocol=tcp comment="lv traffic med from"
/system script add name=start_shaping
/system script add name=stop_shaping
/queue type
remove pfifo
remove pfifo_long
remove red
remove sfq
remove pcq_game
remove pcq_rest
remove pcq_default
add kind=pfifo name=pfifo pfifo-limit=50
add kind=pfifo name=pfifo_long pfifo-limit=250
add kind=sfq name=sfq sfq-allot=1514 sfq-perturb=5
add kind=red name=red red-avg-packet=1000 red-burst=40 red-limit=180 red-max-threshold=100 red-min-threshold=30
#Short que better latency
add kind=pcq name=pcq_game pcq-rate=0 pcq-classifier=dst-address pcq-limit=20 pcq-total-limit=500
add kind=pcq name=pcq_rest pcq-rate=0 pcq-classifier=dst-address pcq-limit=200 pcq-total-limit=8000
add kind=pcq name=pcq_default pcq-rate=0 pcq-classifier=dst-address pcq-limit=50 pcq-total-limit=2000
:foreach intId in=[/interface wireless find] do={ \
:local intname [/interface wireless get $intId name]; \
:local qname ("Q_" . $intname);
:local subqname ("Q_SUB_" . $intname);
:local maxlimit 1000000000;
/queue tree
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=$maxlimit \
max-limit=$maxlimit name=$qname parent=$intname
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=512000 \
max-limit=512000 name=("voip_46_" . $intname) packet-mark=dscp_46 parent=$qname priority=1 queue=pcq_game
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=512000 \
max-limit=512000 name=("critical_7_" . $intname) packet-mark=dscp_7 parent=$qname priority=1 queue=pcq_game
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=3000000 \
max-limit=3000000 name=("games_6_" . $intname) packet-mark=dscp_6 parent=$qname priority=2 queue=pcq_game
#pfifo
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=5000000 \
max-limit=$maxlimit name=("lv_hi_" . $intname) packet-mark=lvhi parent=$qname priority=1 queue=pcq_rest
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=1000000 \
max-limit=$maxlimit name=("lv_med_" . $intname) packet-mark=lvmed parent=$qname priority=2 queue=pcq_rest
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=512000 \
max-limit=512000 name=("management_5_" . $intname) packet-mark=dscp_5 parent=$qname priority=3 queue=pcq_default
#pfifo
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=1000000 \
max-limit=$maxlimit name=$subqname parent=$qname
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=100000 \
max-limit=$maxlimit name=("high_4_" . $intname) packet-mark=dscp_4 parent=$subqname priority=5 queue=pcq_rest
#pfifo_long
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=100000 \
max-limit=$maxlimit name=("norm_3_" . $intname) packet-mark=dscp_3 parent=$subqname priority=6 queue=pcq_rest
#pfifo_long
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=100000 \
max-limit=$maxlimit name=("filetrans_2_" . $intname) packet-mark=dscp_2 parent=$subqname priority=7 queue=pcq_rest
#pfifo_long
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=100000 \
max-limit=$maxlimit name=("bulk_1_" . $intname) packet-mark=dscp_1 parent=$subqname priority=8 queue=pcq_game
#sfq
add burst-limit=0 burst-threshold=0 burst-time=0s limit-at=100000 \
max-limit=$maxlimit name=("unmarked_" . $intname) packet-mark="unmarked" parent=$subqname priority=8 queue=pcq_game
#sfq
/system script
set start_shaping source=([/system script get start_shaping source] . " \
/queue tree set games_6_$intname limit-at=5000000 max-limit=5000000; \
/queue tree set [find parent=$subqname] max-limit=5000000; \
/queue tree set $subqname max-limit=5000000\r\ \n")
set stop_shaping source=([/system script get stop_shaping source] . " \
/queue tree set games_6_$intname limit-at=3000000 max-limit=3000000; \
/queue tree set [find parent=$subqname] max-limit=$maxlimit; \
/queue tree set $subqname max-limit=$maxlimit\r\ \n")
}
/system clock
set time-zone-name=Africa/Johannesburg
/system ntp client
set enabled=yes mode=unicast primary-ntp=172.20.2.1 secondary-ntp=\
172.20.11.34
/system scheduler
remove stop_shaping
add comment="" interval=1d name=stop_shaping \
on-event=stop_shaping start-date=jan/01/1970 start-time=01:30:00
:put "done"
';
$ServerList [] = "172.20.16.106";
$ServerList [] = "172.20.245.55";
$ServerList [] = "172.20.14.177";
$Username = 'username';
$Pass = 'password';
foreach ($ServerList as $Server)
{
routeros_connect($Server, $Username, $Pass);
routeros_cmd($cmd);
fclose($fp);
}
?>
Hello, can I send command tool fetch mode=http… with this Class ?
I Try some commands and works but tool fetch no
I’m trying to use this command to update routers affected by the
http://forum.mikrotik.com/t/urgent-security-advisory/117944/1
if possible, can you help me with syntax? Thank you