zoukankan      html  css  js  c++  java
  • Python Ethical Hacking

    FUNCTIONS

    • Set of instructions to carry out a task.
    • Can take input, and return a result.
    • Make the code clearer, reusable, and more abstract.
    • input() function prompts the user to enter the value.

    Rewrite the Python script using the function style.

    #!/usr/bin/env python
    
    import subprocess
    import optparse
    
    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"])
    
    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) = parser.parse_args()
    
    change_mac(options.interface, options.new_mac)

    Execute the following commands successfully to change the MAC.

    python mac_changer.py -i eth0 -m 00:11:22:33:44:22

    Rewrite the Python script.

    #!/usr/bin/env python
    
    import subprocess
    import optparse
    
    def get_arguments():
        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")
        return parser.parse_args()
    
    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"])
    
    (options, arguments) = get_arguments()
    change_mac(options.interface, options.new_mac)

    Execute the following commands successfully to change the MAC.

    python mac_changer.py -i eth0 -m 00:11:22:33:44:33

     Decision Making

    • Execute code ONLY if a condition is true.

    Rewrite the Python code using conditional statements.

    #!/usr/bin/env python
    
    import subprocess
    import optparse
    
    def get_arguments():
        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) = parser.parse_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"])
    
    options = get_arguments()
    change_mac(options.interface, options.new_mac)

    Test the script using the following commands.

    相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
  • 相关阅读:
    java设计模式-适配器模式
    java设计模式-外观模式
    java设计模式-享元模式
    java设计模式-装饰模式
    java设计模式-组合模式
    java设计模式-桥接模式
    12月Java原生商城APP源码-完全开源
    uniapp插件市场-涂图视频编辑-美妆-剪辑-微整形原生sdk插件发布-优雅草科技
    12月最新仿知音漫画网站源码+手机端,小说漫画生成静态文件,超强负载量安全可靠
    如何把网易云音乐ncm格式转换成mp3格式---记一下
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/11333947.html
Copyright © 2011-2022 走看看