In this video you're going to learn how to change the MAC address of your devices with the python.
Now let's start with some basic terminology.
First what is a MAC address.
So if a MAC address is being assigned to every network device by its manufacturer and it's actually
being used for identification and communication purposes across the network it often is that the MAC
address is Brendan on the device itself.
So the the address is hardcoded in the device.
However it can be spoofed or temporarily changed for different reasons or purposes.
There are a lot of legitimate reasons when changing the MAC address makes sense such as when creating
wireless connections to a particular network.
And of course there are also many illegitimate reasons to do to do it such as for gaining unauthorized
access to a network for example.
And they need to say that you should only use the information in this video for legitimate purposes
only.
OK.
One thing to add is that when spoofed the MAC address is changed until rebooting the device or the system.
That said what we're going to build today is a python code that will change your mac address with a
brand new one.
So procedurally we're going to make this code compatible with Linux systems.
And as you can see I have a linux Bharat operating system up and running here.
So I've told you what's a MAC address.
But how does it actually look like.
So for example it can look like this A one B to a six for example 34 one B six D.
So any combination of two characters from eight to f 0 2 9.
This is all a MAC address looks like.
All right.
Now the fun part.
The coding lets actually go to a nano Ed..
Well that's a new Mac dot view why.
All right.
So first of all in fourth I can just edit code in nano like they do in Visual Studio on windows.
OK.
So first we'll import random.
We'll also need to import os for operating system commands and also sub process.
So we'll create two functions that will help us get a valid random new mac address and this code is
actually inspired by a post on Reddit which I'm willing to link in the code or in the resources of this
video.
We'll start with the basic function which is called get Rand and then this function is going to return
random so we're going to look into random choice ABC the e f 0 1 2 3 4 5 6 7 8 9.
So like I said from 8 to F and from 0 to 9 so this function is going to return a random character from
this string.
Then we create another function to actually create a completely new mac address.
So we'll say get or not get less.
OK actually seeing new Mac not you man but new Mac red an empty string.
We start with an empty string like I said and we use a for loop.
So for I in range 0 5
we have new underscore plus equals get Rand plus get Rand plus this.
Then we create a new Mac and I say that with a sub process call.
So this is gonna give us something like let's say a B zero forty five 1 4 6 0 4 B and it also up until
at this point if we run the new Mac this is what we can get.
But we also see that we have common and this is unfinished because we have only one two three four five
pairs and we need six pairs six double pairs in this case or any two.
Another two character combination and to get to actually get the value MAC address so we exit the loop
we exit the for loop.
In this case let me just indented correctly.
So we exit the for loop and then we'll say new on score plus equals get underscore Rand plus get underscore
Rand.
And this is going to complete our address with the last two characters.
And finally this function is going to return the new underscore.
OK.
So these are the functions.
Now let's actually change the Mac.
What system calls.
We change the MAC address.
What system calls.
So first let's pretty old address.
We'll say print OS starts system I have config 88 zero and we'll grab for ether minus o e I'm gonna
explain this shortly.
0 from 9 2 0 from 9.
ABC the E F we close the square brackets 17 characters and we close this one too.
And I like to think I vote a veto on Reddit for this regex.
Okay now we changed the address with some process and first we need to turn the ether 0 down.
But let me show you.
So if I do.
And I have config here we can see 88 0.
So we do I have conflict 88 0 if I do I have config 88 0.
It's only going to show me 0 and if I grab for it ether like I did in this case it's going to only give
me the line with the MAC address and then minus all we is going to give me exactly the MAC address itself.
So if I do the entire thing here until shift C control shift the
grep ether No such file or directory in this case it's being taken so 0 9 A B C D E F There is also
another we're at
so crap
like this.
And we only get the MAC address as you can see.
So this is the command to get the MAC address and in order to change the address with the process with
the sub process module WE NEED TO TURN THE PAGE 0 down.
So we need sudo to make these changes and I'll just say so we need to make some process call and this
call is going to run the command sudo f config.
So you have to specify each command individually when you do some process call some process and os o
our system and sub process call have similar functionalities.
So 88 0 and also down this is going to turn it down.
Then we get a new mac address instantiated in a variable so that's a new underscore am new Mac so we
run the new Mac function that we've just created and then we make a system call to assign the new mac
address to each DHS 0 so we'll also going to use the saw process call and then we're gonna send the
following commands Sudo.
I f config
88.
0 h w e thir.
So for the either net present as first and new underscore M which is going to assign the new mac address
with the sub process.
Paul this is really cool.
Then we can turn it 0 on with uh with the new mac address assigned to it another sub process start call
Sudo.
I have one thing e.g. zero up here we turned it down and now we turn it up or turn it on.
And finally we can print the new mac address so save print.
Actually OS that system I have config 88 0.
We're at aether so it's very similar to the line here.
Another grab it's actually the same thing minus all we from 0 to 9 A B C D E F close that 17 characters
and we close everything.
Awesome now control all to save control X to quit and let's actually run this.
So our new if I do want I have config 88 0.
We can see that our mac address is this 1 0 8 0 0 0 terminating in 0 9.
And if I do Python new Mac that P Why of course we have the valid syntax that we eat that it's actually
necessary.
So it's not like this doesn't have to be in quotes.
Let's run it again.
Some process call when we have another valid and invalid syntax in this case.
So we go back and correct that
e.g. age 0 ether over here
clear this and then run in one more time.
Let's actually write with pseudo Python
to underscore a mad dog P Y
so if we run it with pseudo it actually changes the first time it didn't change.
So if I do an IV config 88 to zero Now we can see that it changed it didn't want to change it the first
time I have a hunch that it works without the pseudo.
So if I run it again let's try that again yeah.
So I don't have to actually run with Sudo it's probably because it doesn't actually change so fast as
I run these commands.
But anyway there you have it.
This is how you change your mac address and by phone.
Now I would encourage you to learn more about the sub process and the OS modules as they come in the
python standard library and you can do some pretty heavy stuff with.