Get my public IP using netcat
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 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开发者_如何学Python.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
Since whatismyip.com's automation is down, here's an alternative:
$ echo GET / | nc icanhazip.com 80
...which will output your IP.
create a file called 'script'
put this in the contents
GET /automation/n09230945.asp HTTP/1.0
Host: www.whatismyip.com
Make sure there are 2 empty linefeeds on the bottom (this editor wont show them)
then run
cat script | nc www.whatismyip.com 80 | tail -1
There is your ip... you can put it in a variable like
IP=`cat script | nc www.whatismyip.com 80 | tail -1`
echo $IP
This will return just the ip address, no fuss
You can use netcat to send a working, canned HTTP request:
command
cat httpreq.txt | nc www.whatismyip.com 80
httpreq.txt (make sure to end the file with at least two blank lines)
GET / HTTP/1.0
Host: www.whatismyip.com
User-Agent: netcat
Referer: http://www.whatismyip.com/
<<don't forget your blank lines above here>>
...but you will still need some mechanism to parse the HTTP response.
Even if you can find a "what is my IP" web service that returns only the unadorned IP address in plain text (freeing you from having to parse HTML), you will still need to parse off the HTTP response headers.
What facilities do you have that might allow you to do some string parsing? BASH? sed? awk?
Edit: looks like superfro found a plain-text "what is my ip" service, and an easy way to parse off the HTTP response headers.
(echo GET /; echo) |nc www.whatismyipaddress.com 80 | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
if you have sed on your box, you can try
echo -e "GET /automation/n09230945.asp HTTP/1.1\r\nHost: www.whatismyip.com\r\n" | nc -w1 www.whatismyip.com 80 | sed -ne '$p'
精彩评论