MAC CHANGER ALGORITHMS DESIGN

# SIMPLE ALGORITHM
Steps:
  1. Execute and read ifconfig
  2. read the mac address from the output
  3. check if MAC in ifconfig is what the user requested
  4. print appropriate message
DOWNLOAD/VIEW
# Why we put '#' /line 27
# change_mac(options.interface, options.new_mac)
cuz,So that python, could not read that line/and could not execute the program
 
#1-Execute and read ifconfig

#REGULAR EXPRESSION [REGEX]
  • Search for specific patterns within a string
  • use rules to match the pattern
#Great to tell a program what to look for in a large text
#2-TO read the mac address from the output
https://pythex.org/

Regular expression cheatsheet

we just needed / 08:00:27:7e:e2:d2
extract the mac address using the rule

   
DOWNLOAD/VIEWS

The result in last 2nd line
[ 08:00:27:7e:e2:d2 ]

 #Refactoring & Housekeeping
 [modefing/to get just mac address([ 08:00:27:7e:e2:d2 ])

#READING ERROR!!

line 37/cannot concatenate 'str' and 'NoneType' objects

In line 37 current mac is capturing the result from 36 line
Now this current Mac variable is capturing the result that's returned by the get current Mac function
when we execute code its shows  Could not read MAC address 
and do not read the line 30/31
basically means that whatever code we have here [30/31] is not able to read the MAC address for us.
Therefore when this function is finished executing it has nothing to return SO U can see that if we get a MAC address we are going to 
return the value of the MAC address. 

But if we don't have a value for the MAC address we're only printing that 
we could not read the MAC address and the function is exiting.

Now when the function exits here we're trying to capture a variable in here and current MAC since the
function has nothing to return.
It's returning an object called none which basically means nothing.
It's a non type.
so when we print
print("Current MAC = " + current_mac))
The value stored in current MAC is not text/
So Python does not know how to print this because it's telling me that this is a non-time# SO, we need to tell Python whatever value you get in here..
I want you to treat it as a string /as a text.
print("Current MAC = " + str(current_mac))
Now this idea of printing something as something else or treating a variable as another type
is called # CASTING in programming.
DOWNLOAD/VIEW
current_MAC = None







#IMPIMENTING THE VALIDATION ALGORITHM

DOWNLOAD/VIEW

#WHY WE USED '=='? /line-43
  • if current_mac == options.new_mac:
== :to check that the two values are equal or identical
=  :to assign a value we use single equal




#4

#Changing code for python 3 compatibility

TYPE ERROR: cannot use a string pattern on a bytes-like object

The main differences between Python2 and three and Python3 is
There is a very clear distinction between strings and bytes.
You can think of byte objects as machine-readable strings.

THERE ARE 2 TYPES OF STRING :
  • MACHINE READABLE STRINGS: Only read by machine /byte object/ifconfig_result
  • HUMAN READABLE STRINGS: Only read by humans/ \w\w:\w\w:\w\w:\w\w:\w\w:\w\w
LINE-29 : mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w",str(ifconfig_result))
And what Python is saying you cannot use a string, which we said is a human-readable string on a byte object, which is a machine-readable string.
SO TO FIX IT!
will either have to convert this string right here to a byte object, or we have to convert this byte object, which is a machine-readable string to a human-readable string.

LINE 38:WE HAVE DONE SAME THING WE CONVERTED machine-readable string to a human-readable string. by using 'str'

SO WE DO SAME HERE!
mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w",str(ifconfig_result))
ADDING 'str' CONVERTED machine-readable string to a human-readable string.





Post a Comment

If you have any doubts, please let me know

Previous Post Next Post