using text file to get ip address from hostname
Dan Katorza
dkatorza at gmail.com
Sat Sep 15 12:20:42 EDT 2012
בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת Dan Katorza:
> hello ,
>
>
>
> i'm new to Python and i searched the web and could not find an answer for my issue.
>
>
>
> i need to get an ip address from list of hostnames which are in a textfile.
>
>
>
> this is what i have so far
>
> --------------------------------------------------------------------------
>
> #!/usr/bin/env python
>
> #Get the IP Address
>
>
>
> import socket
>
> hostname = 'need it to read from a text file'
>
> addr = socket.gethostbyname(hostname)
>
> print 'The address of ', hostname, 'is', addr
>
>
>
> ---------------------------------------------------------------------------
>
>
>
> any idea ?
>
> sorry for my english
>
>
>
> thanks.
hello again friends,
thanks for everyone help on this.
i guess i figured it out in two ways.
the second one i prefer the most.
i will appreciate if someone can give me some tips.
thanks again
so...
-------------------------------------------------------------
First
-------------------------------------------------------------
#!/usr/bin/env python
#Get the IP Address
print("hello, please enter file name here >"),
import socket
for line in open(raw_input()):
hostname = line.strip()
print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostname)))
------------------------------------------------------------
second
------------------------------------------------------------
#!/usr/bin/env python
#Get the IP Address
import os
print("Hello, please enter file name here >"),
FILENAME = raw_input()
if os.path.isfile(FILENAME):
print("\nFile Exist!")
print("\nGetting ip from host name")
print("\n")
import socket
for line in open (FILENAME):
hostname = line.strip()
print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostname)))
else:
print ("\nFinished the operation")
else:
print ("\nFIle is missing or is not reasable"),
~
More information about the Python-list
mailing list