Get IP Address From Hostname


  1. 說明
    1. Python
    2. PowerShell

說明如何批次從伺服器 Hostname 取得伺服器的 IP Address,本次示範使用 Python 以及 PowerShell 😁

logo

說明

Python

import socket

datas = {
'web-servernode1': '',
'web-servernode2': '',
'sql-node1': '',
}

for server in datas:
  try:
    ip_address = socket.gethostbyname(server)
  except:
    ip_address = 'unknown'
  print(f"{server}: {ip_address}")
  datas[server] = ip_address

PowerShell

('web-servernode1', 'web-servernode2', 'sql-node1') | 
  % {RESOLVE-DNSNAME $_ } | 
  select name, ipaddress, namehost | 
  clip