-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmikrotik_ip-arp.php
382 lines (310 loc) · 13.8 KB
/
mikrotik_ip-arp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* Mikrotik IP ARP
*
* PHP versions 5 and 7
*
* LICENSE: This source file is subject to version 3 of the GNU license
* that is available through the world-wide-web at the following URI:
* https://www.gnu.org/licenses/gpl.txt. If you did not receive a copy of
* the GNU License and are unable to obtain it through the web, please
* send a note to gnu@gnu.org so we they can mail you a copy immediately.
*
* @category Servers
* @package Mikrotik
* @author costa <deepcell@gmail.com>
* @copyright Free to use and change under the terms of the license.
* @license https://www.gnu.org/licenses/gpl.txt GNU License Version 3
* @version 0.1
* @link https://github.com/deepcell/Mikrotik
* @see --
* @since File available since Release 0.1
* @deprecated --
*/
/* controle aqui o acesso(sessao) ao script */
require 'config.php';
/**
*
* Importante
* para poder testar ativar "TEST" nas permissões de acesso para o
* usuário da API -->> `/system/user`
*
* Exemplo de GET request via url:
* `http://localhost/mikrotik_ip-arp.php?oprt=set&serverid=4&serveridnew=5&mk_mac=00:0C:42:69:E7:69&mk_ip=192.168.100.201&mk_published=true&mk_interface=NAMEINTERFACE&disabled=false&comment=test`
*
*/
$op = $_GET["oprt"]; // operacoes sao: delete, deletebyip, set e setmacip
$serverid = $_GET["serverid"]; //ID do servidor extraido do ip do cliente
$serveridnew = $_GET['serveridnew']; //ID do novo servidor escolhido
$mk_mac = $_GET['mk_mac'];
$mk_ip = $_GET['mk_ip'];
$mk_published = $_GET['mk_published']; // true ou false
$mk_interface = $_GET['mk_interface']; // nome da interface
$disabled = $_GET['disabled']; // true ou false
$comment = $_GET['comment'];
############################################## SERVIDOR ATUAL ##################
/*******************************************************************************
* Consulta os dados do servico do servidor - precisamos pegar o port do servico
* Precisa fazer a consulta baseado no parametro passado aqui via $_GET[serverid]
******************************************************************************/
$dss = $database->select( 'servidor_services', '*', array( 'server_id[=]' => $serverid ));
foreach($dss as $ds) {
// recebe a porta da API
if ($ds['service_name'] == "api" and $ds['service_status'] == 1)
$APIPort = $ds['service_port']; // porta usada para acessar a API Mikrotik
}
/*******************************************************************************
*consulta os dados do servidor para acesso
******************************************************************************/
$ds = $database->select( 'servidor', '*', array( 'id[=]' => $serverid ));
$ipmk = $ds['0']['ip_servidor'];
$user = $ds['0']['usuario'];
$pwdd = $ds['0']['autenticacao'];
$Pass = trim($pwdd);
################################################################################
########## SERVIDOR SELECIONADO ################################################
/*******************************************************************************
* Consulta os dados do servico do servidor - precisamos pegar o port do servico
* Precisa fazer a consulta baseado no parametro passado aqui via $_GET[serverid]
******************************************************************************/
$dss2 = $database->select( 'servidor_services', '*', array( 'server_id[=]' => $serveridnew ));
foreach($dss2 as $ds2) {
// recebe a porta da API
if ($ds2['service_name'] == "api" and $ds2['service_status'] == 1)
$APIPort2 = $ds2['service_port']; // porta usada para acessar a API Mikrotik
}
/*******************************************************************************
*consulta os dados do servidor para acesso
*
******************************************************************************/
$ds22 = $database->select( 'servidor', '*', array( 'id[=]' => $serveridnew ));
$ipmk2 = $ds22['0']['ip_servidor'];
$user2 = $ds22['0']['usuario'];
$pwdd2 = $ds22['0']['autenticacao'];
$Pass2 = trim($pwdd2);
// guardar na sessao dados da conexao
session_start();
$_SESSION['MK_IP2'] = $ipmk2;
$_SESSION['MK_USER2'] = $user2;
$_SESSION['MK_AUTH2'] = $Pass2;
$_SESSION['MK_PORT2'] = $APIPort2;
################################################################################
/**********************************************************************************
* DELETE : faz a remocao dos ips do gateway apresentado - remocao feito via MAC
**********************************************************************************/
if ($op == "delete") {
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect($ipmk, $user, $Pass, $APIPort)) {
$API->write("/ip/arp/getall",false);
$API->write('?mac-address='.$mk_mac,true);
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
if (count($ARRAY)>0) {
$API->write("/ip/arp/remove",false); // Na linha xx listamos as ether
$API->write("=.id=".$ARRAY[0]['.id'],true); // ID sequencial do MK da interface
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
$msg .= "<pre> A associação MACxIP foi removida na API com sucesso. </pre>";
} else {
//$READ = $API->read(false); nao usar esse metodo aqui novamente pois vai travar.
//$ARRAY = $API->ParseResponse($READ); nao usar esse metodo aqui novamente pois vai travar.
$msg .= "<pre>O mac não foi encontrado na API. Nada foi removido. <a href='' onclick='location.reload()'>⟳</a></pre>";
}
$API->disconnect();
} else
$msg .= "<pre>Erro na conexão com API Mikrotik.</pre>";
}
/**********************************************************************************
* DELETE : faz a remocao dos ips do gateway apresentado - remocao feito via IP
**********************************************************************************/
elseif ($op == "deletebyip") {
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect($ipmk, $user, $Pass, $APIPort)) {
$API->write("/ip/arp/getall",false);
$API->write('?address='.$mk_ip,true);
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
if (count($ARRAY)>0) {
$API->write("/ip/arp/remove",false); // Na linha xx listamos as ether
$API->write("=.id=".$ARRAY[0]['.id'],true); // ID sequencial do MK da interface
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
$msg .= "<pre> A associação MACxIP foi removida na API com sucesso. </pre>";
} else
$msg .= "<pre>O IP não foi encontrado na API. Nada foi removido. <a href='' onclick='location.reload()'>⟳</a></pre>";
$API->disconnect();
} else
$msg .= "<pre>Erro na conexão com API Mikrotik.</pre>";
}
/*******************************************************************************
* SET : faz a insert/update dos ips do gateway apresentado
* Obs.: Caso ocorra algum problema com SET entao alterar a consulta (getall)
* ao innvez de consultar pelo IP, entao consultar por MAC ADDRESS
******************************************************************************/
elseif ($op == "set") {
/* nao validar aqui, pois o metodo esta sendo usado em mais de um local, e alguns locais nao podem ser validado!
if ($mk_mac == "" && $mk_interface != "" && $mk_ip != "") {
die("erro: os dados nã foram validados corretamente.");
} */
// checa se dados do novo servidor existe, caso positivo entao precisa gravar os dados nesse novo servidor.
if (!empty($serveridnew)) {
$ipmk = $ipmk2;
$user = $user2;
$Pass = $Pass2;
$APIPort = $APIPort2;
} else {
$ipmk = $ipmk;
$user = $user;
$Pass = $Pass;
$APIPort = $APIPort;
}
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect($ipmk, $user, $Pass, $APIPort)) {
$API->write("/ip/arp/getall",false);
$API->write('?address='.$mk_ip,true);
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
if (count($ARRAY)>0) {
// apenas alteramos
$API->write("/ip/arp/set",false);
$API->write("=.id=".$ARRAY[0]['.id'],false);
$API->write('=mac-address='.$mk_mac,false); // Edita o campo MAC ($macAddress)
$API->write('=interface='.$mk_interface,false); // Nome da interface que esse IP atuará
$API->write('=published='.$mk_published,false); // Insere yes/no. Padrão="no". Não o utilizaremos agora.
$API->write('=disabled='.$disabled,false); // true/false
$API->write('=comment='.$comment,true); // Edita o campo Comentario
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
$msg .= "O MAC Address `".$mk_mac."` foi vinculado ao IP `".$mk_ip."` com sucesso na ARP List do servidor `".$ipmk."`.";
// update na tabela mikroti_ip-arp para atualizar historico
$database->update( "mikrotik_ip-arp",
array(
"mac" => $mk_mac,
"interface" => $mk_interface,
"server_id" => $serverid,
"published" => $mk_published,
"disabled" => $disabled,
"comment" => $comment
),
array( "ip[=]" => $mk_ip )
);
} else {
$API->write("/ip/arp/add",false);
$API->write('=address='.$mk_ip,false);
$API->write('=mac-address='.$mk_mac,false);
$API->write('=interface='.$mk_interface,false);
$API->write('=published='.$mk_published,false);
$API->write('=disabled='.$disabled,false);
$API->write('=comment='.$comment,true);
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
$msg .= "O IP ".$mk_ip." foi vinculado ao MAC ".$mk_mac." na ARP List do servidor `".$ipmk."`.";
// insert na tabela mikrotik_ip-arp para registrar historico.
$lastid = $database->insert("mikrotik_ip-arp",
array(
"id" => NULL,
"ip" => $mk_ip,
"mac" => $mk_mac,
"interface" => $mk_interface,
"server_id" => $serverid,
"published" => $mk_published,
"disabled" => $disabled,
"comment" => $comment
)
);
}
$API->disconnect();
} else
$msg .= "<pre>Erro na conexão com API Mikrotik.</pre>";
}
/*******************************************************************************
* SET NEW MAC ADDRESS : usado para atualizar ou cadastrar novo MACxIP
* A consulta na api deve ser feita pelo mac aqui.
******************************************************************************/
elseif ($op == "setmacip") {
if ($mk_mac == "" && $mk_interface != "" && $mk_ip != "")
die("erro: os dados nã foram validados corretamente.");
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect($ipmk, $user, $Pass, $APIPort)) {
$API->write("/ip/arp/getall",false);
$API->write('?mac-address='.$mk_mac,true);
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
if (count($ARRAY)>0) {
// desabilita o registro na api
$dis = "true";
$comm = "esse registro foi desabilitado, pois uma nova entrada com o mesmo IPxMAC foi registrada.";
$API->write("/ip/arp/set",false);
$API->write("=.id=".$ARRAY[0]['.id'],false);
//$API->write('=mac-address='.$mk_mac,false); // Edita o campo MAC ($macAddress)
//$API->write('=interface='.$mk_interface,false); // Nome da interface que esse IP atuará
//$API->write('=published='.$pub,false); // Insere yes/no. Padrão="no". Não o utilizaremos agora.
$API->write('=disabled='.$dis,false); // desabilitar esse registro na api
$API->write('=comment='.$comm,true); // Comentario qualquer
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
$msg .= "O MAC Address `".$mk_mac."` foi vinculado ao IP `".$mk_ip."` com sucesso na ARP List do servidor `".$ipmk."`.";
// cria um novo registro na api
$API->write("/ip/arp/add",false);
$API->write('=address='.$mk_ip,false);
$API->write('=mac-address='.$mk_mac,false);
$API->write('=interface='.$ARRAY[0]['.interface'],false); // aqui nao temos de onde receber esse dado
$API->write('=published='.$mk_published,false);
$API->write('=disabled='.$disabled,false);
$API->write('=comment='.$comment,true);
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
$msg .= "O IP ".$mk_ip." foi vinculado ao MAC ".$mk_mac." na ARP List do servidor `".$ipmk."`.";
// update na tabela mikroti_ip-arp - atualizacao do historico
$database->update("mikrotik_ip-arp",
array(
//"mac" => $mk_mac,
//"interface" => $mk_interface,
//"server_id" => $serverid,
"published" => $mk_published,
"disabled" => $disabled,
"comment" => $comment
),
array("mac[=]" => $mk_mac)
);
} else {
$API->write("/ip/arp/add",false);
$API->write('=address='.$mk_ip,false);
$API->write('=mac-address='.$mk_mac,false);
//$API->write('=interface='.$mk_interface,false); // aqui nao temos de onde receber esse dado
$API->write('=published='.$mk_published,false);
$API->write('=disabled='.$disabled,false);
$API->write('=comment='.$comment,true);
$READ = $API->read(false);
$ARRAY = $API->ParseResponse($READ);
$msg .= "O IP ".$mk_ip." foi vinculado ao MAC ".$mk_mac." na ARP List do servidor `".$ipmk."`.";
// insert na tabela mikrotik_ip-arp - registro de historico.
$lastid = $database->insert("mikrotik_ip-arp",
array(
"id" => NULL,
"ip" => $mk_ip,
"mac" => $mk_mac,
//"interface" => $mk_interface,
"server_id" => $serverid,
"published" => $mk_published,
"disabled" => $disabled,
"comment" => $comment
)
);
}
$API->disconnect();
} else
$msg .= "<pre>Erro na conexão com API Mikrotik.</pre>";
}
/*******************************************************************************
* DEFAULT : null
******************************************************************************/
else { echo "null"; }
/*******************************************************************************
* display
******************************************************************************/
echo $msg;