Friday, 1 November 2013

How to show all computer name in LAN network using python



In your LAN network, if there are number of computer are connected. W e can easily find all computers name and IP using python script.


Here below script will find all computers name or IP which is connected through LAN network.

Only you have provide network IP reference first
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')
 
Output of run program:

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...



192.168.1.30       - Ashish

192.168.1.14       - TEST

192.168.1.3         - SAMPLE



Done

No comments:

Post a Comment