Check my external ip linux

What *nix based command can I use to find my external IP?

Similar to a http://whatismyip.com lookup. It would obviously need to query a computer out there. Just wondering if anyone had a clever way to do it?

12 Answers 12

curl http://myip.dnsomatic.com 

This only works if you are using OpenDNS as your dns server.

If you aren’t, one of these should work:

dig +short myip.opendns.com @208.67.222.222 dig +short myip.opendns.com @208.67.220.220 dig +short myip.opendns.com @208.67.222.222 @208.67.220.220

+1 for using DNS to find out an IP (instead of http). You could also specify the dns server using its name e.g., @resolver1.opendns.com (for readability)

% stun -v stun.ekiga.net . MappedAddress = 88.189.152.187:18009

You can use curl to get the page from something like whatismyip and then get the pieces out. I used whatismyipaddress.com in this example. obviously the fields will differ with different services.

curl -s http://whatismyipaddress.com/ | grep LOOKUPADDRESS | awk '< print $4 >' 
lynx -dump http://www.pcmesh.com/ip-check.cgi | awk '/REMOTE_ADDR/' 

One must use OpenDNS’ servers to use this. You can query a certain DNS server with dig like that:

dig +short myip.opendns.com @208.67.222.222

the simplest way is: curl ifconfig.me

i just run a traceroute to somewhere on the internet and look for the hop out of our local network.

perhaps there’s a better way?

I love to use curl -4 icanhazip.com . The service is made to be used in scripts.

you can use the ifconfig command to list all interfaces and their associated IP address(es).

so, if you know your internet interface is ppp0, you can run

$ ifconfig ppp0 ppp0 Link encap:Point-to-Point Protocol inet addr:X.X.XX.X P-t-P:Y.Y.Y.Y Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1444 Metric:1 RX packets:198986 errors:0 dropped:0 overruns:0 frame:0 TX packets:122929 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 RX bytes:134195571 (127.9 MiB) TX bytes:17101701 (16.3 MiB)

X.X.X.X will be your IP address. Y.Y.Y.Y is the IP address of the next hop.

you can then postprocess the output of ifconfig with grep/awk/sed/cut/perl/whatever to extract just the IP.

another alternative, if you have the iproute tools installed, is to use the ip command. e.g.

$ ip addr list ppp0 21842: ppp0: mtu 1444 qdisc htb state UNKNOWN qlen 3 link/ppp inet X.X.X.X peer Y.Y.Y.Y/32 scope global ppp0

that’s probably easier to read and certainly easier to parse:

$ ip addr list ppp0 | awk '/inet/ ' X.X.X.X

— There is chance that any IP in ifconfig will not match real Internet IP (in case of biNAT) — In FreeBSD there is no iproute2 package, so ip command is not applicable

true, i don’t use NAT so it never occurred to me to. it just seemed «wrong» to me to use an external service for information you can query your own system about. even with NAT, i’d still prefer to figure out how to query the router doing the NAT (perhaps via an SNMP query) than to rely on an external service which may or may not be available when i need it.

Читайте также:  Linux add swap fstab

@Craig, Your approach is fine. However, its not an external IP unless you can reach it on that value from outside. You do have to rely on an external point for this answer. The idea is to find a reliable (at least more than your perimeter point) external point for the query. OpenDNS is a good point.

@nik: the only likely case where your border router’s IP isn’t the external IP is when you’re behind multiple layers of NAT. in which case, the correct solution is to switch to a non-brain-damaged service ASAP. also, the problem with fetching a URL to find out your IP is that it only tells you the IP of the host that actually fetches it — which may be a proxy that strips Via headers.

If you facing a private ip of your isp and use some of firewall like cisco ASA, then you may setting up DNS Client and try to resolve fqdn myip.opendns.com quary in OpenDNS resolver to now that public ip you have from ISP.

SomeASA# show ip address System IP Addresses: Interface Name IP address Subnet mask Method Vlan1 inside 10.10.10.1 255.255.255.248 CONFIG Vlan2 outside 192.168.1.2 255.255.255.0 CONFIG Current IP Addresses: Interface Name IP address Subnet mask Method Vlan1 inside 10.10.10.1 255.255.255.248 CONFIG Vlan2 outside 192.168.1.2 255.255.255.0 CONFIG SomeASA# ! - - Configure DNS client for OpenDNS resolver on ASA: dns domain-lookup outside dns server-group DefaultDNS name-server 208.67.222.222 name-server 208.67.220.220 domain-name example.com ! - - and try to ping myip.opendns.com SomeASA# ping myip.opendns.com Type escape sequence to abort. Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds: . Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms SomeASA# ! - - when DNS resolver return IP on fqdn myip.opendns.com request with IP what he that obtain from sources of ip address in request that arrives on resolver (in this example is resolver1.opendns.com [208.67.222.222] and resolver2.opendns.com [208.67.220.220]) SomeASA# show dns-hosts Host Flags Age Type Address(es) myip.opendns.com (temp, EX) 0 IP 1.1.1.1 SomeASA# 

Источник

Linux command for public ip address

and many more. But they all are giving me internal IP Address. Then I found some sites which provides API for this. Example : curl http://ipecho.net/plain; echo But I don’t want to rely on third party website service. So, is there any command line tool available to get external IP Address?

12 Answers 12

You can use this command to get public ip and private ip(second line is private ip; third line is public ip.)

It’s the cleanest solution, it’s sad that most of articles / answers using external resources to get IP instead of this simple solution

simplest of all would be to do : curl ifconfig.me

Yes, it works fine as I have mentioned in my question, But I do not want to depend on this third party service. What if that server stop this service in future?

You could use this script

# !/bin/bash # echo 'Your external IP is: ' curl -4 icanhazip.com 

But that is relying on a third party albeit a reliable one. I don’t know if you can get your external IP without asking someone/somesite i.e. some third party for it, but what do I know.

Читайте также:  Linux calculate cl install

This is doing the same thing as a command the -4 is to get the output in Ipv4

I would suggest you to use the command external-ip ( sudo apt-get install miniupnpc ) as it (I’m almost sure) uses upnp protocol to ask the router instead of asking an external website so it should be faster, but of course the router has to have upnp enabled.

It might not work on amazon because you might be using NAT or something for the server to access the rest of the world (and for you to ssh into it also). If you are unable to ssh into the ip that is listed in ifconfig then you are either in a different network or dont have ssh enabled.

ifconfig eth0 | awk '/inet / < print $2 >' | sed 's/addr://' 

That returns whatever is IP address assigned to your eth0 device. So if your server is hosted on Amazon, and was assigned an internal IP address, and since Amazon instances are using NAT, that’s what you’d see. If you want a sure reliable way of getting your external, public IP address, then use one of the external service like ifconfig.me.

This is the best I can do (only relies on my ISP):

ISP=`traceroute -M 2 -m 2 -n -q 1 8.8.8.8 | grep -m 1 -Eo '2\.7\.6\.5'` extIP=`ping -R -c 1 -t 1 -s 1 -n $ISP | grep RR | grep -m 1 -Eo '9\.8\.7\.9'` echo $extIP 

Or, the functionally same thing on one line:

ISP=`traceroute -M 2 -m 2 -n -q 1 8.8.8.8 | grep -m 1 -Eo '7\.6\.7\.5'` | ping -R -c 1 -t 1 -s 1 -n $ISP | grep RR | grep -m 1 -Eo '3\.7\.9\.1' 

to save it to a temporary & hidden file add > .extIP to the end of the last line, then cat .extIP to see it.

If your ISP’s address never changes (honestly i’m not sure if it would or not), then you could fetch it once, and then replace $ISP in line two with it

This has been tested on a mac with wonderful success. the only adjustment on linux that I’ve found so far is the traceroute «-M» flag might need to be «-f» instead and it relies heavily on the ping’s «-R» flag, which tells it to send back the «Record Route» information, which isn’t always supported by the host. But it’s worth a try!

the only other way to do this without relying on any external servers is to get it from curl’ing your modem’s status page. I’ve done this successfully with our frontier DSL modem, but it’s dirty, slow, unreliable, and requires hard-coding your modem’s password. Here’s the «process» for that:

curl http://[user]:[password]@[modem's LAN address]/[status.html] | grep 'WanIPAddress =' | grep -m 1 -Eo '5\.2\.1\.6' 

That fetches the raw html, searches for any lines containing «WanIpAddress mt24″>

Источник

Как узнать внешний ip из консоли

Внешний ip

Доброго дня дорогие читатели. Сегодня хочу затронуть тему о том как узнать внешний ip из консоли.

Есть много других вариантов как узнать свой внешний ip адрес, например, открыть браузер и зайти на сайт. Определяющий ваш внешний ip адрес сайтов достаточно много, найти их не составит труда. Но есть определенные моменты когда браузера нет в системе. Например, есть у вас Asterisk только с консольным доступом. Для настройки sip провайдера и привязки его к внешнему ip нужно его указать. Сеть в организации может быть большая и точно определить внешний ip адрес возможно лишь с сервера Asterisk.

Читайте также:  Зеркало обновлений nod32 linux

Можно привести и другой пример, сервер Windows Core или бесплатный Hyper-V, там тоже изначально только консоль. Давайте уже узнаем наш внешний ip адрес

Узнать внешний ip адрес из консоли Linux

Данный метод работоспособен для Ubuntu, Centos, Gentoo и прочих дистрибутивов

На случай если указанный выше сайт перестанет работать ниже приведу еще несколько рабочих вариантов

wget -qO- ipecho.net/plain curl ifconfig.me wget -O - -q ifconfig.me/ip
Узнать внешний ip адрес из консоли Windows
Nslookup

Определить ваш внешний адрес из консоли WIndows можно с помощью утилиты nslookup. Обычно nslookup используется для проверки работы DNS. Есть один хитрый способ, как с помощью данной утилиты узнать свой внешний ip адрес. Выполним следующую команду:

nslookup myip.opendns.com. resolver1.opendns.com

Мы выполнили запрос к серверу DNS под названием resolver1.opendns.com. Он в свою очередь должен был выдать нам ip адрес доменного имени myip.opendns.com. Но весь смысл в том, что DNS сервер resolver1.opendns.com настроен так, чтобы при запросе конкретного домена выдавать ip адрес с которого поступил запрос. Вот так без особых усилий мы узнали наш внешний ip.

Powershell

Можно использовать и Powershell. Будет удобно для дальнейшей работы с ip в ваших скриптах.

$ip = new-object System.Net.WebClient $ip.DownloadString("http://ipinfo.io/ip")

Источник

Check External IP using the Linux Command Line

Knowing the IP address of our computer is a basic task to establish network connections. But remember that there is also a public one with which we access the Internet, and doing it from the terminal can be an interesting task. Today, you will learn how to know the external IP address using the terminal.

In a typical network, the router uses a public IP address to identify it to the rest of the Internet. However, this same home router allows many devices to connect to this same router by assigning it another range of IP addresses that are known as private.

So, the device has a private IP address to communicate with the internal network, but the router uses a different one to access the Internet. Here is the difference between the private IP and the public IP.

To know the private IP from the terminal, it would be enough to use the ipconfig command. But in the case of the external or public IP, the process is a little different.

I will show you several ways to do it.

Check External IP using the Linux Command Line

The reality is that there are many commands and ways to do this process. The first way to do this is by using the dig command.

In this case, the dig command will make a request to a website that will provide us with the external or public IP address.

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

The output on the screen will simply be, the IP address you are looking for. For example:

Источник

Оцените статью
Adblock
detector