Get my external IP

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I want to setup a cron job that retrieves a file from a remote server and looks for my IP in the file.
The system the cron job will be running on is behind a router.

My first thought is to wget whatsmyip.com and extract the IP from there, but surely there has to be a more elegant way ?
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Your router should know its external IP. If you can find a way to communicate with it (SNMP?) you might be able to extract it from there. Instead of going to an external server.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Looks like the simplest thing to do is going to be fetch the routers DHCP status page from within the network with wget and pulling the IP from there.

That will be much simpler for me than spending a day or so studying SNMP.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

SNMP is a pain in the butt. I wouldn't wish that on you.

Also, in case you didn't know, whatismyip.org returns your IP in simple plaintext. No parsing necessary. Still, fetching it from your router is a much better choice, IMO.
  • nice123
  • Newbie
  • Newbie
  • nice123
  • Posts: 9

Post 3+ Months Ago

Use ifconfig.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Assuming ifconfig doesn't just return the IP my router assigns to me, how would I use ifconfig to do this ?
  • nice123
  • Newbie
  • Newbie
  • nice123
  • Posts: 9

Post 3+ Months Ago

joebert wrote:
Assuming ifconfig doesn't just return the IP my router assigns to me, how would I use ifconfig to do this ?


ifconfig | grep -A
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

ifconfig just seems to give me the intranet address my router assigns to the server, not the one assigned by my ISP to the router.
  • Daemonguy
  • Moderator
  • Web Master
  • User avatar
  • Posts: 2700
  • Loc: Somewhere outside the box in Sarasota, FL.

Post 3+ Months Ago

http://whatismyipaddress.com/

go to that from any machine on your network. Instead of the internal IP, it will show the address assigned by your ISP which is your external NAT.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Here's the monster I'm working with at the moment.

BASH Code: [ Select ]
scp user@domain.com:/home/user/file.txt file.txt;\
wget -q --output-document=- --http-user=user --http-password=pass http://192.168.1.1/status.html |\
tr -d '\n' |\
grep -P -o '<!--WAN head-->.+IP Address:.+\d+\.\d+\.\d+\.\d+.+Subnet' |\
grep -P -o '\d+\.\d+\.\d+\.\d+' |\
cmp - file.txt
  1. scp user@domain.com:/home/user/file.txt file.txt;\
  2. wget -q --output-document=- --http-user=user --http-password=pass http://192.168.1.1/status.html |\
  3. tr -d '\n' |\
  4. grep -P -o '<!--WAN head-->.+IP Address:.+\d+\.\d+\.\d+\.\d+.+Subnet' |\
  5. grep -P -o '\d+\.\d+\.\d+\.\d+' |\
  6. cmp - file.txt


I would like to get this thing to be quiet unless the contents of the files differ, in which case it will print something for crons MAILTO user to get.

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 59 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.