7

Actually I want to make something like ifconfig.me functionality but only for my internal network. I see it the way something 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?

7

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
  • 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
0
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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