-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHosts2XG.php
59 lines (42 loc) · 1.33 KB
/
Hosts2XG.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
<?php
// Set these Variables - XG Firewall
$usernameXG = "USER" ;
$pwXG = "PW";
$apiVersion = "VERSION";
$apiXG = "https://XG-IP:XG-PORT/webconsole/APIController?reqxml=";
// Read Host File in HOST:IP format
$hosts = file_get_contents('hosts.txt');
// +++++++++++++++++++ Process Data +++++++++++++++++++
// Create API Login - head
$head = $apiXG . '<Request APIVersion="' . $apiVersion . '"><Login><Username>' . $usernameXG . '</Username><Password passwordform="plain">' . $pwXG . '</Password></Login>';
processHosts($head,"hosts.txt", "host.curl");
// +++++++++++++++++++ Function Definitions +++++++++++++++++++
/**
* processHosts()
* Processes the a File with format HOST:IP\nHOST:IP
*
* Params:
* $head -> XML Head
* $inFile -> File to Read from
* $outfile -> Filename for Output
*
*/
function processHosts($head, $inFile, $outfile) {
$outFile = fopen($outfile, "w");
$file = fopen($inFile, "r");
if ($file) {
while (($line = fgets($file)) !== false) {
$arr = explode(":",$line);
$req = $head .'<Set operation="add">';
$req .= '<IPHost><Name>' . $arr[0] . '</Name><HostType>IP</HostType><IPAddress>'. trim($arr[1]) . '</IPAddress></IPHost>' ;
$req .= "</Set></Request>";
$req .= "\n";
fwrite($outFile,$req);
}
} else {
die("error opening file.");
}
fclose($file);
fclose($outFile);
}
?>