Skip to content

Instantly share code, notes, and snippets.

@iamso
Created August 31, 2016 11:52
Show Gist options
  • Save iamso/050845c199590f6e2443e564610e5b0f to your computer and use it in GitHub Desktop.
Save iamso/050845c199590f6e2443e564610e5b0f to your computer and use it in GitHub Desktop.
Output hosts file entries for use in VirtualBox and Parallels Desktop
<?php
$file = file_get_contents("/etc/hosts");
$lines = explode("\n", $file);
$hosts = array();
foreach($lines as $line) {
if (preg_match("/^#/", $line) || !$line) {
continue;
}
$line = preg_replace("/((\d+\.+\d+\.*)+)\s*/", '', $line);
$line = preg_replace("/\:\:\d\s*/", '', $line);
$line = preg_replace("/\s*\#\s*.*/", '', $line);
$line = preg_replace("/\t*/", '', $line);
$hosts[] = $line;
}
$hosts = array_unique($hosts);
sort($hosts);
header('Content-Type: text/plain');
echo "VirtualBox:\n-----------\n";
foreach($hosts as $host) {
echo "10.0.2.2 $host\n";
}
echo "\n\nParallels Desktop:\n------------------\n";
foreach($hosts as $host) {
echo "10.211.55.2 $host\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment