1

I did a thorough research everywhere I could and yet came up short (partial solutions) on solving the problem described in the Title.

I tried adapting my script by using this solution found here - https://stackoverflow.com/a/32484733/7238741

chars=abcd1234ABCD
for i in {1..8} ; do
    echo -n "${chars:RANDOM%${#chars}:1}"
done
echo

PROBLEM: It will generate a random string for each curl command in my bash script, the problem is when it generates a, let's say, string of 5 characters, it will use 'neighbor' characters i.e. 45678 or defgh. So perhaps we should leave this solution aside completely, and focus on the next one.

Next, I used this solution in my script which seems would've done the job 100% - https://gist.github.com/earthgecko/3089509 - but... here's a practical example what I'd like to achieve - each of the two curl commands should end up posting a Random and Different comment which, in this case, consists of 5 alpha-numeric characters:

Here's v1 of the bash script:

#!/bin/bash

NEW_UUID=$(cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 5 | head -n 1)

curl "https://mywebsite.com/comment... ...&text=$NEW_UUID"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=$NEW_UUID"; sleep 60;

Here's v2 of the bash script (I will use this one, I posted the more simple v1 just in case it's easier for someone to provide a solution)

#!/bin/bash

NEW_UUID=$(cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 5 | head -n 1)

for i in {1..2}; do curl "https://mywebsite.com/comment... ...&text=$NEW_UUID"; sleep 60; done

PROBLEM: In both versions, the script will generate a different random string each time it's executed, but that one string will be used for both curl commands, resulting in always posting the exact same comment both times ( ex: TuJ1a ), and the goal is to generate a different and random string for each curl command.

In the end, the first few answers to this Question which is somewhat the same as mine, do provide a solution how to "generate a different string in each line" - https://unix.stackexchange.com/questions/428737/how-can-i-add-random-string-for-each-line - it's just Their answers are adapted to OP's personal script, and unfortunately for me I do not have the proper knowledge to adapt those solutions to my 'v2 script'.

Thank you for any assistance / hint you may provide <3

0

1 Answer 1

2

the goal is to generate a different and random string for each curl command.

So just put the string generation in the loop........

#!/bin/bash
for i in {1..2}; do
     newuuid=$(cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 5 | head -n 1)
     curl "https://mywebsite.com/comment... ...&text=$newuuid"
     sleep 60
done

if it's generating UUID, call uuidgen if available to generate uuid.

cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 5 | head -n 1 is heavy execution wise and may deplete entropy input very fast.

Sign up to request clarification or add additional context in comments.

7 Comments

This solves it. I tried this y-day but I've made a crucial mistake. The "newuuid" line was put before "do curl", when I should've followed your example and write "do newuuid-line curl". So basically this is the absolute correct answer :) I'm still worried about your last sentence... I generate my random unique strings here atm in the tens of thousands - random.org/strings , will using 'cat /dev/urandom' cause issues to do the same ? As an amateur I just thought it will create a random value each time out of 62^5 possible combinations. So what causes the depletion of entropy a.k.a randomness ?
in the tens of thousands you'll end up in 100% cpu. What kind of strings do you need? will using 'cat /dev/urandom' cause issues to do the same ? Yes. And zero bytes are possible. cat will read a full block from urandom. So for your string of 5 bytes, you'll end up reading 4KiB of bytes from random....
The problem with cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 5 | head -n 1 is that: Each process has full buffering, so it will read ca ~4096 bytes and tr discards like 50% of numbers. So basically the line takes ~16k of random bytes (on my system I see 128KiB read from urandom in this line....) and discards 99% of them just to give you just 5 bytes....
Random & different 5-char alphanumeric strings (comments) that will be posted with a pause of... 5sec between each. CPU did went to ~25% with your script for running a single curl command. For now, I'm using random.org to generate 30K strings, and with the help of a simple excel file I generate few 1000s of copies (at a time) of the same curl command, and each command will end with one of those strings. All of that is put in my script and executed with Git Bash. CPU usage is below 5% constantly. So I'm trying to simplify the process with my v2 script by ditching the excel file step, that's all
Update on previous comment: the 25% - must've been the wind :) I tried again and was looking directly at the MSYS2 terminal, it didn't pass 1% of CPU usage. So everything is as it should be, except that you worried me for the lack of entropy over time :) I still assume a random value will be created out of 62^5 possible combinations for each curl command in the list of 2,000 curl commands that represent the content of the script, and the chances of repeating the same comment may happen... once a week or so, so no problem there.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.