Now to get this result.
Okay.
Or this Mac address from output which is stored in ifconfig result.
Okay.
We are going to use a module called regex or re.
So let's import that okay.
Import re.
So okay so now we have imported this.
Maybe it is not already installed.
Okay.
So if you want to install this so let me go to here use pip install re.
So this will be installed okay.
If we have internet connection.
So it will be installed.
If we don't have it no problem.
Let we will we will install that okay.
So as you see it is it has another color okay.
Change color.
So maybe it is not installed okay.
So we're going to use from that.
And that Re has a function that is called search.
Very easy.
Okay.
Now let's use that re dot search.
Okay.
Here we are going to provide the regex.
Okay.
So in here I am going to give the regex pattern that I just created here.
Let me copy this.
And go back to curly.
Here I'm going to paste this.
Okay.
So this is the first argument that search accepts.
And the next one is uh the resource or where do you want to search this.
Okay.
We are going to search this pattern into this variable which is if config result.
Very good.
So you need to give the if config result right here.
Okay.
If config result.
So there we go.
Now we have it here.
And it is better to store this into a variable.
Okay.
So later we can use it.
It is going to be Mac address search result.
So let's use Mac.
Address.
Search result.
It is equal to something like this.
And.
Okay.
Now I think it is installed.
Um, okay, I think I don't have internet connection.
No problem.
I will solve that.
Okay, so now we have installed the we have installed this into this variable.
Okay.
So the next part is to just print this very easy okay.
So I just use print method here.
Okay.
And Mac address search result.
And sometimes it return multiple values okay.
For example right now here it is only returning one thing and that is the Mac address.
But sometimes it is returning multiple things.
So we need to specify that by using another function that is called group.
And here we give the zero.
So it will give you the first one or the specified by one.
So it will give you the next one which is the second one okay.
So we have this and let me go to Python interpreter.
See if it is installed.
Import re.
Okay.
As you see it gives me no error.
That means it is installed.
Very good.
And is it okay.
So what is the next thing we need to do right now.
If I execute this.
So it will give me no error and it will be executed successfully.
So let's see that here.
So right now I'm trying to change the Mac address for it is zero.
And hitting enter providing the password which is curly and hitting enter.
So.
Okay.
And here you see it gives me a result says cannot use string pattern on byte like object.
Okay no problem.
As you see here it is string.
And it is saying that we cannot use this okay.
On object.
So it is better to change it to something else.
I will be back okay.
Okay.
We have re here which is going to be an object.
And also.
Let's see the error again.
What is it giving me.
It is saying return this one okay.
And also dot search.
Oh my God.
This search method must have string.
Okay.
Anything that it returns, anything you give to this must be string.
So here as you see the first one.
It is a string and this ifconfig I think it is not a string.
Okay, so let's provide this as a string and change it to string str.
Now let me save this and let's go back to our terminal here Re-execute this and hit enter.
Okay.
Now as you see here okay.
The first one, it is giving me the result okay.
The result for eth0 as you see that here.
And then at the end it is printing only the Mac address that we are telling this to print that here.
Okay.
What if I try to change this for not.
It is zero for low.
What will be happening?
Let's see.
Now when I hit enter, you see that it is printing the low result here.
Okay.
And it is giving me that the interface is low, but but, uh, again, it is giving me this result.
Why?
Because the low doesn't have any Mac address, so we need to handle that here.
Okay.
After printing or before printing that we need to handle that using decision making.
And you know how to use decision making that is using by using if and else.
So here I'm going to use if Mac address source result.
If it returns something true then we will be printing this one okay.
In else part okay.
In else part.
Let's print.
Could not read Mac address or something like that okay.
So use else and here print that.
Okay.
Here just give.
Uh, okay.
Could not read Mac address.
Now I'm going to save this and let's come back to terminal.
Let's use clear.
Image.
Okay.
Right now I'm trying to change it for lo.
And you see that it is giving me could not read Mac address.
Okay.
And if I try to change it for eth0 that is here and hit enter.
You see it is giving me the Mac address.
So here we are able to read or capture the Mac address from this output.
Okay.
This is the Mac address for this interface.
And we are handling it.
We just handled it.
Okay.
So let's go and see how we can improve this in the next lecture.