all ip addresses of machines in the local network

damacy wegein at gmail.com
Wed Aug 30 20:54:06 EDT 2006


Amit Khemka wrote:
> On 8/24/06, Amit Khemka <khemkaamit at gmail.com> wrote:
> > On 23 Aug 2006 21:46:21 -0700, damacy <wegein at gmail.com> wrote:
> > > hi, sandra.
> > >
> > > no, it's not as complicated as that. all i want to do is to load a
> > > database onto different machines residing in the same network. i hope
> > > there is a way doing it. or perhaps i have a poor understanding of how
> > > networks work.
> > >
> >
> > I expect that you would know the IP range for your network. Then you
> > can simply 'ping' each IP in the range to find wether its alive.
> > Moreover by your description I guess you would actually want to find
> > all machines in your network that run a particular network service, to
> > allow you to "distribute the database". In such case you can use
> > "nmap" with -p option, to find all the machines which are listening on
> > the particular port.
> >
> > hth,
> > amit.
> It seems that I am not too busy, so here is a code which may work with
> a few tweaks here and there:
> _________________________________________________________________________
> import os
> # base and range of the ip addresses
> baseIP = "10.0.0."
> r = 6
> interestingPort = 22 # port that you want to scan
> myIPs = []
>
> for i in range(r):
>         ip = baseIP+str(i)  # It may need some customization for your case
>         print "scanning: %s" %(ip)
>         for output in os.popen("nmap %s -p %s" %(ip,
> interestingPort)).readlines():
>                 if output.__contains__('%s/tcp open'
> %interestingPort):  # i guess it would be tcp
>                         myIPs.append(ip)
> __________________________________________________________________________
> print myIPs
>
>
> hth,
> amit.
> --
> ----
> Amit Khemka -- onyomo.com
> Home Page: www.cse.iitd.ernet.in/~csd00377
> Endless the world's turn, endless the sun's Spinning, Endless the quest;
> I turn again, back to my own beginning, And here, find rest.

thank you for your code. i had a look at nmap and i think it's got some
cool features in it. however, i found it quite slow in my case as it
takes extra time to process the output.

in my program so far, multiple threads (255 threads in total) spawned
at once with each one of them trying to call socket.gethostbyaddr(ip)
function. i.e. if exception thrown, no machine found. i used .join() to
wait for the threads to terminate. it's fully working however the
problem is that it's too slow. it takes approx. 14 seconds to process
(i tried using 'ping' but it's even slower.).

my question is.. is there a way to improve performance of the program
if i know what the port number would be? in my case, the port number
will always be constant although i have no clue on what ip addresses
would be (that's the reason all of 255 different addresses must be
tested).

i tried the same function with the port number specified,
gethostbyaddr(ip:portno), but it is even 10-second slower than using
the same function without a port number specified (i.e. approx. 25
seconds to complete).

could anyone think of a better way of solving this problem?

regards, damacy




More information about the Python-list mailing list