External IP of Internal Server

8,920

I think the only way you're gonna do this is with external services. You can use the system.Net.WebClient Powershell library along with the website http://icanhazip.com, which returns nothing but your external IP address.

$obj = New-Object system.Net.WebClient;
$ip = $obj.downloadString("http://icanhazip.com")
Share:
8,920

Related videos on Youtube

p.s.w.g
Author by

p.s.w.g

Updated on September 18, 2022

Comments

  • p.s.w.g
    p.s.w.g over 1 year

    I've seen this question, but it doesn't address my current issue (it's about getting the external IP address of the current machine):
    Windows command that returns external IP

    I'm on a network that has a number of external IP addresses. I need to determine the external address of an internal machine that may use a different external address. Obviously, I could go to a machine outside the network and ping it from there, but how do I do this from inside the network?

    I would like this to be incorporated into an automated process, so a DOS command or PowerShell script would be preferred.

    • Ashtray
      Ashtray over 10 years
      Isn't it set using NAT on your router? The machine isn't aware of any external address. Only router is
    • p.s.w.g
      p.s.w.g over 10 years
      @Ashtray Is it possible to do something like ping the server via another external server / service?
    • Ashtray
      Ashtray over 10 years
      It would be easier to answer if we'd know what are you trying to achieve/test/whatever :)
    • p.s.w.g
      p.s.w.g over 10 years
      @Ashtray I want to automatically build a list of IP address(es) that a customer will need to access when we install a particular product on their machine. Some customers are behind a firewall, so we can't determine it from the machine we're installing on (we would already need to know which IP's need to be unblocked). Rather, I'd like our build server to generate the list dynamically by resolving the server names to external IP addresses.
  • p.s.w.g
    p.s.w.g over 10 years
    How can I get this to return the IP address of another machine?
  • trpt4him
    trpt4him over 10 years
    You'd have to execute it from THAT machine. Or, you could run Powershell on that machine remotely by using PsExec. technet.microsoft.com/en-us/sysinternals/bb897553.aspx
  • Scott - Слава Україні
    Scott - Слава Україні over 10 years
    I agree: you would need to be on the other machine in order to discover its external IP address, so the other question that you cited is relevant.
  • barlop
    barlop over 10 years
    @Scott it's not the ip of that machine it's the ip of the network interface of the internet side of the NAT router. not of that machine at all.
  • trpt4him
    trpt4him over 10 years
    @barlop, the OP said there are multiple external IPs. Presumably different machines could have different external IPs.
  • p.s.w.g
    p.s.w.g over 10 years
    @Scott If I can find a service like this that I can use to ping another server, e.g. http://icanhazip.com?target=myserver.mydomain.com, that would work just fine.
  • trpt4him
    trpt4him over 10 years
    Well, if you have a hostname that reliably points to that IP, then you can just use nslookup to get the IP.
  • p.s.w.g
    p.s.w.g over 10 years
    @trpt4him yup nslookup is exactly what I needed.
  • Sander Steffann
    Sander Steffann over 10 years
    If you read the comments to the accepted answer you'll notice that the this is not the answer the OP was looking for.
  • Eun
    Eun over 9 years
    as an alternative: ifcfg.me
  • Eun
    Eun over 9 years
    Should work with ifcfg.me too, just do curl ifcfg.me, btw it also supports ftp, nslookup etc.
  • barlop
    barlop over 9 years
    @Eun oh I see, it detects user agent so chrome returns an html page and curl - an ip. good find
  • fixer1234
    fixer1234 over 8 years
    Just a download link is not an answer even if it is useful in solving the question. It's likely to attract downvotes.