Skip to content

Instantly share code, notes, and snippets.

@wsummerhill
Created November 16, 2022 17:32

Revisions

  1. wsummerhill created this gist Nov 16, 2022.
    14 changes: 14 additions & 0 deletions get-IPs-by-hostname.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    #Quick Python script to loop through hostnames from file and resolve their IPs
    import socket, sys

    hostsFile = sys.argv[1]

    with open(hostsFile) as file:
    for line in file:
    try:
    host = line.strip()
    ip = socket.gethostbyname(host)
    print(f'Host: {host} = {ip}')
    except socket.gaierror as e:
    #print(f'Error: IP not found for {host}')
    pass