Archive for the ‘Unix’ Category

Domain redirection to localhost

Thursday, March 20th, 2008

What is this: When you enter http://www.yahoo.com in your address bar of browser, It wont go to http://www.yahoo.com rather it will show a custom page which is inside you pc not in Internet.

Why is this: Sometimes you may need to restrict your children from accessing site that contains adult content, violent pictures etc. Also web service developer may use it to simulate and debug web service host.

Tools: You need actually nothing. Only skill of editing files. A text editor will do. In Windows machine you can use notepad.exe. For Linux machine you can use vi, ed, ee etc.

How: There is a special file called `hosts` which contain about domain and ip address mapping information. The format is plain text. In Windows the file in “%SystemRoot%\System32\Drivers\Etc” directory and in Linux its in “/etc” directory. Here is a sample file content for my Windows XP Service pack 2.


# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a ‘#’ symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

127.0.0.1 localhost

Its almost same for Linux. This file is responsible to redirect any request to “localhost” domain to 127.0.0.1 IP. Yes, thats why you get ping response from 127.0.0.1 when you ping “localhost”.

Now open the file and add some alias to “localhost” entry. Like the following,

127.0.0.1 localhost badweb.com www.badweb2.net

When you are done saving the file. Ping “badweb.com”. You’ll get response from 127.0.0.1. Now whenever someone enters http://www.badweb2.net in any browser it will show the content of http://127.0.0.1/. If you don’t have any web server installed it will show a “page not found” page. And it serves our purpose as well. The surfer of badweb.com will always see the web is down. ;)

Disadvantage: The main problem is if you put “badweb.com” as alias it wont work for any sub domain of “badweb.com”. For example, If you put,

127.0.0.1 localhost badweb.com

Entering http://www.badweb.com/ wont redirect to http://127.0.0.1 also http://anysubdomain.badweb.com wont work too. To work around you have to put series of sub domains. Like this,

127.0.0.1 localhost badweb.com www.badweb.com anysubdomain.badweb.com

For this the entry will be too long. :( :(

Some addition: You can enhance it more by the following methods

  1. Installing a web server.
  2. Creating alias IP for your network device

Installing a web server will make you show a customized page. You can put a message there too. Like “Access denied”. So unauthorized user will see your message every time they visit their page. Also you can put per domain customized page with a little knowledge of Javascript or PHP or URL rewrite.

If you can create alias of your own IP pinging the domain will get response from different IP and all of the IP are alias. Its just same web but IPs are different. if you use 176.16.16.[1-254] as you alias you can put some entry in hosts file like the follwing


127.0.0.1 localhost
176.16.16.1 badweb.com
176.16.16.4 www.badweb.com
176.16.16.2 badweb.net
176.16.16.3 verybadweb.com

Now you’ll get response from different IPs. It’ll be hard for visitor to track the whole thing.

Thats all.

Keep bluffing. ;)



© 2008 by A K M Mokaddim

How to Pipe standard error (STDERR) to another program

Thursday, January 24th, 2008

Everybody who has a little knowledge in linux shell know about piping and redirection. Piping is done by ‘|’ sign and for redirection ‘>’, ‘<’, ‘<<’, ‘>>’, ‘<<<’ signs are used.To redirect standard output of a program to a file we use

% foo > bar.txt

or
% foo 1> bar.txt

To redirect standard error of a program to a file we use

% foo 2> bar.txt

The pipe command is used normally to pass one commands output to another programs input. To pass output of `foo` to `bar` we use

% foo | bar

The problem is `bar` always gets the standard output of `foo`. Not standard error.

I was trying like redirection.

% foo 2| bar

But this didn’t work. After some trial and error I found a way.

See the command bellow.

% foo 2>&1 1>/dev/null | bar

The extra part here is “2>&1 1>/dev/null“. What does it do? How does it work?

Well, `bar` takes output of `foo` not error. Keeping this in mind if I could redirect the standard error to standard output of `foo`, `bar` will get what it wants. But if I just redirect stderr to stdout, stdout will contain both original stdout content and new stderr content. To overcome this, I removed the original content of stdout at first then I do redirection.

1>/dev/null cleans the original content of standard output, then

2>&1 redirects standard error to standard output.

When `bar` is executed it has the input.



© 2008 by A K M Mokaddim


Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
Based on a work at talk.cmyweb.net.