Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > using text file to get ip address from hostname

Reply
Thread Tools

using text file to get ip address from hostname

 
 
dkatorza@gmail.com
Guest
Posts: n/a
 
      09-12-2012
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.
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      09-12-2012
On Thu, Sep 13, 2012 at 12:24 AM, <> wrote:
> 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 sounding like homework, so I'll just give you a basic pointer.

You have there something that successfully resolves one hostname to an
IP address. Now you want to expand that to reading an entire file of
them and resolving them all. Presumably you need to produce a list of
IP addresses; check the question as to whether you need to create a
file, or output to the screen, or something else.

What you want, here, is to open a file and iterate over it. The most
convenient way would be to have one hostname per line and iterate over
the lines of the file. Check out these pages in the Python docs
(you're using Python 2 so I'm going with Python 2.7.3 documentation):

Opening a file:
http://docs.python.org/library/functions.html#open
Ensuring that it'll be closed when you're done:
http://docs.python.org/reference/com...with-statement
Looping over an iterable:
http://docs.python.org/tutorial/cont...for-statements

See where that takes you; in fact, all of
http://docs.python.org/tutorial/ is worth reading, if you haven't
already.

Have fun, enjoy Python!

ChrisA
 
Reply With Quote
 
 
 
 
dkatorza@gmail.com
Guest
Posts: n/a
 
      09-12-2012
בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת dkat...@gmail.com:
> hello ,
>
>
>
> i'm new to Python and i searched the web and could not find an answer formy 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.


thank you ChrisA
it's not really homework, i found a lab exercise on the web and i;m trying to study with it. maybe not the most efficient way.

i have a file with hostnames ordered line by line.

i will check the sources and will get back with outcome.

once again , Thanks,
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      09-12-2012
On 9/12/2012 10:41 AM, wrote:


> it's not really homework, i found a lab exercise on the web
> and i;m trying to study with it. maybe not the most efficient way.
>
> i have a file with hostnames ordered line by line.


Key fact for this exercise: open files are iterable.
So your top level structure is...

for line in open(<hostname file>):
<process hostname in line>

--
Terry Jan Reedy

 
Reply With Quote
 
Jason Friedman
Guest
Posts: n/a
 
      09-13-2012
> 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


$ cat hostnames
google.com
microsoft.com
facebook.com

$ python3
Python 3.2.3 (default, May 3 2012, 15:51:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> for line in open("hostnames"):

.... hostname = line.strip()
.... print("IP address for {0} is {1}.".format(hostname,
socket.gethostbyname(hostname)))
....
IP address for google.com is 74.125.225.33.
IP address for microsoft.com is 64.4.11.37.
IP address for facebook.com is 69.171.237.16.
 
Reply With Quote
 
Dan Katorza
Guest
Posts: n/a
 
      09-15-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 formy 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(hostnam e)))

------------------------------------------------------------
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(hostnam e)))
else:
print ("\nFinished the operation")
else:
print ("\nFIle is missing or is not reasable"),
~
 
Reply With Quote
 
Hans Mulder
Guest
Posts: n/a
 
      09-15-2012
On 15/09/12 18:20:42, Dan Katorza wrote:
> בתאריך יום רביעי, 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 >"),


Instead of printing this string, you can pass it as the
argument to raw_input:

for line in open(raw_input("hello, please enter file name here> ")):

Cosmetically, I'd prefer a space after the '>'.

> import socket


PEP 8 recommends putting all imports at the top of the file, like you
do in your second script.

> for line in open(raw_input()):
> hostname = line.strip()
> print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostnam e)))


To my mind, this line does two things: it finds the IP address and
prints it. I think it would be more readable to do these on separate
lines:

ip_address = socket.gethostbyname(hostname)
print("IP address for {0} is {1}.".format(hostname, ip_address)

This forces you to find a good name for the value returned
by gethostbyname, which I consider a Good Thing (tm).

PEP 8 recommends that lines should not be longer than 80
characters. Your line is longer than that; splitting it
in two conceptual steps nicely solves that issue as well.

> ------------------------------------------------------------
> second
> ------------------------------------------------------------
> #!/usr/bin/env python
> #Get the IP Address
>
> import os
>
> print("Hello, please enter file name here >"),
> FILENAME = raw_input()


PEP 8 recommends all upper case for constants, for example
socket.IPPROTO_IPV6. The filename here is not a hard-wired
constant, so it should be in lower case.

> if os.path.isfile(FILENAME):


Your first script allowed me to enter "/dev/tty" at the prompt,
and then type a bunch of hostnames and an end-of-file character.
This script reports "FIle is missing or not reasable", because
/dev/tty is not a regular file. I think the message should be
"File is missing or not readable or not a regular file".

I'm always annoyed when I get an error message with several
"or"s in it. I prefer programs that figure out which of the
three potential issues is the case, and mention only one cause.

> 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(hostnam e)))
> else:
> print ("\nFinished the operation")
> else:
> print ("\nFIle is missing or is not reasable"),


You don't want a comma at the end of this line: it messes
up the next shell prompt.

Also, this line a rather far away from the test that triggers it.

How about:

filename = raw_input("Hello, please enter file name here> ")
if not os.path.isfile(filename):
if not os.exist(filename):
print("\nFile {} does not exist")
else:
print("\nFile {} is not a regular file")
sys.exit(1)

print("\nFile {} exists", filename)
# etc.

Or you could skip the whole 'os' thing and use a try/except
construct instead:

#!/usr/bin/env python
#Get the IP Address

import sys, socket

filename = raw_input("Hello, please enter file name here> ")
try:
infile = open(filename)
except EnvironmentError as e:
print(e)
sys.exit(1)

print("\nFile {} exists!".format(filename))
print("\nGetting IP addresses for hosts")
print("\n")
for line in infile:
hostname = line.strip()
try:
ip_address = socket.gethostbyname(hostname)
except EnvironmentError as e:
print("Couldn't find IP address for {}: {}".format(hostname, e))
continue
print("IP address for {0} is {1}.".format(hostname, ip_address))
else:
print ("\nFinished the operation")


Using try/except has the advantage that it will correctly
report issues you didn't think about (for example, if file
exists, but you don't have permission to read it).


Hope this helps

-- HansM



 
Reply With Quote
 
Dan Katorza
Guest
Posts: n/a
 
      09-15-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 formy 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.


Hi Hans,
thank you very much for the tips.
as i mentioned before I'm new to python and I'm trying to learn it step by step.
thank you for showing me other ways, i will explore them.

 
Reply With Quote
 
Dan Katorza
Guest
Posts: n/a
 
      09-19-2012
בתאריך יום ראשון, 16 בספטמבר 2012 01:43:31 UTC+3, מאת Dan Katorza:
> בתאריך יום רביעי, 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.

>
>
>
> Hi Hans,
>
> thank you very much for the tips.
>
> as i mentioned before I'm new to python and I'm trying to learn it step by step.
>
> thank you for showing me other ways, i will explore them.


Hello again,
I have another question and i hope you will understand me..
Is there any option where you can set the program to go back to lets say the top of the code?
I mean if the program finished the operation and i want to stay in the program and go back ro the start.
after any operation i want the option to do it again , go back to the main menu or full exit from the program, and i want it every time.

i hope i'm clear


 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      09-19-2012
On Wed, Sep 19, 2012 at 5:41 PM, Dan Katorza <> wrote:
>
> Hello again,
> I have another question and i hope you will understand me..
> Is there any option where you can set the program to go back to lets say the top of the code?
> I mean if the program finished the operation and i want to stay in the program and go back ro the start.
> after any operation i want the option to do it again , go back to the main menu or full exit from the program, and i want it every time.
>
> i hope i'm clear


Yep! Look up the docs and tutorial on "control flow" and "looping
constructs". Sounds like what you want here is a 'while' loop.

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I get the hostname or IP address during a TCPServer"conversation" Victor Reyes Ruby 1 12-27-2007 04:13 PM
How to get IP address from giving hostname in ASP? Microlong® ASP General 3 10-12-2005 10:48 PM
Get hostname, when IP address is known vi Java 5 09-19-2005 10:36 PM
Trouble using a hostname in the address field for the Radius client in IAS Ned Cisco 0 08-11-2005 02:59 PM
RMI: take ip/hostname what client was using and give it back as aremote objects hostname AWieminer Java 0 07-12-2005 08:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57