-
Notifications
You must be signed in to change notification settings - Fork 0
/
apconvert.php
180 lines (149 loc) · 5.38 KB
/
apconvert.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
<?php
# FortiAP Migratie script
# Rudy Broersma <[email protected]>
$host = "https://";
$uri = "/api/v2/cmdb/switch-controller/managed-switch";
$token = "";
$readonly = TRUE;
$trigger_description = "ACCESS";
$trigger_vlan = "VLAN_600";
$fortiap_vlan = "VLAN_100";
$fortiap_allowed_vlans = [ "VLAN_101", "quarantine" ];
$whitelist_switches = [ ];
class SQLiteConnection {
const SQLITE_FILE = '/tmp/test.sqlite3';
private $pdo;
public function __construct() {
$this->connect();
$this->initialize();
}
private function connect() {
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
if ($this->pdo == null) {
$this->pdo = new \PDO("sqlite:" . self::SQLITE_FILE, null, null, $options);
}
return $this->pdo;
}
private function initialize() {
$query = $this->pdo->prepare("CREATE TABLE IF NOT EXISTS switchport(sn TEXT, port TEXT, status INT DEFAULT 0, hasbeenup INT DEFAULT 0, UNIQUE(sn, port))");
return $query->execute();
}
public function setPort($sn, $port_id, $status) {
$portStatus = $this->getPortHistorical($sn, $port_id);
if ($portStatus == 1) {
$hasbeenup = 1;
} else {
$hasbeenup = 0;
};
if ($status == 1) {
$hasbeenup = 1;
};
if ($portStatus === FALSE) {
$query = $this->pdo->prepare("INSERT INTO switchport(sn, port, status, hasbeenup) VALUES(:sn, :port_id, :status, :hasbeenup)");
} else {
$query = $this->pdo->prepare("UPDATE switchport SET status = :status, hasbeenup = :hasbeenup WHERE sn = :sn AND port = :port");
};
try {
$query->execute(['sn' => $sn, 'port_id' => $port_id, 'status' => $status, 'hasbeenup' => $hasbeenup]);
$result = $query->rowCount();
} catch (PDOException $e) { }
}
public function getPortCurrent($sn, $port_id) {
$query = $this->pdo->prepare("SELECT * FROM switchport WHERE sn = :sn AND port = :port_id");
$query->execute(['sn' => $sn, 'port_id' => $port_id]);
$retVal = $query->fetch();
if ($retVal === FALSE) { return FALSE; } else { return $retVal['status']; };
}
public function getPortHistorical($sn, $port_id) {
$query = $this->pdo->prepare("SELECT * FROM switchport WHERE sn = :sn AND port = :port_id");
$query->execute(['sn' => $sn, 'port_id' => $port_id]);
$retVal = $query->fetch();
if ($retVal === FALSE) { return FALSE; } else { return $retVal['hasbeenup']; };
}
}
$db = new SQLiteConnection();
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
function fix_port($switch, $port) {
global $host, $token;
global $fortiap_vlan, $fortiap_allowed_vlans;
global $readonly;
$uri = "/api/v2/cmdb/switch-controller/managed-switch/" . $switch . "/ports/" . $port;
$av_block = "";
foreach($fortiap_allowed_vlans as $av) {
$av_block = $av_block . "{ \"vlan-name\": \"" . $av . "\", \"q_origin_key\": \"" . $av . "\"},";
};
$put_data = "
{
\"json\" : {
\"vlan\": \"". $fortiap_vlan . "\",
\"allowed-vlans\": [
" . $av_block . "
],
}
}";
$defaults = array(
CURLOPT_URL => $host . $uri . "?&access_token=" . $token,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $put_data
);
$ch = curl_init();
curl_setopt_array($ch, $defaults);
if ($readonly != true) {
$data = curl_exec($ch);
$retval = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($retval == "200") {
echo $switch . " - " . $port . " is reconfigured!\n";
} else {
echo $switch . " - " . $port . " FAILED TO RECONFIGURE!\n";
};
} else {
echo $switch . " - " . $port . " NOT CHANGED. Read Only Mode is enabled\n";
}
}
$defaults = array(
CURLOPT_URL => $host . $uri . "?&access_token=" . $token,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
);
$ch = curl_init();
curl_setopt_array($ch, $defaults);
while(true) {
$data = curl_exec($ch);
#var_dump(curl_getinfo($ch));
#var_dump(curl_error($ch));
#var_dump($data);
$json = json_decode($data);
#var_dump($json);
foreach($json->results as $switch) {
echo "Switch " . $switch->{'switch-id'} . ":\n";
foreach($switch->ports as $port) {
echo $port->{'switch-id'} . " - " . $port->{'port-name'} . " - " . $port->{'link-status'} . "\n";
if (str_contains(strtoupper($port->{'description'}), strtoupper($trigger_description)) && $port->vlan == $trigger_vlan) {
echo $port->{'switch-id'} . " - " . $port->{'port-name'} . " is configured as accesspoint and has trigger VLAN!\n";
if ($port->{'link-status'} == "down") {
if ($db->getPortHistorical($port->{'switch-id'}, $port->{'port-name'}) == 1) {
echo $port->{'switch-id'} . " - " . $port->{'port-name'} . " is a trigger port with status DOWN and was up at some point. Reconfiguring...\n";
$db->setPort($port->{'switch-id'}, $port->{'port-name'}, 0);
fix_port($port->{'switch-id'}, $port->{'port-name'});
};
} else {
// Port is up. Store in DB.
$db->setPort($port->{'switch-id'}, $port->{'port-name'}, 1);
}
};
};
};
sleep(1);
};