ßł Ặck Ĥặwk ßł Ặck Ĥặwk - 6 months ago 70
Perl Question

Get Bios serial number in perl

I want to get the Bios serial number then check if it is exist in webpage content using Perl , I tried this code :

#!/usr/bin/perl
use LWP::Simple;
my $serial = qx(wmic bios GET SerialNumber 2>&1);
my $source = get("http://localhost/Check/serials.txt");
my $ff = "$serial";

if ("$source" =~ "$ff"){
print "Serial Found ^_^\n";
}else{
print "Sorry , Serial Not Found In Our Database !!\n";
}


* the URL
http://localhost/Check/serials.txt
have list of serial numbers.

But script always give me
Sorry , Serial Not Found In Our Database !!
but the serial number of my Bios is found !!

Answer

There are several issues here, all to do with the exact contents of various strings.

The WMIC command doesn't just hand back 61-1210-000747-00101111-071595-M747 because it's meant for a command-line interface and has to look nice in that context

Note that I can't test this directly as my Windows system is hand-built and has no serial number

Instead, it returns something like

"SerialNumber  \r\n61-1210-000747-00101111-071595-M747\r\n\r\n"

and searching for that in the serials.txt file on your intranet is pretty much bound to fail. You need to isolate the serial number from the surrounding text using something like this

my $response = qx(wmic bios GET SerialNumber);
my ($serial) = $response =~ /^SerialNumber\s+(\S+)/;

There is also a problem with using a regex pattern to search the contents of the serials.txt on your intranet. It would be best to parse the contents of that file properly, but you don't show what it looks like

Instead you can use \Q...\E in the regex pattern, so that any regex metacharacters that may be in the serial number are handled literally instead. You will als need to surround that in \b...\b so that a serial number like 123A isn't "validated" because it happens to be a substring of ABC-123AD-59F in the serials.txt file

This should work for you

#!/usr/bin/perl

use strict;
use warnings qw/ all FATAL /;

use LWP::Simple 'get';
use Data::Dump 'dump';

use constant SERIALS_URL => 'http://localhost/Check/serials.txt';


my $response = qx(wmic bios GET SerialNumber);
my ($serial) = $response =~ /^SerialNumber\s+(\S+)/
        or die sprintf qq{Unexpect response %s\n    from WMIC}, dump $response;


my $source = get SERIALS_URL or die;


if ( $source =~ /\b\Q$serial\E\b/ ) {
    print "Serial Found\n";
}
else{
    print qq{Serial number "$serial" not found in our database};
}