Sponsored Content
Top Forums Shell Programming and Scripting Need specialized random string generator script Post 302486064 by Chubler_XL on Thursday 6th of January 2011 10:35:01 PM
Old 01-06-2011
This does what you asked for (based on BeefStu's great starter).

Code:
#!/bin/ksh
genstr()
{
    # generate a readom string contaning n characters from a supplied
    # list of chars
    #
    # usage: genstr <n> <listchar ...>
    #
    # eg:  genstr 8 0 1 would generate an 8 digit binary number
    #
    LEN=$1
    shift
    set -A CHARS $@
    CNUM="${#CHARS[@]}"
    RANDSTR=""
    let POSCNT=0;
    while true
    do
        [ $POSCNT -ge $LEN ] && break;
        let POSCNT=$POSCNT+1
        RANDSTR="${RANDSTR}${CHARS[${RANDOM}%${CNUM}]}"
    done
    echo "${RANDSTR}"
}
need=$(genstr 1 1 2 3 4)
genstr $need 0 1

Here is an example of running it 10 times:
Code:
100
00
0001
000
10
1100
1
1010
111
10

 

9 More Discussions You Might Find Interesting

1. Solaris

Does Solaris have a random number generator?

I am trying to find a way to generate random numbers within a shell script. Does Solaris have a utility that will generate random numbers? Thanks in advance. B (3 Replies)
Discussion started by: one_ring99
3 Replies

2. Shell Programming and Scripting

Random Sentence Generator

Hi, I need to create a table with random sentences. I need lines that are upto 1000 characters in lenght. I need a random sentence generator that will create sentences and output it to a text file. The sentences should be of lenght varying from 1 to 1000. Does anyone know how this can be... (7 Replies)
Discussion started by: kaushys
7 Replies

3. Shell Programming and Scripting

Replace a certain string with string containing random rubbish

Hello, in my shell script i have some multi-line text in a variable $TEMP - f.e. blablahblah blah blah bla TARGET hgloglo And i need to replace TARGET with text from another variable ($REPLACE), which is containing some text with nasty characters (\n, ", :, etc.) And stuff the altered text... (2 Replies)
Discussion started by: MilanCZ
2 Replies

4. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

5. UNIX for Dummies Questions & Answers

A crude random byte generator...

There was an upload recently on generating a pseudo-random file when /dev/random does NOT exist. This does not need /dev/random, /dev/urandom or $RANDOM either... (I assume $RANDOM relies on the /dev/random device in some way.) This code uses hexdump just because I like hexdump for ease of... (2 Replies)
Discussion started by: wisecracker
2 Replies

6. Shell Programming and Scripting

Need a script to create specialized output file

I need a script to process the (space-separate) values for each line as seen in the below input file and then output the data into an output file as follows. We have been unable to create this using typical bash scripting and cold not find anything out on the internet similar to what we are trying... (3 Replies)
Discussion started by: ddirc
3 Replies

7. Shell Programming and Scripting

Specialized grep script

I have been (unsuccessfully) trying to write a script that will do the following: for each line of an input file (that contains IP address), the script searches the target file for any lines containing said IP address if it finds a line (or lines) it writes the line(s) to output if the line is... (4 Replies)
Discussion started by: ddirc
4 Replies

8. Shell Programming and Scripting

awk (or) UNIX random RGB colors generator?

Dear UNIX Friends, I was wondering if there is a random RGB color generator or any function in any unix platforms. Please share your ideas. Thanks (2 Replies)
Discussion started by: jacobs.smith
2 Replies

9. Shell Programming and Scripting

Random Password generator with 2 digits and 6 characters

I am using the below to random generate a password but I need to have 2 numeric characters and 6 alphabetic chars head /dev/urandom | tr -dc A-Za-z0-9 | head -c 8 ; echo '' 6USUvqRB ------ Post updated at 04:43 PM ------ Any Help folks - Can the output be passed onto a sed command to... (9 Replies)
Discussion started by: infernalhell
9 Replies
ASN1PARSE(1SSL) 						      OpenSSL							   ASN1PARSE(1SSL)

NAME
asn1parse - ASN.1 parsing tool SYNOPSIS
openssl asn1parse [-inform PEM|DER] [-in filename] [-out filename] [-noout] [-offset number] [-length number] [-i] [-oid filename] [-strparse offset] [-genstr string] [-genconf file] DESCRIPTION
The asn1parse command is a diagnostic utility that can parse ASN.1 structures. It can also be used to extract data from ASN.1 formatted data. OPTIONS
-inform DER|PEM the input format. DER is binary format and PEM (the default) is base64 encoded. -in filename the input file, default is standard input -out filename output file to place the DER encoded data into. If this option is not present then no data will be output. This is most useful when combined with the -strparse option. -noout don't output the parsed version of the input file. -offset number starting offset to begin parsing, default is start of file. -length number number of bytes to parse, default is until end of file. -i indents the output according to the "depth" of the structures. -oid filename a file containing additional OBJECT IDENTIFIERs (OIDs). The format of this file is described in the NOTES section below. -strparse offset parse the contents octets of the ASN.1 object starting at offset. This option can be used multiple times to "drill down" into a nested structure. -genstr string, -genconf file generate encoded data based on string, file or both using ASN1_generate_nconf(3) format. If file only is present then the string is obtained from the default section using the name asn1. The encoded data is passed through the ASN1 parser and printed out as though it came from a file, the contents can thus be examined and written to a file using the out option. OUTPUT The output will typically contain lines like this: 0:d=0 hl=4 l= 681 cons: SEQUENCE ..... 229:d=3 hl=3 l= 141 prim: BIT STRING 373:d=2 hl=3 l= 162 cons: cont [ 3 ] 376:d=3 hl=3 l= 159 cons: SEQUENCE 379:d=4 hl=2 l= 29 cons: SEQUENCE 381:d=5 hl=2 l= 3 prim: OBJECT :X509v3 Subject Key Identifier 386:d=5 hl=2 l= 22 prim: OCTET STRING 410:d=4 hl=2 l= 112 cons: SEQUENCE 412:d=5 hl=2 l= 3 prim: OBJECT :X509v3 Authority Key Identifier 417:d=5 hl=2 l= 105 prim: OCTET STRING 524:d=4 hl=2 l= 12 cons: SEQUENCE ..... This example is part of a self signed certificate. Each line starts with the offset in decimal. d=XX specifies the current depth. The depth is increased within the scope of any SET or SEQUENCE. hl=XX gives the header length (tag and length octets) of the current type. l=XX gives the length of the contents octets. The -i option can be used to make the output more readable. Some knowledge of the ASN.1 structure is needed to interpret the output. In this example the BIT STRING at offset 229 is the certificate public key. The contents octets of this will contain the public key information. This can be examined using the option -strparse 229 to yield: 0:d=0 hl=3 l= 137 cons: SEQUENCE 3:d=1 hl=3 l= 129 prim: INTEGER :E5D21E1F5C8D208EA7A2166C7FAF9F6BDF2059669C60876DDB70840F1A5AAFA59699FE471F379F1DD6A487E7D5409AB6A88D4A9746E24B91D8CF55DB3521015460C8EDE44EE8A4189F7A7BE77D6CD3A9AF2696F486855CF58BF0EDF2B4068058C7A947F52548DDF7E15E96B385F86422BEA9064A3EE9E1158A56E4A6F47E5897 135:d=1 hl=2 l= 3 prim: INTEGER :010001 NOTES
If an OID is not part of OpenSSL's internal table it will be represented in numerical form (for example 1.2.3.4). The file passed to the -oid option allows additional OIDs to be included. Each line consists of three columns, the first column is the OID in numerical format and should be followed by white space. The second column is the "short name" which is a single word followed by white space. The final column is the rest of the line and is the "long name". asn1parse displays the long name. Example: "1.2.3.4 shortName A long name" EXAMPLES
Parse a file: openssl asn1parse -in file.pem Parse a DER file: openssl asn1parse -inform DER -in file.der Generate a simple UTF8String: openssl asn1parse -genstr 'UTF8:Hello World' Generate and write out a UTF8String, don't print parsed output: openssl asn1parse -genstr 'UTF8:Hello World' -noout -out utf8.der Generate using a config file: openssl asn1parse -genconf asn1.cnf -noout -out asn1.der Example config file: asn1=SEQUENCE:seq_sect [seq_sect] field1=BOOL:TRUE field2=EXP:0, UTF8:some random string BUGS
There should be options to change the format of output lines. The output of some ASN.1 types is not well handled (if at all). SEE ALSO
ASN1_generate_nconf(3) 1.0.1e 2013-02-11 ASN1PARSE(1SSL)
All times are GMT -4. The time now is 11:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy