I have a vhost on my webserver whose sole and unique goal is to return the client IP adress:
petrus@bzn:~$ cat /home/vhosts/domain.org/index.php
<?php echo $_SERVER['REMOTE_ADDR']; echo "\n" ?>
This helps me troubleshoot networking issues, especially when NAT is involved. As such, I don't always have domain name resolution and this service needs to work even if queried by its IP address.
I'm using it this way:
petrus@hive:~$ echo "GET /" | nc 88.191.133.41 80
191.51.4.55
petrus@hive:~$ echo "GET /" | nc ydct.org 80
191.51.4.55
router#more http://88.191.133.41/index.php
88.191.124.254
However I found that it wasn't working from at least a computer:
petrus@seth:~$ echo "GET /" | nc ydct.org 80
petrus@seth:~$
petrus@seth:~$ echo "GET /" | nc 88.191.133.41 80
petrus@seth:~$
What I checked:
This is not related to ipv6:
petrus@seth:~$ echo "GET /" | nc -4 ydct.org 80
petrus@seth:~$
petrus@hive:~$ echo "GET /" | nc ydct.org 80
2a01:e35:ee8c:180:21c:77ff:fe30:9e36
netcat
version is the same (except platform, i386 vs x64):
petrus@seth:~$ type nc
nc est haché (/bin/nc)
petrus@seth:~$ file /bin/nc
/bin/nc: symbolic link to `/etc/alternatives/nc'
petrus@seth:~$ ls -l /etc/alternatives/nc
lrwxrwxrwx 1 root root 15 2010-06-26 14:01 /etc/alternatives/nc -> /bin/nc.openbsd
petrus@hive:~$ type nc
nc est haché (/bin/nc)
petrus@hive:~$ file /bin/nc
/bin/nc: symbolic link to `/etc/alternatives/nc'
petrus@hive:~$ ls -l /etc/alternatives/nc
lrwxrwxrwx 1 root root 15 2011-05-26 01:23 /etc/alternatives/nc -> /bin/nc.openbsd
It works when used without the pipe:
petrus@seth:~$ nc ydct.org 80
GET /
2a01:e35:ee8c:180:221:85ff:fe96:e485
And the piping works at least with a test service (netcat
listening on 1234/tcp and output to stdout)
petrus@bzn:~$ nc -l -p 1234
GET /
petrus@bzn:~$
petrus@seth:~$ echo "GET /" | nc ydct.org 1234
petrus@seth:~$
I don't know if this issue is more related to netcat
or Apache
, but I'd appreciate any pointers to troubleshoot this issue !
bzn
is the server,hive
is a working client andseth
is the client on which I have the issue.
Edit:
It works also with telnet
but telnet
doesn't allow piping.
2nd edit in answer to MickeyB:
Indeed host headers are not transmitted, but as seen with hive
host above, I've configured the default
vhost of Apache to behave like ydct.org
vhost:
petrus@bzn:/etc/apache2/sites-available$ cat default
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName 88.191.133.41
ServerAlias 2a01:e1b:1:132:1a0a:a9ff:fec8:f0a9
DocumentRoot /home/vhosts/ydct.org/
</VirtualHost>
However, it works with curl
!
petrus@seth:~$ curl ydct.org
2a01:e35:ee8c:180:221:85ff:fe96:e485
Output of apache2 -S
posted to http://pastebin.com/aSf446Jv
But I'd like to know why it doesn't work with netcat
...
In short, its the version of netcat you are running.
I tested the command string on my machines as follows:
Mac OS X Lion:
FreeBSD:
CentOS:
Debian (version 6)
Its not until I got to Ubuntu "lucid" (which is what you are apparently are running) is when I got this:
It appears that Ubuntu defaults to nc.openbsd rather that nc.traditional (which is the default on Debian). Once I use the traditional version, I received the desired output:
What is curious that both of your client machines have nc.openbsd, yet they different in behavior. Of course, it could be that one of them either has a different version of nc.openbsd or a different version of Ubuntu altogether. In either that, you can sync your versions of nc.openbsd or switch over to nc.traditional.
Works for me :)
If you are trying to hit the vhost you are doing so incorrectly. You need to send a Host: header to identify the virtual host which you want to use.
The best way of using your 'what is my IP' service would be curl. Hey look, I see the same problem on my end when I do what you're doing:
FIX YOUR VHOSTS!!!
Set a wait time and try again: