source: trunk/Modules/NetworkConfigRouterOS/Generators/AddressPortability.php

Last change on this file was 935, checked in by chronos, 3 years ago
  • Modified: Code cleanup.
File size: 3.6 KB
Line 
1<?php
2
3if (isset($_SERVER['REMOTE_ADDR'])) die();
4include_once(dirname(__FILE__).'/../../../Common/Global.php');
5include_once(dirname(__FILE__).'/../Routerboard.php');
6$Path = array('ip', 'dhcp-server', 'lease');
7
8$Host = array();
9
10function ConvertExpireTime2($Time)
11{
12 $TimeParts = explode(':', $Time);
13 return $TimeParts[0] * 3600 + $TimeParts[1] * 60 + $TimeParts[2];
14}
15
16$Routerboard = new Routerboard();
17$Routerboard->UserName = $Config['MainRouter']['UserName'];
18$Routerboard->Timeout = $Config['MainRouter']['ConnectTimeout'];
19
20$DbResult3 = $System->Database->query('SELECT * FROM `NetworkSubnet` WHERE `Member` = 0 GROUP BY `DHCP`');
21while ($Subnet = $DbResult3->fetch_assoc())
22{
23 echo($Subnet['AddressRange'].'/'.$Subnet['Mask'].' on router '.$Subnet['DHCP']."\n");
24 $Routerboard->HostName = $Subnet['DHCP'];
25 $List = $Routerboard->ListGet($Path, array('address', 'active-mac-address', 'active-address', 'expires-after', 'server', 'dynamic'));
26 foreach ($List as $Properties)
27 {
28 if ($Properties['dynamic'] == 'true')
29 //and ($Properties['address'] != $Properties['active-address']))
30 {
31 $Properties['expires-after'] = time() - (24 * 3600 - ConvertExpireTime2($Properties['expires-after']));
32 echo('MAC: '.$Properties['active-mac-address']."\n");
33 $DbRows2 = $System->Database->query('SELECT `Id` FROM `NetworkInterface` WHERE `MAC`="'.$Properties['active-mac-address'].'"');
34 if ($DbRows2->num_rows > 0)
35 {
36 $Interface = $DbRows2->fetch_assoc();
37 $InterfaceId = $Interface['Id'];
38 $DbRows2 = $System->Database->query('SELECT `Id` FROM `NetworkInterfacePortable` WHERE `NetworkInterface`='.$InterfaceId);
39 if ($DbRows2->num_rows > 0)
40 {
41 $System->Database->update('NetworkInterfacePortable', '`Time` < "'.TimeToMysqlDateTime($Properties['expires-after']).'" AND `NetworkInterface`='.$InterfaceId, array('DynamicIP' => $Properties['active-address'], 'Update' => 1));
42 } else $System->Database->insert('NetworkInterfacePortable', array('NetworkInterface' => $InterfaceId, 'DynamicIP' => $Properties['active-address'], 'Time' => TimeToMysqlDateTime($Properties['expires-after']), 'Update' => 1));
43 } else echo('Not registred'."\n");
44 }
45 }
46}
47
48// Update mangle and NAT
49$PathNAT = array('ip', 'firewall', 'nat');
50$PathMangle = array('ip', 'firewall', 'mangle');
51$MangleRule = array();
52$NATRule = array();
53$DbRows = $System->Database->query('SELECT NetworkDevice.Name AS DeviceName, NetworkInterface.Name AS InterfaceName, DynamicIP FROM `NetworkInterfacePortable` JOIN NetworkInterface ON NetworkInterface.Id=NetworkInterfacePortable.NetworkInterface JOIN NetworkDevice ON NetworkDevice.Id = NetworkInterface.Device WHERE `Update`=1');
54while ($Portable = $DbRows->fetch_assoc())
55{
56 $Name = $Portable['DeviceName'];
57 if ($Portable['InterfaceName'] != '') $Name .= '-'.$Portable['InterfaceName'];
58 array_push($NATRule, implode(' ', $PathNAT).' set [find comment="'.$Name.'-in"] to-addresses='.$Portable['DynamicIP']);
59 array_push($NATRule, implode(' ', $PathNAT).' set [find comment="'.$Name.'-out"] src-address='.$Portable['DynamicIP']);
60 array_push($MangleRule, implode(' ', $PathMangle).' set [find comment="'.$Name.'-in"] dst-address='.$Portable['DynamicIP']);
61 array_push($MangleRule, implode(' ', $PathMangle).' set [find comment="'.$Name.'-out"] src-address='.$Portable['DynamicIP']);
62}
63
64$Routerboard->HostName = $Config['MainRouter']['HostName'];
65print_r($NATRule);
66print_r($MangleRule);
67$Routerboard->ExecuteBatch($NATRule);
68$Routerboard->ExecuteBatch($MangleRule);
69$System->Database->query('UPDATE NetworkInterfacePortable SET Update=0');
Note: See TracBrowser for help on using the repository browser.