#!/usr/bin/perl print "################################\n"; print "# wiperand.pl #\n"; print "# ----------- #\n"; print "# #\n"; print "# File/Device wiper #\n"; print "# 2006 phormix.com/Tyler Aviss #\n"; print "# #\n"; print "# (you probably should not use #\n"; print "# this file unless you know #\n"; print "# what you are doing! I will #\n"; print "# not be held responsible if #\n"; print "# you erase something #\n"; print "# important!) #\n"; print "# #\n"; print "# Press CTRL+C now or continue #\n"; print "# at your own risk!!! #\n"; print "# #\n"; print "################################\n\n"; $start=`date`; $device=$ARGV[0]; $size=$ARGV[1]; $sizekb = $size/1000; $onepercent = $size / 100; $chunksize=10; if ($device eq "" ) { print "No device specified\n"; print "Usage $0 DEVICEorFILE SIZE\n\n"; exit; } if ($size < 1 ) { print "Invalid/missing size\n"; print "Usage $0 DEVICEorFILE SIZE\n\n"; exit; } main(); sub main { print "\n\n\n"; print "File $device will be overwritten with $size bytes ($sizekb " . "kb) of semi-random data. Continue?\n"; print "\n"; print "(y/n): "; while (($key=getc) ) { print "$key\n"; last if $key ne ""; }; if ( $key ne "y" ) { print "\nAborted by user\n\n"; exit; } #if ( -b $device ) #{ # print "$device is a device. This might be a partition or even a full disk. Are you REALLY sure you want to delete it?\n\n(y/n): "; # # while (($key2=getc) ) # { # print "$key2\n"; # last if $key2 ne ""; # }; # # if ( $key2 ne "y" ) # { # print "\nAborted by user\n\n"; # exit; # } #} if ( -f "/tmp/tmpfifo" ) { `rm -f /tmp/tmpfifo`; } `mkfifo /tmp/tmpfifo`; unless ($pid = fork) { unless (fork) { #print "Fork-->\n"; #exec "what you really wanna do"; print "Creating process to dump FIFO\n"; `dd if=/tmp/tmpfifo of=$device bs=1b count=$size`; print "End FIFO dump\n"; exit 0; } exit 0; } print "Done\n"; print "Creating process to generate random data\n"; open(OUTFIFO, '>/tmp/tmpfifo'); $written=0; print "-- Need to write $size bytes ($sizekb kb) of data...\n\n"; while ($size > 0) { if ($size < $chunksize) { $chunksize = $size; } $chars = generate_random_string($chunksize); print OUTFIFO $chars; $written += $chunksize; $size-=$chunksize; if ($written % 100000 == 0 ) { print "-- " . ($written/1000) . "kb written (" . ($size/1000) . " kb left)\n"; } } close (OUTFIFO); print "done randomizing process\n"; waitpid($pid,0); $end = `date`; print "Started $start\n"; print "Ended $end\n"; print "Generated and copied $size bytes ($sizekb kb) of semi-random data over $device\n"; } # This function generates random strings of a given length sub generate_random_string { my $length_of_randomstring=shift;# the length of # the random string to generate my @chars=('a'..'z','A'..'Z','0'..'9','_',"\n"); my $random_string; foreach (1..$length_of_randomstring) { # rand @chars will generate a random # number between 0 and scalar @chars $random_string.=$chars[rand @chars]; } return $random_string; }