Make a python script that picks up ip's and mac-addresses at the local network

olli1324

Beta member
Messages
3
Location
Norge
I am trying to make a script that can pick up all the ip's and mac-adresses for the ip's at the local network at work, and I am kind of new to programming.
In the first row am I trying to make a script that picks up all the ip's and mac's and get them printed in a xml file, but in the end do I want to make a WOL script for all the pc's I have picked up the mac's for.
I have also been told we want as much as possible of the code in functions, so other scripts can be placed "upon" this one as easy as possible.
I have tried arp -a and alot of other things, but I always come short in ip's and mac addresses, since I do expect max 100 ip's, but get around 10 when I use arp -a...
So if anyone have any tips related to this, I would be more than happy to read it!
Thanks!
 
Well, we have alot of computers with simulators, and we need a wake on lan script, so we can run that daily or whenever we need it. For example if we can place the script in the Windows Scheduler so it runs at a specific time every morning.
But yes, I want a script that scans the LAN for all the mac's and ip's and output all of them, then wakes them up.
 
Is there a particular reason you'd like to stick with Python? You can easily do this with Powershell if that's an option for you.
e.g. here's a function that'll send a WOL packet to the specified mac/port/ip; scanning a subnet for active IPs is super easy too
Code:
function Send-WOL
{
<# 
  .SYNOPSIS  
    Send a WOL packet to a broadcast address
  .PARAMETER mac
   The MAC address of the device that need to wake up
  .PARAMETER ip
   The IP address where the WOL packet will be sent to
  .EXAMPLE 
   Send-WOL -mac 00:11:32:21:2D:11 -ip 192.168.8.255 
#>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True,Position=1)]
        [string]$mac,
        [string]$ip="255.255.255.255", 
        [int]$port=9
    )

    $broadcast = [Net.IPAddress]::Parse($ip)
 
    $mac=(($mac.replace(":","")).replace("-","")).replace(".","")
    $target=0,2,4,6,8,10 | % {[convert]::ToByte($mac.substring($_,2),16)}
    $packet = (,[byte]255 * 6) + ($target * 16)
 
    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect($broadcast,$port)
    [void]$UDPclient.Send($packet, 102) 

}

https://gallery.technet.microsoft.com/scriptcenter/Send-WOL-packet-using-0638be7b
 
Well, I know nearly all of our scripts is written in Python, so may it be something about them being compatible with eachother? I have only been told that Python was the wanted language for this script, and currently Python is the most (still little) foreign language to me. So I guess the best would be if this one was in Python too:)
 
Looks like someone's written a python module already too: https://pypi.python.org/pypi/wakeonlan/0.2.2
Or alternatively, here it is in a function: https://github.com/bentasker/Wake-On-Lan-Python/blob/master/wol.py

For the mac address part, FIRST scan your whole subnet for active IPs (ping each ip in the range). If you get a response, your arp table will update with that record including the mac address.

After that it should be pretty straightforward to build the list of host details to send to the WOL function. If I get time today I'll see if I can get something written for you, but it's gonna be a busy one -_-
 
Back
Top Bottom