Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create linux_mac_changer.py #450

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Python/linux_mac_changer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/python3

import subprocess
import argparse
import re

def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("-i","--interface", dest="interface", help=" whose mac address is being changed")
parser.add_argument("-m","--mac", dest="new_mac", help=" new mac address")
args=parser.parse_args()
if not args.interface:
parser.error("Please specify an interface, use --help for more info ")
elif not args.new_mac:
parser.error("Please specify an interface, use --help for more info ")
return parser.parse_args()

def change_mac(interface, new_mac):
subprocess.run(["sudo", "ifconfig", interface, "down"])
subprocess.run(["sudo", "ifconfig", interface, "hw", "ether", new_mac])
subprocess.run(["sudo", "ifconfig", interface, "up"])

def current_mac(interface):
ifconfig_result=subprocess.run(["ifconfig",interface],capture_output=True)
mac_address_search_result= re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w",str(ifconfig_result.stdout))
if mac_address_search_result:
return mac_address_search_result.group(0)
else:
print(f"couldn't print mac address ")

args= get_arguments()
interface= args.interface
new_mac= args.new_mac
current_mac= current_mac(interface)
print(f"current Mac = "+current_mac )

change_mac(interface,new_mac)
if current_mac(interface)== new_mac:
print(f"Mac address changed for {interface} to {new_mac}")
else:
print("Mac address didnot changed")