You are not logged in.

#1 2008-12-15 17:53:14

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Python LAN Scanner

Hello,

I'm making a LAN scanner in Python. It's supposed to connect to all available IP addresses on the LAN and if a connection is established, then there's a PC on that address. It works fine in Arch-x86 (and -x86_64), but for some reason it doesn't work right in Windows (XP and Vista, both 32-bit, if it matters). On my LAN I have machines on 192.168.1.2, .1.3, and .1.100. The problem is that 192.168.1.100 is never found. I can ping it, I can connect to it in a Python shell, but the script can't find it.

Can you try to fix it? It would be much appreciated! Thank you in advance.

- Boris

from socket import *
network = '192.168.1.'

def is_up(addr):
    s = socket(AF_INET, SOCK_STREAM)
    s.settimeout(0.01)    ## set a timeout of 0.01 sec
    if not s.connect_ex((addr,135)):    # connect to the remote host on port 135
        s.close()                       ## (port 135 is always open on Windows machines, AFAIK)
        return 1
    else:
        s.close()

def run():
    print ''
    for ip in xrange(1,256):    ## 'ping' addresses 192.168.1.1 to .1.255
        addr = network + str(ip)
        if is_up(addr):
            print '%s \t- %s' %(addr, getfqdn(addr))    ## the function 'getfqdn' returns the remote hostname
    print    ## just print a blank line


if __name__ == '__main__':
    print '''I'm scanning the local network for connected Windows machines (and others with samba server running).
Also, I'll try to resolve the hostnames.
This might take some time, depending on the number of the PC's found. Please wait...'''

    run()

    raw_input('Done')

Offline

#2 2008-12-15 18:05:58

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 835
Website

Re: Python LAN Scanner

Hi, since you are coding in python, might I interest you in scapy http://www.secdev.org/projects/scapy/ It's one powerful script, you can use it, import from it etc.


You need to install an RTFM interface.

Offline

#3 2008-12-15 18:20:34

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: Python LAN Scanner

anrxc wrote:

Hi, since you are coding in python, might I interest you in scapy http://www.secdev.org/projects/scapy/ It's one powerful script, you can use it, import from it etc.

Thanks, but in Windows (with Python 2.6), when I try to import it, I get this error:

>>> import scapy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\site-packages\scapy.py", line 3662
    tr = map(lambda x: Gnuplot.Data(x,with="lines"), trt.values())
                                         ^
SyntaxError: invalid syntax
>>>

I downloaded the latest version of scapy.

Last edited by Boris Bolgradov (2008-12-15 18:22:26)

Offline

#4 2008-12-15 19:31:39

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 835
Website

Re: Python LAN Scanner


You need to install an RTFM interface.

Offline

#5 2008-12-15 19:56:01

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: Python LAN Scanner

anrxc wrote:

Okay, now it wants the pcap module, which wants Python 2.5 ...
From http://oss.coresecurity.com/projects/pcapy.html :
"Latest release (0.10.5), updated on March 27, 2007 – windows installer – Python 2.5 and WinPcap 4.0."

I think it's getting too complicated for a simple script. Isn't there another workaround?

Offline

#6 2008-12-15 20:37:30

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 835
Website

Re: Python LAN Scanner

Sorry, never used it on Windows. You might find something browsing other tickets.


You need to install an RTFM interface.

Offline

#7 2008-12-15 20:47:28

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: Python LAN Scanner

I'll take a look, thanks.

Meanwhile, I'm still waiting for a "simple fix" to my problem, because I don't get why it's working in Arch, but not in Windows, and the Python version is 2.6. What could be so different?

* Edit:
With timeout set to 0.1 sec (instead of 0.01 sec), everything is fine. Problem solved.

Last edited by Boris Bolgradov (2008-12-16 19:23:03)

Offline

#8 2008-12-19 04:13:50

LeoSolaris
Member
From: South Carolina
Registered: 2008-03-30
Posts: 354

Re: Python LAN Scanner

Boris Bolgradov wrote:

I'll take a look, thanks.

Meanwhile, I'm still waiting for a "simple fix" to my problem, because I don't get why it's working in Arch, but not in Windows, and the Python version is 2.6. What could be so different?

* Edit:
With timeout set to 0.1 sec (instead of 0.01 sec), everything is fine. Problem solved.

See...  Windows is slower than Linux!


I keep getting distracted from my webserver project...

huh? oooh...  shiny!

Offline

Board footer

Powered by FluxBB