Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Detect Plugged USB Flash Drive / CD in CDROM Drive

by renegadex (Beadle)
on Nov 28, 2008 at 04:40 UTC ( #726512=note: print w/ replies, xml ) Need Help??


in reply to Detect Plugged USB Flash Drive / CD in CDROM Drive

Hi! I tried to extract messages from the etc/mtab. Here's what I did, its very simple. I'm just beggining to learn RegEx, please bear with me hehe..

sub scan_cddvd { # mtab file /etc/mtab # cd/dvd signature /dev/sr0 open(MYINPUTFILE, "</etc/mtab"); my $cddvd_path = 'not found'; while(<MYINPUTFILE>){ my($line) = $_; chomp($line); if($line =~ m/^\/dev\/sr/){ print "FOUND CD/DVD\n"; print $line , "\n"; #$line =~ /(\/media\/.*)\ /; $line =~ /(\/media\/\S+)\s+/; my $newline = $1; print $newline , "\n"; while($newline =~ m/\\040/){ $newline =~ s/\\040/ /; } print $newline , "\n"; $cddvd_path = $newline } } close MYINPUTFILE; return $cddvd_path; }
I made a timer of some sort and calls this function to determine if a CD / DVD is inside the drive.works pretty ok. :D hehe. I would be applying the same concept with usb flash drives. Instead of looking for /dev/sr, I'd be looking for /dev/sdb hehe.hope it would work just fine.
Mabuhay Civil Engineers! :D


Comment on Re: Detect Plugged USB Flash Drive / CD in CDROM Drive
Download Code
Replies are listed 'Best First'.
Re^2: Detect Plugged USB Flash Drive / CD in CDROM Drive
by Perlbotics (Canon) on Nov 28, 2008 at 12:08 UTC

    Your snipped might work for your PC only and you'll only see the last /dev/sr device - which might not be a CD or DVD at all. The following suggestion is only a tiny improvement:

    use strict; # returns list of CD/DVD mountpoints or empty list sub scan_cddvd { my @drives = (); my $mtabfile = "/etc/mtab"; open my $mtfh, "<", $mtabfile or die "cannot open $mtabfile - $!"; while( <$mtfh> ){ my ($dev, $mountpoint, $fstype, $options) = split /\s+/; next unless $fstype =~ /udf|iso\d+/ && $options =~ /ro\b/; #ro: the /ro/-test above should skip CDRW too $mountpoint =~ s/\\(0\d+)/chr(oct($1))/eg; # resolve oct chars push @drives, $mountpoint; } close $mtfh; return @drives; } print "o <", join(">\no <", scan_cddvd), ">\n";
    Things to note: uses strict; avoids "not found" as a special failure-indicator; returns list of mountpoints; expands all octal escapes; less HW-dependent (e.g. IDE, SCSI, SATA, ...), e.g. not restricted to /dev/sdb; tries a little harder to find a CD or DVD (iso/udf,ro), uses 3 param open and die; no globs (MYINPUTFILE); implicit use of $_; will fail for derviant mtab-formats; ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://726512]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2015-08-01 01:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls