Skip to content

Instantly share code, notes, and snippets.

@cntlscrut
Created June 15, 2018 20:20
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cntlscrut/ac2010ae0ed5bd547480c1b612ca082f to your computer and use it in GitHub Desktop.
Quick CSV to ACL declaration function
<?php
/**
* Read in a CSV file as taken from the command line arg
* CSV file needs to be in the same directory as this
* script for the time being
*
* Output is expected to be text representing an ACL
* declaration that can be copypasta'd into a VCL file.
*
* ACL function name will be the header from CSV and
* IPs will be formatted accordingly.
*/
/**
* Grab the filename from the command line args
*/
$filename = $argv[1];
$handle = fopen($filename, "r");
$output = "";
$i = 0;
if ($handle !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
//on the first iteration, we'll create the acl function name using the csv header
if ($i == 0) {
$output = "acl " . strtolower(str_replace(" ", "_", $data[0])) . " {\n";
} else {
$ipstr = explode('/', $data[0]);
$output .= " \"$ipstr[0]\"/$ipstr[1];\n";
}
$i++;
}
}
$output .= "}";
if (file_put_contents("acl-convert.txt", $output)) {
echo "all done!\n";
}
fclose($handle);
@cntlscrut
Copy link
Author

as long as the CSV file is in the same directory, you can run the script as:

php aclcreate.php [filename]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment