Skip to content

Instantly share code, notes, and snippets.

@jebaird
Last active November 3, 2022 23:32
Show Gist options
  • Save jebaird/4357122 to your computer and use it in GitHub Desktop.
Save jebaird/4357122 to your computer and use it in GitHub Desktop.
generate ip host config list form the host file at http://someonewhocares.org/hosts/ . Useful for blocking adds, spyware and shocks sites for whole networks
<?php
/*
* Hosts to ip host
*
* @author Jesse Baird <jebaird@gmail.com>
* @date 12/21/2012
*
* Licensed under The MIT License
* http://opensource.org/licenses/MIT
*
* Make hosts file into ip host command for cisco ios
*
* http://someonewhocares.org/hosts/
*
*
*
* Back story / abstract
*
* I recently ditched add block plus for firefox because it was eating my machine alive. I switched to using hostsman witch is a great utility for windows.
* But I felt that there was a better way to block ads for my entire home network. Since I have a Cisco router I decided to set it up as a domain forwarder
* I needed to setup a list of hosts to block. So I decided to make this script to pull the great hosts file from someonewhocares.org (many thanks to them!)
* But Cisco being Cisco didn't make it easy on me. If you get errors about not enough memory when trying to copy running-config to startup-config check out https://supportforums.cisco.com/docs/DOC-1841
*
* TODO:
* It would be nice to add in the comments into the config that are in this host file
*/
$TEST_MODE = FALSE;
/*
*
* out put for the Cisco config
*/
const NEWLINE = "\n";
/*
* chagne this to the ip you wish to redirect to
*/
$IP = '0.0.0.0';
$HOSTFILE_LOCATION = 'http://someonewhocares.org/hosts/zero/hosts';
/*
* the path and output filename
*/
$FILENAME_OUT = './hosts-to-ip-host-config.txt';
$FILENAME_OUT_CONFIG_HEADER = "!The following hosts are from ".$HOSTFILE_LOCATION." generated from, hosts to ip host By Jesse Baird <jebaird@gmail.com>".NEWLINE;
/*#############################################
*
*#############################################
*/
echo "Fetching hosts file.....";
if( $TEST_MODE ){
$hosts = <<<HOST
#<localhost>
127.0.0.1 localhost
127.0.0.1 localhost.localdomain
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 local
fe80::1%lo0 localhost
#</localhost>
#<shock-sites>
# For example, to block unpleasant pages, try:
0.0.0.0 goatse.cx # More information on sites such as
0.0.0.0 www.goatse.cx # these can be found in this article
0.0.0.0 oralse.cx # en.wikipedia.org/wiki/List_of_shock_sites
0.0.0.0 www.oralse.cx
0.0.0.0 goatse.ca
0.0.0.0 www.goatse.ca
0.0.0.0 oralse.ca
0.0.0.0 www.oralse.ca
0.0.0.0 goat.cx
0.0.0.0 www.goat.cx
0.0.0.0 1girl1pitcher.org
0.0.0.0 1guy1cock.com
0.0.0.0 1man1jar.org
HOST;
}else{
$hosts = file_get_contents( $HOSTFILE_LOCATION );
}
echo "done\n";
echo "Stripping started...";
//strip out everything before the #</localhost>
$hosts = substr($hosts, strpos( $hosts, '#</localhost>') + 14 );
//remove all comments
$hosts = preg_replace('/#.+/', "\n", $hosts);
//remove most of the white space
$hosts = preg_replace('/\s{2,100}/', ' ', $hosts);
$hosts = preg_replace('/\n/', ' ', $hosts);
$hosts = preg_replace('/\r/', ' ', $hosts);
/*
*
* find all of the valid host names
* http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address
*
*/
$hostNames = preg_split('/(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/', $hosts);
//print_r($hostNames);
echo "done\n";
echo "There are ".count( $hostNames )." hosts. Building list...\n";
$ipHost ="";
foreach( $hostNames as $name ){
$name = trim( $name );
if( !$name ){
continue;
}
$ipHost .= "ip host ".$name." ". $IP.NEWLINE;
}
file_put_contents($FILENAME_OUT, $FILENAME_OUT_CONFIG_HEADER.$ipHost );
echo "Done. Check ".$FILENAME_OUT. ' for the updated ip host list.';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment