In Bash Script, How to Generate a Random String

In Bash Script, How to Generate a Random String

I need to generate many random alphanumeric strings, which also use capital letters. It needs to start with 3 characters (e.g. Dk6) and then, with a while loop, go up to about 9 characters (e.g. D7H3j8Sjx). Also, I'm using sshpass and I need to check when it's the correct password, then stop. I know that to guess the password, it would take a very long time, but it would also help if it guessed it by going aaa, aab, aac, aad... aaz, aa1 etc.

4

1 Answer

This function

  function letter() {
    s=abcdefghijklmnopqrstuvxwyz
    p=$(( $RANDOM % 26))        echo -n ${s:$p:1}
  }

generates a random letter. You can substitute abc... for the list of characters you wish to use, count them, and substitute their number to the 26 in line 3.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.