Python

GitBook docs section concerning Python context.

Create simple menu options in python script, with three entries.

def menu():
    print("[1] First Entry")
    print("[2] Second Entry")
    print("[3] Third Entry")
    print("[0] Exit")

menu()
menu_option = int(input('Select an option: '))

while menu_option != 0:
    if menu_option == 1:
        # do stuff for first entry
    elif menu_option == 2:
        # do stuff for second entry
    elif menu_option == 3:
        # do stuff for third entry
    else:
        # selected wrong entry

    print()
    menu()
    menu_option = int(input('Select an option: '))

print()
print('Script terminated.')

Font Color & Style

Customize font and style of printed text in python script.

### COLORS & STYLE DEFINITION ###
class colors:
    reset='\033[0m'
    bold='\033[01m'
    disable='\033[02m'
    underline='\033[04m'
    reverse='\033[07m'
    strikethrough='\033[09m'
    invisible='\033[08m'
    class fg:
        black='\033[30m'
        red='\033[31m'
        green='\033[32m'
        orange='\033[33m'
        blue='\033[34m'
        purple='\033[35m'
        cyan='\033[36m'
        lightgrey='\033[37m'
        darkgrey='\033[90m'
        lightred='\033[91m'
        lightgreen='\033[92m'
        yellow='\033[93m'
        lightblue='\033[94m'
        pink='\033[95m'
        lightcyan='\033[96m'
    class bg:
        black='\033[40m'
        red='\033[41m'
        green='\033[42m'
        orange='\033[43m'
        blue='\033[44m'
        purple='\033[45m'
        cyan='\033[46m'
        lightgrey='\033[47m'

# example
print(f"{colors.bg.red}{colors.fg.orange}I'm a colored string{colors.reset}")

IP Scanner

Simple scripts that scans lan network IPs.

Version One (Unix only)

import scapy.all as scapy


def scan(ip):
    arp_req_frame = scapy.ARP(pdst = ip)

    broadcast_ether_frame = scapy.Ether(dst = "ff:ff:ff:ff:ff:ff")

    broadcast_ether_arp_req_frame = broadcast_ether_frame / arp_req_frame

    answered_list = scapy.srp(broadcast_ether_arp_req_frame, timeout = 1, verbose = False)[0]
    result = []
    for i in range(0,len(answered_list)):
        client_dict = {"ip" : answered_list[i][1].psrc, "mac" : answered_list[i][1].hwsrc}
        result.append(client_dict)

    return result

def display_result(result):
    print("-----------------------------------\nIP Address\tMAC Address\n-----------------------------------")
    for i in result:
        print("{}\t{}".format(i["ip"], i["mac"]))


target = input("Enter Target IP Address/Adresses: ")
scanned_output = scan(target)
display_result(scanned_output)

Version Two (Unix)

import subprocess
import re


def Get_Host(x):
    Dot_Counter = 0
    Pos_Counter = 0
    for i in x:
        if i == ".":
            Dot_Counter += 1
        if Dot_Counter == 3:
            return (x[0:Pos_Counter + 1], x[Pos_Counter + 1: ])
            break
        Pos_Counter += 1


First_ip = input("Enter the first IP Address: ")
Last_ip = input("Enter the last IP Address: ")

Network, First_Host = Get_Host(First_ip)
Network, Last_Host = Get_Host(Last_ip)

Empty_String = ""
Counter = 0

for i in range(int(First_Host), int(Last_Host) + 1):
    Process = subprocess.getoutput("ping -c 1 " + Network + str(i))
    Empty_String += Process

    String_Needed = re.compile(r"ttl=")
    mo = String_Needed.search(Empty_String)
    try:
        if mo.group() == "ttl=":
            print("Host " + Network + str(i) + " is UP")
    except:
        print("Host " + Network + str(i) + " is Down")

    Empty_String = ""

print("Completed")

Version Two (Windows)

import subprocess
import re


def Get_Host(x):
    Dot_Counter = 0
    Pos_Counter = 0
    for i in x:
        if i == ".":
            Dot_Counter += 1
        if Dot_Counter == 3:
            return (x[0:Pos_Counter + 1], x[Pos_Counter + 1: ])
            break
        Pos_Counter += 1


First_ip = input("Enter the first IP Address: ")
Last_ip = input("Enter the last IP Address: ")

Network, First_Host = Get_Host(First_ip)
Network, Last_Host = Get_Host(Last_ip)

Empty_String = ""
Counter = 0

for i in range(int(First_Host), int(Last_Host) + 1):
    Process = subprocess.getoutput("ping -n 1 " + Network + str(i))
    Empty_String += Process

    String_Needed = re.compile(r"TTL=")
    mo = String_Needed.search(Empty_String)
    try:
        if mo.group() == "ttl=":
            print("Host " + Network + str(i) + " is UP")
    except:
        print("Host " + Network + str(i) + " is Down")

    Empty_String = ""

print("Completed")

Last updated