Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving public IP address to variable or file 1

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
GB
Hi all

Wish to get the public IP address of a Ubuntu server running on a LAN and either save it to a file or a system variable for use in a CRONTAB script

The command used is:-

Code:
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 'S/<.*$//'

This displays the IP address in the command window.

By saving it to either a file or variable, it would mean that a suitable script might be created to check for changes to a dynamic IP address.

If a change is detected, the DNS servers at ZoneEdit.com could be updated.

I am aware of ddclient and have been using it, but for some reason it no longer works on either main or backup server.

Any ideas would be appreciated.

TIA

FAQ184-2483​
Chris [pc2]
PDFcommander.com
motrac.co.uk
 
Your script does work, but you need to change the S in the second script to an s (sed doesn't recognize S). Then you can simply run the following to save the output to a file:

Code:
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' > file_to_save

It should be possible to export or echo this into a variable, but I am not familiar enough with shell script syntax to tell you how to do this off hand.

 
Update: use the following

variable=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`

Then you variable will be accessible via $variable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top