Question: How do I get my IF loops into my assignment3 database? I am having issues with my WHILE statement. Here is my script below... #
How do I get my IF loops into my assignment3 database? I am having issues with my WHILE statement. Here is my script below...
# Week 3 Assignement 1;
use strict; use warnings;
use DBD::SQLite;
# SQLite database configurations; my $username = "root"; my $password = ""; my $dbh = DBI->connect('DBI:SQLite:assignment3.db3',$username,$password,\my%attr);
# Create data tables and insert rows into the tables; my @tables = ( # Create system information table; "CREATE TABLE IF NOT EXISTS system_information ( Machine_name TEXT, Kernel_version TEXT, Product_type TEXT, Product_version TEXT, Registered_organization TEXT, Registered_owner TEXT, System_root TEXT, Processors TEXT, Physical_memory TEXT );", # Create software installed table; "CREATE TABLE IF NOT EXISTS software_installed ( Machine_Name TEXT, Software_Installed TEXT );", # Create process list table; "CREATE TABLE IF NOT EXISTS process_list ( Machine_Name TEXT, Process_Name TEXT, PID TEXT )");
# Execute all create table statements; for my $sql(@tables){ $dbh->do($sql); }
print "All tables created successfully! ";
# Execute the program and arguments for psinfo and pslist; my $assignment3; my $count; my @program; my $program; @program = `psinfo.exe -S`; my $kernel_version; my @array1; my $machine_name; my $product_type; my $product_version; my $registered_organization; my $registered_owner; my $system_root; my $processors; my $physical_memory; my $applications_only = 0;
foreach my $program (@program) { $program =~ tr/\x00//d; # Machine Name; if ($program =~ m/System information for/) { @array1 = split /\\/, $program; $machine_name = pop @array1; print "==> $machine_name "; } # Kernel Version; if ($program =~ m/Kernel version/) { $kernel_version = (split (/:/, $program))[1]; $kernel_version =~ s/^\s+|\s+$//g; print "==> $kernel_version "; } # Product Type; if ($program =~ m/Product type/) { $product_type = (split (/:/, $program)) [1]; $product_type =~ s/^\s+|\s+$//g; print "==> $product_type "; } # Product Version; if ($program =~ m/Product version/) { $product_version = (split (/:/, $program)) [1]; $product_version =~ s/^\s+|\s+$//g; print "==> $product_version "; } # Registered Organization; if ($program =~ m/Registered organization/) { $registered_organization = (split (/:/, $program)) [1]; $registered_organization =~ s/^\s+|\s+$//g; print "==> $registered_organization "; } # Registered Owner; if ($program =~ m/Registered owner/) { $registered_owner = (split (/:/, $program)) [1]; $registered_owner =~ s/^\s+|\s+$//g; print "==> $registered_owner "; } # System Root; if ($program =~ m/System root/) { $system_root = (split (/:/, $program)) [1]; $system_root =~ s/^\s+|\s+$//g; print "==> $system_root "; } # Processors; if ($program =~ m/Processors/) { $processors = (split (/:/, $program)) [1]; $processors =~ s/^\s+|\s+$//g; print "==> $processors "; } # Physical Memory; if ($program =~ m/Physical memory/) { $physical_memory = (split (/:/, $program)) [1]; $physical_memory =~ s/^\s+|\s+$//g; print "==> $physical_memory "; } # Applications; if ($applications_only ==1) { print "==> $program "; } else { if ($program =~ m/Applications/) { print "==> $program"; $applications_only = 1; # Insert into database; # This is where I am running into an issue; while(<@program>){ print "$_ "; $assignment3 = "Insert into system_information (Machine_name, Kernel_version, Product_type, Product_version, Registered_organization, Registered_owner, System_root, Processors, Physical_memory) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"; my $sth = $dbh->prepare($assignment3) or die "Prepare failed: " . $dbh->errstr(); $sth->execute($machine_name, $kernel_version, $product_type, $product_version, $registered_organization, $registered_owner, $system_root, $processors, $physical_memory); } } } }
my @program; @program = `pslist.exe`;
$applications_only = 0;
my @array2; my $name; my $name1; my $name2;
foreach my $program (@program) { $program =~ tr/\x00//d; # Machine Name; if ($program =~ m/Process information for/) { @array1 = split /\s+/, $program; my $machine_name = pop @array1; print "==> $machine_name "; } # Process, PID; if ($program =~ m/Name/) { print "==> $program"; $applications_only = 1; } if ($program =~ m/Name/) { $applications_only = 1; } if ($applications_only == 1) { @array1 = (split (/\s+/, $program))[0]; @array2 = (split (/\s+/, $program))[1]; $name1 = pop @array1; $name2 = pop @array2; print "==> $name1, $name2 "; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
