[SCRIPT] Adblock Script and Updater

Search This thread

yareally

Senior Member
Feb 19, 2011
1,444
229
codingcreation.com
I put together a small shell script to make it easy for anyone rooted to block ads and check to see if they have the latest version I keep of adhosts. If after checking you do not have the latest, it will download it for you automatically.

What does this script do (in a nutshell):

1) Checks to see if you have the latest version of ad hosts for adblocking from me
2) If you do not have the latest, it will download it for you
3) If you have the latest, it just exits out, no changes
4) Installs the script to your /system/xbin directory so the next time you just have to type:

Code:
su
blockads

and it will run the script for you :)

How to run this?

Get this if you are using an AOSP ROM:
1) Download it from http://dl.dropbox.com/u/24904191/blockads

Try this if you are using a SENSE ROM (thanks chrootz for the fixes):
1) http://dl.dropbox.com/u/24904191/sense/blockads

*** If you have troubles getting the script to work, try updating your system to the latest version of busybox (stick it in /system/xbin and overwrite the current version of busybox, then change the permissions to 755 and owner and group to root) ***

2) Move it to /data/local/tmp (if you dont have it, create it) from a file explorer with root permissions (must set the system in r/w mode) or from the terminal shell
3) make sure the script has the right permissions by typing:

Code:
su
chmod 755 /data/local/tmp/blockads

in the terminal shell (in the emulator or whatever you use to get to it)

4) run the script by typing

Code:
/data/local/tmp/blockads

5) let it do its thing

After its done you can run it again anytime from the terminal by typing

Code:
su
blockads

as already mentioned.


I'm working on making it a GUI app next (with more features), but this should tie anyone who wants it over until then :)

Anyone who finds any adservers and wants me to add them to the list, feel free to list them here and I will add them.

Latest updates:
2011-12-01: Updated to block carrieriq
 
Last edited:

newter55

Senior Member
Sep 14, 2010
2,273
178
Southern Michigan
Tried this out and got this:

sh-3.2# su
sh-3.2# /data/local/tmp/blockads
Fetching host file via hostname
wget: bad address 'dl.dropbox.com'
update could not be completed, sorry :(
sh-3.2#
 
  • Like
Reactions: ste_bran

yareally

Senior Member
Feb 19, 2011
1,444
229
codingcreation.com
Tried this out and got this:

sh-3.2# su
sh-3.2# /data/local/tmp/blockads
Fetching host file via hostname
wget: bad address 'dl.dropbox.com'
update could not be completed, sorry :(
sh-3.2#

I forgot some ROMs and such have a busybox version that doesn't work right with hostnames in wget. I'll see if I can work around that and update the script :)

EDIT: updated to work around by using the IP for those that their busybox wget does not work correctly with hostnames. Only issue with that is the IP may change.
 
Last edited:

GermanGuy

Senior Member
Mar 19, 2005
1,327
158
Minneapolis
Ok. I am stuck. I actually don't have a hosts file under "etc", only a hosts.bak. With root explorer mounted as r/w I am still not able to rename the "host.bak" file to "hosts" - error message "read-only file/directory". I created a blank "hosts" text file, put in on the sdcard and with terminal emulator still can't cp to "etc" because of "read-only directory" error message. Any other suggestions?
 

DroidTh3ory

Senior Member
Dec 12, 2010
4,214
2,093
Troy, Michigan
www.droid-hive.com
Ok. I am stuck. I actually don't have a hosts file under "etc", only a hosts.bak. With root explorer mounted as r/w I am still not able to rename the "host.bak" file to "hosts" - error message "read-only file/directory". I created a blank "hosts" text file, put in on the sdcard and with terminal emulator still can't cp to "etc" because of "read-only directory" error message. Any other suggestions?

Top right corner of Root Explorer .... Mount RW. :)



Sent from my A05PTH3ORY using XDA App
 
  • Like
Reactions: FBis251

newter55

Senior Member
Sep 14, 2010
2,273
178
Southern Michigan
I forgot some ROMs and such have a busybox version that doesn't work right with hostnames in wget. I'll see if I can work around that and update the script :)

EDIT: updated to work around by using the IP for those that their busybox wget does not work correctly with hostnames. Only issue with that is the IP may change.

Updated blockads script to test this am and:


sh-3.2$ export PATH=/data/local/bin:$PATH
sh-3.2$ su
sh-3.2# blockads
Fetching host file via hostname
wget: bad address 'dl.dropbox.com'
The busybox version of wget on your system does not work correctly with
hostnames. Trying workaround with latest known IP to the hostname...
Connecting to 184.73.229.87 (184.73.229.87:80)
hosts 100% |***********| 641k 00:00:00 ETA
backing up old host file and updating
failed on 'hosts' - Cross-device link
md5sum: can't open '/system/etc/hosts': No such file or directory
/system/xbin/blockads: line 38: [: 8172d35d1b72871f7e1ef4e709920f0e: unary operator expected
Unable to chmod /system/etc/hosts: No such file or directory
Unable to chmod /system/etc/hosts: No such file or directory
remounting filesystem in r/o mode...
Cleaning up files...
rm failed for -f, No such file or directory
update complete!
sh-3.2#

The end result is the existing hosts file being renamed hosts.bak and obviously no new hosts filebeing being created.

EDIT.... The above is testing on my inspire running busybox 1.19. Tried using the script on my sensation running bb 1.18.4 and it works fine.

Edit edit... Installed bb 1.18.4 on inspire and result is the same as posted above.
 
Last edited:
After reading over the code and seeing the comments, I can only guess you had the modified version of wget on your device during testing, which does support host names. Before I continue, I am not trying to rain on your parade, but... By downloading the file, checking the md5, comparing it to the existing file, replacing if needed, and then cleaning up... you went about 5 steps beyond what just doing an "if server file is newer" check would do by including a custom wget. Even though you have to put in the code to install and verify the wget, the trade is that you no longer have to download the file every time, check it every time, and worry about making sure the md5 matches.

If you decide you want to switch over to that method, you can check out the option already included in http://xdaforums.com/showthread.php?t=1092128 by downloading the live install version to view the actual script. Just a recommendation to optimize the process. Nice work, though, you definitely found a way to get the job done.

As for the difference between this and Adfree. There is no real difference. Adfree checks the server, does a callback, and prompts the user with updates. Adfree just requires an entire shell to turn it into an app, but will automatically run on boot.
 

spotmark

Senior Member
Feb 18, 2010
3,435
2,041
Massachusetts
Samsung Galaxy Z Fold 4
As for the difference between this and Adfree. There is no real difference. Adfree checks the server, does a callback, and prompts the user with updates. Adfree just requires an entire shell to turn it into an app, but will automatically run on boot.

So, for those of you not comfortable running ADB, or Terminal Emulator, just install AdFree, and let it do everything for you.
 
So, for those of you not comfortable running ADB, or Terminal Emulator, just install AdFree, and let it do everything for you.

Well, you also have the option of most included ROM toolkits, ScriptFusion, and a lot of preinstalled adblock scripts, but the big difference is the hosts file itself. Sounds like this one is custom, whereas mine takes bamf and adfree and merges them. This one may end up blocking more ads, but the only way to know would be to sit down and compare them all.

Sent from my ADR6400L using Tapatalk
 

spotmark

Senior Member
Feb 18, 2010
3,435
2,041
Massachusetts
Samsung Galaxy Z Fold 4
Well, you also have the option of most included ROM toolkits, ScriptFusion, and a lot of preinstalled adblock scripts, but the big difference is the hosts file itself. Sounds like this one is custom, whereas mine takes bamf and adfree and merges them. This one may end up blocking more ads, but the only way to know would be to sit down and compare them all.

Sent from my ADR6400L using Tapatalk

Ah, I see.
 

yareally

Senior Member
Feb 19, 2011
1,444
229
codingcreation.com
Thank you for your comments everyone :). I'll refine the script a bit more and also deal with a permanent workaround for not having a wget that checks hostsnames by testing with a ping and getting the IP first, using that and then doing wget. This started as a way for me to deal with having to move my ad host file each time I update it on my phone to just giving it out to everyone.

The only other difference than what was mentioned between my script and adfree as far as I know is I include a wider range of adhosts according to those that have used both. I have never used adfree, but people that have said I blocked more ads than they do and I also use an invalid IP address to block (0.0.0.0) instead of the loopback address (127.0.0.1) so it kills them a bit faster.

If anyone is still having problems that weren't resolved with it from above, let me know as well. I tested it against busybox 1.16.2 on my own phone, but it seems no 2 busybox version are alike :)

I havent tried this, but this one does include busybox 1.19 for those that don't want to manually install it. https://market.android.com/details?id=com.jrummy.busybox.installer&feature=related_apps
 
Last edited:
Thank you for your comments everyone :). I'll refine the script a bit more and also deal with a permanent workaround for not having a wget that checks hostsnames by testing with a ping and getting the IP first, using that and then doing wget. This started as a way for me to deal with having to move my ad host file each time I update it on my phone to just giving it out to everyone.

The only other difference than what was mentioned between my script and adfree as far as I know is I include a wider range of adhosts according to those that have used both. I have never used adfree, but people that have said I blocked more ads than they do and I also use an invalid IP address to block (0.0.0.0) instead of the loopback address (127.0.0.1) so it kills them a bit faster.

If anyone is still having problems that weren't resolved with it from above, let me know as well. I tested it against busybox 1.16.2 on my own phone, but it seems no 2 busybox version are alike :)

I havent tried this, but this one does include busybox 1.19 for those that don't want to manually install it. https://market.android.com/details?id=com.jrummy.busybox.installer&feature=related_apps

Hit me up if I can be any help. It's good to have a better list available. Adfree has never been very thorough.

Sent from my ADR6400L using Tapatalk
 

chrootz

Senior Member
Apr 9, 2011
397
91
+1 I exam yareally's hosts, This is much faster killing them and I glad you finally developing the script update for the hosts. I am going test your script update on 2 of the roms A05PTH3ORY and Gengeritis 3D VII. Thanks - Keep it up :D

The only other difference than what was mentioned between my script and adfree as far as I know is I include a wider range of adhosts according to those that have used both. I have never used adfree, but people that have said I blocked more ads than they do and I also use an invalid IP address to block (0.0.0.0) instead of the loopback address (127.0.0.1) so it kills them a bit faster.
 

Zomb!e

Senior Member
Jul 19, 2010
362
44
Thanks for the script. I'm going to give it a shot in a little bit. AdFree hasn't worked right in months. I've been using Absolute System to block ads, but I think I'll give this a try. Thanks again.
 

yareally

Senior Member
Feb 19, 2011
1,444
229
codingcreation.com
I'm about 50% done working on a basic gui app to do what the script does + some other features, just to give an update. GUI app will be free to any xda members of course :)


Things I am putting into it:

Choice between no blocking, partial blocking (basically in browser only stuff in case you want to show developers some love by not blocking their application ads), and full blocking.

Updating like the script does with just a touch of a button.

Optional ability to download a host block list for your desktop pc.

*features that may come later*
-ability to send me hosts from the app to add to the block list
-ability to add your own hosts on the fly to the list (either for personal routing or for adblocking, since adblocking is only the secondary use of the hosts file). Unfortunately, you will have to figure out the hosts yourself, since there's no easy way to tell really automatically.

Any other suggestions are welcomed
 
Last edited:

Mustang302LX

Senior Member
Feb 28, 2011
1,634
161
40
Petaluma, CA
I'm about 50% done working on a basic gui app to do what the script does + some other features, just to give an update. GUI app will be free to any xda members of course :)


Things I am putting into it:

Choice between no blocking, partial blocking (basically in browser only stuff in case you want to show developers some love by not blocking their application ads), and full blocking.

Updating like the script does with just a touch of a button.

Optional ability to download a host block list for your desktop pc.

*features that may come later*
-ability to send me hosts from the app to add to the block list
-ability to add your own hosts on the fly to the list (either for personal routing or for adblocking, since adblocking is only the secondary use of the hosts file). Unfortunately, you will have to figure out the hosts yourself, since there's no easy way to tell really automatically.

Any other suggestions are welcomed

Sounds like AdFree but much better and will work better as well. Can't wait!!
 

Camaros_Kill

Senior Member
Apr 10, 2010
180
12
Just tried this today on gingeritis 3d beta8 and it doesnt seem to work as i get the same error someone else posted a few posts back. Something about no such file or directory system/etc/hosts. I reverted adfree and uninstalled it and renamed the hosts file to hosts from hosts.bak but when I Su adblock it gets renamed to hosts.bak and terminal shows that same error
 

Top Liked Posts

  • There are no posts matching your filters.
  • 7
    I put together a small shell script to make it easy for anyone rooted to block ads and check to see if they have the latest version I keep of adhosts. If after checking you do not have the latest, it will download it for you automatically.

    What does this script do (in a nutshell):

    1) Checks to see if you have the latest version of ad hosts for adblocking from me
    2) If you do not have the latest, it will download it for you
    3) If you have the latest, it just exits out, no changes
    4) Installs the script to your /system/xbin directory so the next time you just have to type:

    Code:
    su
    blockads

    and it will run the script for you :)

    How to run this?

    Get this if you are using an AOSP ROM:
    1) Download it from http://dl.dropbox.com/u/24904191/blockads

    Try this if you are using a SENSE ROM (thanks chrootz for the fixes):
    1) http://dl.dropbox.com/u/24904191/sense/blockads

    *** If you have troubles getting the script to work, try updating your system to the latest version of busybox (stick it in /system/xbin and overwrite the current version of busybox, then change the permissions to 755 and owner and group to root) ***

    2) Move it to /data/local/tmp (if you dont have it, create it) from a file explorer with root permissions (must set the system in r/w mode) or from the terminal shell
    3) make sure the script has the right permissions by typing:

    Code:
    su
    chmod 755 /data/local/tmp/blockads

    in the terminal shell (in the emulator or whatever you use to get to it)

    4) run the script by typing

    Code:
    /data/local/tmp/blockads

    5) let it do its thing

    After its done you can run it again anytime from the terminal by typing

    Code:
    su
    blockads

    as already mentioned.


    I'm working on making it a GUI app next (with more features), but this should tie anyone who wants it over until then :)

    Anyone who finds any adservers and wants me to add them to the list, feel free to list them here and I will add them.

    Latest updates:
    2011-12-01: Updated to block carrieriq
    2
    I'm about 50% done working on a basic gui app to do what the script does + some other features, just to give an update. GUI app will be free to any xda members of course :)


    Things I am putting into it:

    Choice between no blocking, partial blocking (basically in browser only stuff in case you want to show developers some love by not blocking their application ads), and full blocking.

    Updating like the script does with just a touch of a button.

    Optional ability to download a host block list for your desktop pc.

    *features that may come later*
    -ability to send me hosts from the app to add to the block list
    -ability to add your own hosts on the fly to the list (either for personal routing or for adblocking, since adblocking is only the secondary use of the hosts file). Unfortunately, you will have to figure out the hosts yourself, since there's no easy way to tell really automatically.

    Any other suggestions are welcomed
    1
    Tried this out and got this:

    sh-3.2# su
    sh-3.2# /data/local/tmp/blockads
    Fetching host file via hostname
    wget: bad address 'dl.dropbox.com'
    update could not be completed, sorry :(
    sh-3.2#
    1
    Ok. I am stuck. I actually don't have a hosts file under "etc", only a hosts.bak. With root explorer mounted as r/w I am still not able to rename the "host.bak" file to "hosts" - error message "read-only file/directory". I created a blank "hosts" text file, put in on the sdcard and with terminal emulator still can't cp to "etc" because of "read-only directory" error message. Any other suggestions?

    Top right corner of Root Explorer .... Mount RW. :)



    Sent from my A05PTH3ORY using XDA App
    1
    Added about 10-15 more hosts today.