Simple Python Script to Communicate with ChatGPT API

I wrote a simple python script that allows you to communicate with the ChatGPT API, the workflow is as straight-forward as this:

  • You read the prompt from a file
  • Send the prompt to the model of your choice
  • Receive the response and write it to a file

Prerequisites:

  • Python (obviously :D), I used Python 3.12
  • The openai python bindings (pip install openai)
  • OpenAI subscription with at least a few cents there!

Limitations:

  • Only single prompt and response
  • No exception handling, too lazy to write it now!

Here is the code in its simplest form: https://github.com/ahmedatawfik/usefulscripts/blob/main/chatgptcomm.py

import openai

# This is a dummy key, replace with your actual OpenAI API key
openai.api_key = "9i-wmcxtsO-VuDJlPsbRlR9sqWfdLphxFEedbFIyw3MFHlcbtcTapxj6v4CfwmEtarZQWJ0ieEJXSCBHOEM5LP6m-Kd1KVfvuOKPBP8MAeVlvXQsudk8"

# Function to read the prompt from prompt.txt
def read_prompt_from_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        prompt = file.read()
    return prompt

# Function to write the response to response.txt
def write_response_to_file(file_path, response):
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(response)

# Function to get response from ChatGPT
def get_chatgpt_response(prompt):
    response = openai.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": "You are a helpful assistant who provides cool answers!"},
            {"role": "user", "content": prompt}
        ]
    )
    return response.choices[0].message.content.strip()

# Main function to read, get response, and write
def main():
    prompt = read_prompt_from_file(r"/home/user/prompt.txt")
    response = get_chatgpt_response(prompt)
    write_response_to_file(r"/home/user/response.txt", response)
    print("Response has been written to response.txt")

if __name__ == "__main__":
    main()

That’s it, Enjoy!

Sources:

Posted in python | Tagged , , , | Leave a comment

List all files and first level directories arranged by size on Linux

du -sh * | sort -hr

Where:

  • du -sh: Shows the sizes of files and directories in a human-readable format
  • sort -hr: Sorts the output by size in human format, with the largest items at the top.

Sample:

That’s it, Enjoy!

Posted in Linux | Tagged , | Leave a comment

Mount shared folder inside Kali Linux VM on VMware Workstation

Create a share from your Virtual Machine Settings, let’s call it shared_vm

sudo apt install open-vm-tools # Make sure you have the latest vmware tools installed
sudo mkdir /shared_vm
sudo /usr/bin/vmhgfs-fuse .host:/ /shared_vm -o subtype=vmhgfs-fuse,allow_other # Mounts the shared_vm share to /shared_vm

To make it permanent, add it to /etc/fstab as follows:

vmhgfs-fuse /mnt/hgfs fuse defaults,allow_other 0 0

Tested on the following environment:

  • Host: Windows 11 Pro x86-64 Host
  • Guest: Kali 2024.3 x86-64
  • Hypervisor: VMware Workstation 16.2.5 Pro

That’s it, Enjoy!

Sources: VMware Official Docs

Posted in Linux | Tagged , , , | Leave a comment

Script to grab sizes of GCP buckets

Prerequisites:

  • Google Cloud SDK Installed & Configured

For a simple script that just does the job and gets the sizes of all your buckets:

for bucket in $(gsutil ls); do
  echo "Bucket: $bucket"
  gsutil du -sh $bucket
done

For a more comprehensive script that picks a specific set of buckets and saves the output to a file:

#!/bin/bash

# File to save the output
output_file="bucket_sizes.txt"

# List of specific buckets
buckets=(
  "gs://bucket1/"
  "gs://bucket2/"
  "gs://bucket3/"
  "gs://bucket4"
)

# Clear the output file if it exists, or create a new one
> $output_file

# Loop through the list and get the size of each bucket
for bucket in "${buckets[@]}"; do
  echo "Bucket: $bucket" >> $output_file
  gsutil du -sh $bucket >> $output_file
done

# Print a message indicating that the process is complete
echo "Bucket sizes have been saved to $output_file"

That’s it, Enjoy!

Posted in Linux | Tagged , , | Leave a comment

How to enable Python to craft packets and listen to ports less than 1024 without sudo

To be able to craft packets with Scapy, you have to either use sudo or allow setcap for your python environment. I don’t prefer to use sudo when working with my Anaconda environments, so I did the following:

sudo setcap 'cap_net_raw,cap_net_admin,cap_net_bind_service=eip' /home/ubuntu/anaconda3/envs/py311/bin/python3.11

Note: Using in the above example python instead of python3.11 won’t work, since python in that location is just a symbolic link, you have to specify the executable itself directly.

That’s it, now you can run your program without sudo!

Enjoy!

Posted in Linux | Tagged , , | Leave a comment