Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

2 Answers 2

up vote 5 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: 192.168.0.100
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 10.10.0.1 port 3333 [tcp/*] accepted. I simply extract the ip address using grep.

share|improve this answer

Your Answer

 
discard

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.