Wednesday, 15 January 2014

Get list of connected computer's IP address from subnet mask in LAN network

In big organization there are different VLAN connection created for separate department in single LAN connection. It is done by subnet mask becoz of In world IP is very less and computers are more, day by day it's increasing. so by subnet mask we can create number of ip for computer in single LAN that is called VLAN connection.
  
If you are using LAN network and you want to see all computer ip address according to subnet mask then you can find out easily with below code.

Here i am using one python library ipcalc. Through this library we can calculate list of ip address with subnet mask only you have to provide ip address and subnet mask.
Download ipcalc from here

this is a subnet mask cheat sheet according to subnet

Below code will show all ip address with subnet mask.
import ipcalc

ip='192.168.1.1'

subnet='25'

for x in ipcalc.Network(ip+'/'+subnet):
    print str(x)


And this code will show the list of connected computer's ip address and name in LAN network according to subnet mask. only you have to provide ip address and subnet mask.
 from socket import *
network = '192.168.1.'
import ipcalc
      
def is_up(addr):
    s = socket(AF_INET, SOCK_STREAM)
    s.settimeout(0.01)  
    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 ''
    #using subnet mask
    for x in ipcalc.Network('192.168.1.1/25'):
        addr=str(x)
        if is_up(addr):
            print '%s \t- %s' %(addr, getfqdn(addr))  
    print    ## just print a blank line
    print ''
    #without subnetmask
    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')

Thanks guys if you have any question related this post, comment here.

No comments:

Post a Comment