Useful Python code

Set up virtual environment

Using virtual python environments is helpful when needing to run old python2 or earlier code. It can also keep your main python environment clean if needing to download potentially conflicting python packages.

# Setting up a python2 virtual env called 'etblue-venv' in the /tmp directory
virtualenv /tmp/etblue-venv -p $(which python)

# Connecting to the virtual environment
source /tmp/etblue-venv/bin/activate

# After connecting, your shell will display the name of the venv

# Verify python version (depends on version you selected)
└─$ python2.7 -V                                                          
Python 2.7.18

# Exit venv (you can go back to it with the 'source' command above)

# The venv remains until you delete it 
sudo rm -R etblue-venv

View pip modules

pip list

Package    Version
---------- -------
pip        20.3.4
setuptools 44.1.1
wheel      0.37.1
xlrd       2.0.1

View installed modules

└─$ python3 -c 'help("modules")'
Cryptodome          cairo               ldb                 retrying
Cython              calendar            lib2to3             rfc3986
IPy                 catfish             libfuturize         rfc3987
IPython             catfish_lib         libpasteurize       rich

Parsing JSON

  • The JSON file used below is the output of the command:

"ffuf -w /opt/useful/SecLists/Discovery/DNS/namelist.txt -u http://<target IP> -H HOST: FUZZ.<target domain> -o possible-subdomains.json"
import json

json_dump = "./possible-subdomains.json"

with open(json_dump) as f:
    data = json.load(f)

for host in data["results"]:
    print(host["host"], host["url"]) # print out the host & URL

Last updated