# Python

<br>

## Menu Options

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

```python
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.')
```

<br>

## Font Color & Style

Customize font and style of printed text in python script.

```python
### 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}")
```

<br>

## IP Scanner

Simple scripts that scans lan network IPs.

#### Version One (Unix only)

```python
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)

```python
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)

```python
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")
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sbdevs-organization.gitbook.io/sbdev-docs/docs/docs-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
