Created
June 15, 2018 20:20
Star
You must be signed in to star a gist
Quick CSV to ACL declaration function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
as long as the CSV file is in the same directory, you can run the script as:
php aclcreate.php [filename]