If WSLg apps show a black screen!

You might have rendering issues with WSLg apps, especially in case you are using an Intel graphics card, like this:

In that case, just configure an environment variable named LIBGL_ALWAYS_SOFTWARE and set it to 1. To persist it, add this line inside /etc/profile:

export LIBGL_ALWAYS_SOFTWARE=1

Just restart (using wsl --shutdown for example).

Note: This will disable GPU acceleration for those GUI apps, which I don’t really care about in this case!

Tested on Windows 11 Pro host with Ubuntu 24.04 WSL guest.

That’s it, Enjoy!

Sources:

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

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