Get the IP of a server through powershell when it doesnt respond to a ping

夙愿已清 提交于 2019-12-11 02:43:18

问题


Due to firewall constraints, the servers I deal with do not reply to a ping, but do give their IP address

for eg, if I do :

ping myhost.com

I get

Pinging myhost.com [192.1.1.1] with 32 bytes of data:
Request Timed Out.

How can I just get the IP of the server from powershell? I know it can be done various ways if a server responds to a ping (Test-Connection or Ping-Host) , but I cant figure it when it ignores ping requests


回答1:


You can use the System.Net.Dns.GetHostAddresses method. This method will return an array of System.Net.IPAddress objects.

Examples of usage:

$ipAddresses = [System.Net.Dns]::GetHostAddresses('google.com')

List all addresses:

$ipAddresses | %{$_.IPAddressToString}

Grab the first address in the array:

$ipAddresses[0].IPAddressToString



回答2:


You can try and resolve it by DNS:

[System.Net.Dns]::GetHostByName('google.com').AddressList


来源:https://stackoverflow.com/questions/12808161/get-the-ip-of-a-server-through-powershell-when-it-doesnt-respond-to-a-ping

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!