I have a very basic Linux server running on an IP camera (Busybox). I’d like to get the public IP of that camera, which is located behind a router.
It has no “wget” nor “traceroute”, and “ping” is just answering “alive” or “not alive”…
The only one available is a basic “netcat” :
nc
BusyBox v1.1.3 (2009.12.07-16:16+0000) multi-call binary
Usage: nc [OPTIONS] [IP] [port]
Netcat opens a pipe to IP:port
Options:
-l listen mode, for inbound connects
-p PORT local port number
-i SECS delay interval for lines sent
-w SECS timeout for connects and final net reads
-4 Use IPv4 (default)
-6 Use IPv6
-D DSCP set IP DSCP field
-P PRIO set VLAN user-priority
Provided my ISP allows outgoing HTTP, is it possible to use netcat in order to get the public IP response from a site like http://www.obtainip.com or similar ?
GREAT and many thanks to both of you guys as I did not feel comfortable with nc at all.
Here are the 2 working ways :
echo "GET /automation/n09230945.asp HTTP/1.0" > http_req2.txt
echo "Host: www.whatismyip.com" >> http_req2.txt
echo "" >> http_req2.txt
echo "" >> http_req2.txt
IP2=$(cat http_req2.txt | nc www.whatismyip.com 80 | tail -n 1)
echo $IP2
YEAH !
and more strange but… it also works :
echo "GET / HTTP/1.0" > http_request.txt
echo "Host: www.whatismyip.com" >> http_request.txt
echo "User-Agent: netcat" >> http_request.txt
echo "Referer: http://www.whatismyip.com/" >> http_request.txt
echo "" >> http_request.txt
echo "" >> http_request.txt
IP=$(cat http_request.txt | nc www.whatismyipaddress.com 80 | tail -n 1)
echo $IP
YEAH again!
EDIT
Of course, this can be scripted better like suggested :
echo -e "GET /automation/n09230945.asp HTTP/1.0\n"Host: www.whatismyip.com\n\n" | nc www.whatismyipaddress.com 80 | tail -n 1
create a file called ‘script’
put this in the contents
Make sure there are 2 empty linefeeds on the bottom (this editor wont show them)
then run
There is your ip… you can put it in a variable like
This will return just the ip address, no fuss