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

Go Back   Techist - Tech Forum > Computer Software > Programming
Click Here to Login
Reply
 
Thread Tools Display Modes
 
Old 12-08-2017, 07:12 AM   #1 (permalink)
Newb Techie
 
Join Date: Dec 2017
Location: Norge
Posts: 3
Post Make a python script that picks up ip's and mac-addresses at the local network

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!
__________________

__________________
olli1324 is offline   Reply With Quote
Old 12-10-2017, 09:13 AM   #2 (permalink)
Monster Techie
 
rulezero's Avatar
 
Join Date: Jan 2015
Location: Earth
Posts: 1,692
Default Re: Make a python script that picks up ip's and mac-addresses at the local network

Hi,

So you want a script to scan your LAN daily? and output every IP & MAC it finds?
__________________

__________________
Don't like what I say, ban me!
rulezero is offline   Reply With Quote
Old 12-10-2017, 10:16 AM   #3 (permalink)
Newb Techie
 
Join Date: Dec 2017
Location: Norge
Posts: 3
Default Re: Make a python script that picks up ip's and mac-addresses at the local network

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.
__________________
olli1324 is offline   Reply With Quote
Old 12-10-2017, 10:02 PM   #4 (permalink)
Lord Techie
 
S0ULphIRE's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 8,813
Send a message via MSN to S0ULphIRE
Default Re: Make a python script that picks up ip's and mac-addresses at the local network

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.co...using-0638be7b
__________________
"As a result of all this hardship, dirt, thirst, and wombats, you would expect Australians to be a sour lot. Instead, they are genial, jolly, cheerful, and always willing to share a kind word with a stranger, unless they are an American." -- Douglas Adams
S0ULphIRE is offline   Reply With Quote
Old 12-11-2017, 01:48 PM   #5 (permalink)
Newb Techie
 
Join Date: Dec 2017
Location: Norge
Posts: 3
Default Re: Make a python script that picks up ip's and mac-addresses at the local network

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
__________________
olli1324 is offline   Reply With Quote
Old 12-11-2017, 06:33 PM   #6 (permalink)
Lord Techie
 
S0ULphIRE's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 8,813
Send a message via MSN to S0ULphIRE
Default Re: Make a python script that picks up ip's and mac-addresses at the local network

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.../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 -_-
__________________

__________________
"As a result of all this hardship, dirt, thirst, and wombats, you would expect Australians to be a sour lot. Instead, they are genial, jolly, cheerful, and always willing to share a kind word with a stranger, unless they are an American." -- Douglas Adams
S0ULphIRE is offline   Reply With Quote
Reply

Tags
python, script, wol

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic python script fallenapples Programming 3 01-31-2015 12:29 PM
My LG tv picks up signals from my DVD remote and visa versa? edward.s Hardware Repairs and Troubleshooting 1 02-03-2013 03:46 PM
Allowing only certain MAC addresses to access my network. Jayce Computer Networking and Internet Hardware 6 12-27-2006 10:37 PM
Graphing Picks heavyartilery Off Topic Discussion 4 06-09-2002 06:22 PM
Graphing Picks heavyartilery Off Topic Discussion 4 06-09-2002 06:22 PM



Copyright 2002- Social Knowledge, LLC All Rights Reserved.

All times are GMT -5. The time now is 10:03 PM.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2018, vBulletin Solutions, Inc.