Piping data to multiple processes
Here’s a simple shell script to stream data to multiple processes. It has many applications, but the reason I wrote it is to stream the same data to multiple netcat processes on remote machines.
Here’s the code:
#!/bin/bash
usage()
{
cat <&2
usage : multi-fifo target0 [target1 [target2 [...]]]
Where each targetN is a program you want to send the input multi-fifo receives
EOF
}
[ $# -eq 0 ] && usage && exit 1
i=0
pipeline="tee /dev/null"
while [ -n "$1" ]; do
target=$1
fifo=/tmp/multi-fifo-$$.$i
mkfifo $fifo
eval "$target < $fifo" &
pipeline="$pipeline | tee $fifo"
i=$((i+1))
shift
done
eval "cat | $pipeline"
j=0
while [ $j -lt $i ]; do
rm -f /tmp/multi-fifo-$$.$j
j=$((j+1))
done
Usage is pretty straightforward:
tar cjvf - . | ./multi-fifo "nc host1 9999" "nc host2 9999" >/dev/null
This way, the script will create a fifo for each netcat client, then send it’s stdin to a pipeline that writes to both fifos using tee. Useful for example if you want to stream a backup to multiple destination servers for cloning.
Related posts:
- Using the ENUM data type to increase performance While going through the DATA TYPES section of the Certification...
- Generating data with dbmonster In my last post I included some sample data...
- Running commands from the shell with a timeout (pt 2) Here’s an improved version of the safecmd script. This one...
Related posts brought to you by Yet Another Related Posts Plugin.
Topics:
Programming |
Comments Off










