Cmd 'cat /dev/urandom' not closing cleanly


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Cmd 'cat /dev/urandom' not closing cleanly
# 1  
Old 03-22-2017
Cmd 'cat /dev/urandom' not closing cleanly

Hi

I'm running the following command to generate a random password in a KSH script on a RHEL Linux VM but for some reason the cmd is not being closed and it's causing problems on the host.

Code:
PASSWORD="$(cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 16 | head -1)Aa0!"

The code worked as expected but the side effects were definitely unexpected. Any idea how I can enhance this to ensure the urandom stream gets closed after it's been called and therefore prevent the server from becoming overloaded?
Many thanks.
# 2  
Old 03-22-2017
/dev/random supplies an seemingly infinite number of random bytes (in fact limited to 32MB since linux 3.16), so your cat won't finish soon if at all,as the random generator won't send an EOF.
Quote:
/dev/random will return at most 512 bytes
(c.f. man random), so you might want to read that, of limit the byte count yourself, e.g. by using
Code:
dd if=/dev/urandom count=40 bs=1

in lieu of cat. Or, why not use bash's $RANDOM variable?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-27-2017
You are right that head should send a SIGPIPE to fold that should send a SIGPIPE to tr that should send a SIGPIPE to cat.
And each program in the chain should terminate.
It's not reliable though, a signal *can* get lost, especially if the system is very busy. If this also happens on an idle system then there is a bug somewhere (in one of the programs or in the shell's pipe handling).
In one of my old scripts (using /bin/sh for many LUnix platforms) I had limited it with
Code:
dd if=/dev/urandom dd bs=1 count=12000 | tr ...

and, for safety, if the resul string was too short, I repeated it in a loop.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. HP-UX

Dev/urandom and dev/random missing in HP-UX

Hi, In our HP-UX B.11.11. I could not find dev/urandom and dev/random Are all pseudo-devices implemented as device drivers, or in need to run /configure some package to install the package to have dev/urandom. Please help (4 Replies)
Discussion started by: rashi
4 Replies

2. UNIX for Beginners Questions & Answers

Cleanly log out a user w/o killing process

My AIX-based accounting program runs an end-of-day process that requires all users to be logged out. Sometimes I have a user that leaves for the day and forgets to logout. Is there any way I can cleanly log a user out without killing their process? (4 Replies)
Discussion started by: DLS1701
4 Replies

3. Shell Programming and Scripting

Memory fault(coredump) while using cat cmd

i have few log files that are input to my perl script... i am executing the script as below cat RTR*.log | test.pl and getting the following error -ksh: 25014: Memory fault(coredump) cat: write error: Connection reset by peer can anyone help me on this.... Thanks in... (2 Replies)
Discussion started by: niteesh_!7
2 Replies

4. Shell Programming and Scripting

Error output of cat to /dev/null

Hello, I'm trying to send the error output of a 'cat' operation to /dev/null like this: cat /dirA/dirB/temp*.log > /dirA/dirB/final.log 2>/dev/null This works perfectly in a terminal, but not when placed in a script. If there are no files matching temp*.log the script outputs an error... (7 Replies)
Discussion started by: Nils88
7 Replies

5. Slackware

Can't play sound files with aplay or cat to /dev/dsp

I am having problems using soundes. Until a few moments ago the following commands produced errors and no sound: cat /usr/share/apps/kolf/sounds/blackhole.wav/ > /dev/dsp yielded: /dev/dsp: Invalid argument cat /usr/share/apps/kolf/sounds/blackhole.wav > /dev/audio yelded: /dev/audio:... (3 Replies)
Discussion started by: slak0
3 Replies

6. UNIX for Dummies Questions & Answers

cat /dev/null

Hi, Excuse my ignorance here - I'm a networks man and my knowledge of all things unix is somewhat limited. We have a very large file (/var/tmp/mond.log) that we need to zero - does the "cat /dev/null > /var/tmp/mond.log" command achieve this? (4 Replies)
Discussion started by: freakydancer
4 Replies

7. Solaris

What is /dev/tty /dev/null and /dev/console

Hi, Anyone can help My solaris 8 system has the following /dev/null , /dev/tty and /dev/console All permission are lrwxrwxrwx Can this be change to a non-world write ?? any impact ?? (12 Replies)
Discussion started by: civic2005
12 Replies

8. UNIX for Dummies Questions & Answers

/dev/urandom

Hi, how do I use /dev/urandom to generate a single number between 1-100? I can od /dev/urandom but it gives me an endless list of random numbers, I just want 1 between 1-100. How can I get that? Thanks. (12 Replies)
Discussion started by: Takumi
12 Replies
Login or Register to Ask a Question