[Shell] Retrieving your public IP

Last update on March 9, 2009 09:39 AM by deri58
Published by deri58

[Shell] Retrieving your public IP





For several reasons, we may need to recover its public IP address in a script.
The problem that arises when you are on a LAN, is to find the IP without human intervention.

Implementation


The easiest way is to use a simple command seeking IP in a web page chosen:

Le moyen le plus simple que j'ai trouvé jusqu'à présent est une simple commande recherchant une ip dans une page web choisie :
GET www.myip.org | sed -nre 's/^.* (([0-9]{1,3}\.){3}[0-9]{1,3}).*$/\1/p'
ou
lynx --source www.myip.org | sed -nre 's/^.* (([0-9]{1,3}\.){3}[0-9]{1,3}).*$/\1/p'

GET


The GET command (lwp-request) is foundin libwww of Perl (package: perl-libwww-perl-version).
As its name suggests it is a GET on a page, it returns the source code of the page.

Lynx


Lynx is a text-based browser in most Linux distributions, easier to install than lwp-request(Perl) if necessary.

Sed


The sed command:

parameters
  • -n: small display lines given (related to the parameter"p")
  • -r: regular expressions support extended
  • -e: expression
  • s///: indicates a substitution
  • p: the result is displayed (in connection with the "n")
  • the parameter
  • ^.*(parameter_ip).*$ search for a line containing an IP address
  • \1: what is in brackets can be addressed later in the expression using \1.



The line containing the IP address is replaced by the IP address itself;)
Best answers for « Retrieving your public IP » in :
How to detect your public IP address? Show How to detect your public IP address? The ipconfig for Windows also known as ifconfig under Linux display your IP address. However behind a router that makes NAT, this will not surely correspond to your public IP address which however...
[RegExp]Retrieving an IP address Show [RegExp]Retrieving an IP address Purpose Implementation Explanations Purpose Recovering an IP address from text file, log files, web pages, etc. Implementation The following regular expression combined with a specific command...
Conversion of an IP address 32-bit Show Conversion of an IP address 32-bit We will make a ping kioskea.net in view retrieve the IP address (in bold) $ Ping-c1 kioskea.net PING kioskea.net (194,169,240,130) 56 (84) data bytes. 64 bytes from www.kisokea.org...
Command to detect your IP address! ShowCommand to detect your IP address! Command for Windows 2000 / XP / Vista Command for Windows 95/98 Command for Linux IP address (Internet Protocol) is a numerical identification to detect your computer on a network. It is used to...
Publish a PDF file on your website ShowPublish a PDF file on your Website Displaying a link to a PDF on your Website Displaying the PDF on your website Displaying a link to a PDF on your Website Proposing a PDF link on your own website is an operation that can seem...
Having a static IP address under Ubuntu 8.10 ShowHaving a static IP address under Ubuntu 8.10 To have a fixed IP persistent: Configure manually the interfaces For a fix IP (10.0.0.1 on your eth1 interface) use: To make use of dynamic IP (e.g eth0): DNS NetworkManager is a...
Download IP Traffic Monitor ShowIP Traffic Monitor monitors all network connections on the computer where it is installed. The program shows you detailed information, such as the remote IP address, host name if available, details of inbound and outbound traffic. Advantage It...
IP Address ShowWhat is an IP address? Computers communicate over the Internet using the IP protocol (Internet Protocol), which uses numerical addresses, called IP addresses, made up of four whole numbers (4 bytes) between 0 and 255 and written in the format...
NAT- Network address translation, port forwarding and port trigg ShowThe principle of NAT Network address translation or NAT was developed in order to respond to the shortage of IP addresses with IPv4 protocol (in time the IPv6 protocol will respond to this problem). In fact, in IPv4 addressing the number of...
TCP/IP ShowWhat does TCP/IP mean? TCP/IP is a suite of protocols. The acronym TCP/IP means "Transmission Control Protocol/Internet Protocol" and is pronounced "T-C-P-I-P". It comes from the names of the two major protocols in the suite of protocols, i.e....