Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #55595
    pranetkul
    Participant

    Hi Sir, I have a problem with this code as I receive this: RuntimeError: maximum recursion depth exceeded while calling a Python object

    Can you let me know please what is wrong with my code?

    #!/usr/bin/env python
    
    import re
    import subprocess
    import optparse
    
    def get_args():
        parser = optparse.OptionParser()
        parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC address")
        parser.add_option("-m", "--mac", dest="interface", help="New mac address")
        (options, arguments) = get_args()
        if not options.interface:
            parser.error("[-] Please specify an interface, use --help for more info.")
        elif not options.new_mac:
            parser.error("[-] Please specify a new mac, use --help for more info.")
        return options
    
    def change_mac(interface, new_mac):
        print("[+] Changing MAC address for " + interface + " to " + new_mac)
        subprocess.call(["ifconfig", interface, "down"])
        subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
        subprocess.call(["ifconfig", interface, "up"])
    
    def get_current_mac(interface):
        ifconfig_result = subprocess.check_output("ifconfig", interface)
        print(ifconfig_result)
        mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", str(ifconfig_result))
        if mac_address_search_result:
            return mac_address_search_result.group(0)
        else:
            print("[-] Could not read MAC address.")
    
    options = get_args()
    
    current_mac = get_current_mac(options.interface)
    print("Current MAC = " + str(current_mac))
    
    change_mac(options.interface, options.new_mac)
    
    current_mac = get_current_mac(options.interface)
    if current_mac == options.new_mac:
        print("[+] MAC address was successfully changed to " + current_mac)
    else:
        print("[-] MAC address did not get changed")
    #55627
    Diego PérezDiego Pérez
    Participant

    Hi!

    Can you share a screenshot with the command used to run the code and the result please?

    Thanks!
    Diego

    #55631
    pranetkul
    Participant

    file:///root/Pictures/Screenshot%20from%202021-06-09%2022-41-11.png

    file:///root/Pictures/Screenshot%20from%202021-06-09%2022-41-16.png

    #55632
    pranetkul
    Participant

    Sorry if you can’t see the image. I cannot figure out how to send them

    #55649
    Diego PérezDiego Pérez
    Participant

    Hi!

    Just upload them to some image hosting service or Gdrive and share the link here.

    Greetings!
    Diego

    #55660
    pranetkul
    Participant

    #55661
    pranetkul
    Participant

    • This reply was modified 4 years, 2 months ago by pranetkul.
    #55662
    pranetkul
    Participant

    These are the right screenshots
    image 1
    image 2

    • This reply was modified 4 years, 2 months ago by pranetkul.
    #55684
    Diego PérezDiego Pérez
    Participant

    Hi!
    The access is restricted, can you allow it to anyone who has the link?

    Thanks!
    Diego

    #55686
    pranetkul
    Participant
    #55708
    pranetkul
    Participant

    Hopefully, the images work now

    #55713
    Diego PérezDiego Pérez
    Participant

    Hi!

    In the get_args function you have used interface as dest value for both options, correct this and try it again.

    Greetings!
    Diego

    #55725
    pranetkul
    Participant
    #!/usr/bin/env python
    
    import re
    import subprocess
    import optparse
    
    def get_args():
        parser = optparse.OptionParser()
        parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC address")
        parser.add_option("-m", "--mac", dest="new_mac", help="New mac address")
        (options, arguments) = get_args()
        if not options.interface:
            parser.error("[-] Please specify an interface, use --help for more info.")
        elif not options.new_mac:
            parser.error("[-] Please specify a new mac, use --help for more info.")
        return options
    
    def change_mac(interface, new_mac):
        print("[+] Changing MAC address for " + interface + " to " + new_mac)
        subprocess.call(["ifconfig", interface, "down"])
        subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
        subprocess.call(["ifconfig", interface, "up"])
    
    def get_current_mac(interface):
        ifconfig_result = subprocess.check_output("ifconfig", interface)
        print(ifconfig_result)
        mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", str(ifconfig_result))
        if mac_address_search_result:
            return mac_address_search_result.group(0)
        else:
            print("[-] Could not read MAC address.")
    
    options = get_args()
    current_mac = get_current_mac(options.interface)
    print("Current MAC = " + str(current_mac))
    
    change_mac(options.interface, options.new_mac)
    
    current_mac = get_current_mac(options.interface)
    if current_mac == options.new_mac:
        print("[+] MAC address was successfully changed to " + current_mac)
    else:
        print("[-] MAC address did not get changed")
    
    #55726
    pranetkul
    Participant
    #55745
    pranetkul
    Participant

    can you let me know please what I should do

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.
Privacy Overview
ZSecurity logo featuring a stylized red letter Z

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

3rd Party Cookies

This website uses Google Analytics and Linkedin to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping these cookies enabled helps us to improve our website.