This script will take a file of hostnames and convert each one to its corresponding IP address
Python:
import sys, getopt, socket, inspect,os
def main(argv):
InputFile = ''
OutputFile = ''
try:
opts, args = getopt.getopt(argv,"i:o:",["InputFile=","OutputFile="])
except getopt.GetoptError:
print ('test.py -i <InputFile> -o <OutputFile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('test.py -i <InputFile> -o <OutputFile>')
sys.exit()
elif opt in ("-i", "--ifile"):
InputFile = arg
elif opt in ("-o", "--ofile"):
OutputFile = arg
Path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
reader=open(Path + InputFile,"r")
out=open(Path + OutputFile,"w")
for host in reader.read().split('\n'):
try:
ip=socket.gethostbyname(host)
except:
ip="N/A"
out.write(ip)
out.write("\n")
out.close()
reader.close()
if __name__ == "__main__":
main(sys.argv[1:])
Last edited: