Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Actually I want to make something like ifconfig.me functionality but only for my internal network. I see it the way smth on server listens some port and send ip of connected remote machine.

Seems nc is a great utility for my issue (also I haven't any php/python/whatever on server. just only shell and standard unix tools).

I can see remote ip and dns name if I launch nc -vv and connect to it from remote system, but I can't find the way to send them to remote host.

Or maybe I chose too strange path and there is another simpler solution?

share|improve this question
up vote 6 down vote accepted

You could use something like this:

while true; do nc -lvp 1337 -c "echo -n 'Your IP is: '; grep connect my.ip | cut -d'[' -f 3 | cut -d']' -f 1" 2> my.ip; done

nc will be executed in endless loop listening on port 1337 with verbose option that will write information about remote host to stderr. stderr is redirected to file my.ip. Option -c for nc allows to execute something to "handle" connection. In this case we will next grep for IP addres from my.ip file.

pbm@lantea:~$ curl http://tauri:1337
Your IP is:
share|improve this answer
Just wow! Never thought that it is possible to redirect output to file and read it in command execution. Thank you very much. – rush Mar 22 '12 at 18:11
while true; do nc -l -v 3333 2> status | while read msg; do ip=$(cat status | grep -o -P '(?<=from).*(?=port)') && notify-send "$ip" "$msg" -i gtk-network; done ; done

Redirect stderr to a file called status; then it reads ip of remote connector from the file; finally shows the ip in notify-osd message.

nc remote-connector screenshots

Note: when someone connects to the same port from another computer nc gerneally puts this to stderr: connection from port 3333 [tcp/*] accepted. I simply extract the ip address using grep.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.