简体   繁体   English

Linux bash脚本获取自己的互联网IP地址

[英]Linux bash script to get own internet IP address

I know I got quite rusty when it comes to bash coding, especially the more elaborate needed trickery handling awk or sed parts.我知道我在 bash 编码方面非常生疏,尤其是需要更复杂的技巧处理 awk 或 sed 部分。

I do have a script that logs the IP address currently in use for the interwebs.我确实有一个脚本可以记录当前用于互联网的 IP 地址。

It gets that by either using wget -q0 URL or lynx -dump URL.它通过使用 wget -q0 URL 或 lynx -dump URL 来获取。

The most easy one was a site that only returned the IP address in plain text and nothing else.最简单的是一个网站,它只以纯文本形式返回 IP 地址,没有其他任何内容。 Unfortunately that site no longer exists.不幸的是,该站点已不存在。

The code was simple as can be: IP=$(wget -qO - http://cfaj.freeshell.org/ipaddr.cgi) But alas!代码很简单: IP=$(wget -qO - http://cfaj.freeshell.org/ipaddr.cgi)但是唉! using the code returns nothing cause the site is gone, as lynx can tell us:使用代码不会返回任何内容,因为该站点已消失,正如 lynx 可以告诉我们的那样:

$ 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.我只想提取 IP 地址本身,但就像我说的那样,我的编码技能生疏了。

Here is the relevant area of what lynx -dump returns这是 lynx -dump 返回的相关区域

   [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.xx part.现在,我需要 awk 或 sed 才能找到 178.24.xx 部分。 (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.) (我知道它也可以用 python 或 Perl 来完成,但两者都不是我的 Linux 标准设置的一部分,而 awk 和 sed 是。)

Since the script is there to extract the IP address, one needs to do the following either via sed or awk:由于脚本用于提取 IP 地址,因此需要通过 sed 或 awk 执行以下操作:

  • Search for "Ihre IP-Adresse lautet:"搜索“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).只返回该行剩下的内容(最后没有 lf)。

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.xx" that should be returned.在上面的示例中(仅显示 lynx 转储的相关部分,整个转储要大得多,但上下所有内容都无关紧要。)应该返回“178.24.xx”。

Any help greatly appreciated to get my log-ip script back into working order.非常感谢让我的 log-ip 脚本恢复正常工作的任何帮助。

Currently I have collected some other working URLs that report back the own internet IP.目前我已经收集了一些其他的工作 URL,这些 URL 会报告自己的互联网 IP。 Any of these can also be used, but the area around the reported IP will differ from the above example.也可以使用其中任何一个,但报告 IP 周围的区域将与上述示例不同。 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 eg asked this: https://duckduckgo.com/?q=ip+address&ia=answer当问到这个问题时,甚至duckduckgo 也会返回IP 地址: 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.至少我知道在使用 Internet 时无法获取自己的 IP 地址,而无需检索向我报告该 IP 地址的外部 URL。

你可以做:

wget -O - v4.ident.me 2>/dev/null && echo

So, if you have a VM in some cloud provider you can solve this easily.因此,如果您在某个云提供商处拥有 VM,则可以轻松解决此问题。 I wrote some small Go app than echoes back an HTTP request.我写了一些小的 Go 应用程序,而不是回显 HTTP 请求。 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.其中远程地址是应用程序收到的地址,因此是您的 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.如果您想知道,是的,167.99.63.182 是服务器的 IP,您可以立即卷曲并检查它。 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.我正在公开 IP,因为只要我记得,我就会受到蛮力攻击的轰炸,而且机器没有任何值得突破的东西。

Not exactly without relying on external services, but you could use dig to reach out to the resolver at opendns.com:并非完全不依赖外部服务,但您可以使用 dig 与 opendns.com 上的解析器联系:

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

I think this is easier to integrate to a script.我认为这更容易集成到脚本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM