1

I know I got quite rusty when it comes to bash coding, especially the more elaborate needed trickery handling awk or sed parts.

I do have a script that logs the IP address currently in use for the interwebs.

It gets that by either using wget -q0 URL or lynx -dump URL.

The most easy one was a site that only returned the IP address in plain text and nothing else. Unfortunately that site no longer exists.

The code was simple as can be: IP=$(wget -qO - http://cfaj.freeshell.org/ipaddr.cgi) But alas! using the code returns nothing cause the site is gone, as lynx can tell us:

$ lynx -dump http://cfaj.freeshell.org/ipaddr.cgi

Looking up cfaj.freeshell.org
Unable to locate remote host cfaj.freeshell.org.
Alert!: Unable to connect to remote host.

lynx: Can't access startfile http://cfaj.freeshell.org/ipaddr.cgi

Some other sites I used to retrieve for the same purpose no longer work either.

And the one I want to use is a German speaking one, not that I care one way or the other, it could be in Greek or Mandarin for all I care. I want only to have the IP address itself extracted, but like I said, my coding skills got rusty.

Here is the relevant area of what lynx -dump returns

   [33]powered by

Ihre IP-Adresse lautet:

   178.24.x.x
   Ihre IPv6-Adresse lautet:

   Ihre System-Informationen:

when running it as follows:

lynx -dump https://www.wieistmeineip.de/

Now, I need either awk or sed to find the 178.24.x.x part. (I know it can be done with python or Perl as well, but both are not part of a standard setting of my Linux, while awk and sed are.)

Since the script is there to extract the IP address, one needs to do the following either via sed or awk:

  • Search for "Ihre IP-Adresse lautet:"
  • Skip the next line.
  • Skip the whitespace at the beginning
  • Only return what is left of that line (without the lf at the end).

In the above example (that shows only the relevant part of the lynx dump, the whole dump is much larger but all above and below is irrelevant.) it would be "178.24.x.x" that should be returned.

Any help greatly appreciated to get my log-ip script back into working order.

Currently I have collected some other working URLs that report back the own internet IP. Any of these can also be used, but the area around the reported IP will differ from the above example. These are:

https://meineipinfo.de/
http://www.wie-ist-meine-ip.net/
https://www.dein-ip-check.de/
https://whatismyipaddress.com/
https://www.whatismyip.org/
https://www.whatismyip.net/
https://mxtoolbox.com/whatismyip/
https://www.whatismyip.org/my-ip-address
https://meineipadresse.de/

Even duckduckgo returns the IP address when e.g. asked this: https://duckduckgo.com/?q=ip+address&ia=answer

At least I know of no way of getting the own IP address when using the internet without retrieving an outside URL that reports that very IP address back to me.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Rava
  • 53
  • 7

3 Answers3

4

You can do:

wget -O - v4.ident.me 2>/dev/null && echo
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • Thanks, I use it in my script like so: IP=$(wget -O - v4.ident.me 2>/dev/null) – Rava Feb 05 '20 at 12:47
  • @Rava: it's recommended to use lowercase variable names in scripts https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization – Arkadiusz Drabczyk Feb 05 '20 at 12:49
  • You are correct on that. And when we also consider shellcheck then I have lots of work to do just changing my otherwise working scripts. – Rava Feb 05 '20 at 13:14
1

So, if you have a VM in some cloud provider you can solve this easily. I wrote some small Go app than echoes back an HTTP request. For instance :

 $ curl 167.99.63.182:8888
Method -> 
    GET

Protocol -> 
    HTTP/1.1

Headers -> 
    User-Agent: [curl/7.54.0]
    Accept: [*/*]

Content length (in Bytes) -> 
    0


Remote address -> 
    179.XXXXX 

Payload
####################

####################

Where remote address is the address which the app received, hence, your IP.

And in case you are wondering, yes, 167.99.63.182 is the IP of the server and you can curl it right now and check it. I am disclosing the IP as anyway I get bombarded by brute force attacks for as long as I can remember and the machine does not have anything worth the break through.

Matias Barrios
  • 4,674
  • 3
  • 22
  • 49
0

Not exactly without relying on external services, but you could use dig to reach out to the resolver at opendns.com:

dig +short myip.opendns.com @resolver1.opendns.com

I think this is easier to integrate to a script.

kkubina
  • 147
  • 2
  • "dig: command not found." Seems dig is by default not installed on my system… – Rava Feb 05 '20 at 12:31
  • If you have `nslookup` or `host` you can use those instead. If you have Python or Perl and can install a DNS library, you can write a simple client. – tripleee Feb 05 '20 at 12:39
  • tripleee, currently I have none of these 4 installed. – Rava Feb 05 '20 at 12:45