Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # regular check of hardware, if new hardware is found data is uploaded
- # to Linux-Hardware-Guide database
- # Currently only checking for new PCI and USB devices
- $sid = $ARGV[0];
- $upload_server="http://library.linux-hardware-guide.com";
- $TMPDIR = "$ENV{HOME}/.tmp";
- # Check for cached uploads - if found upload now
- check_missed_uploads();
- # Flag, if new hardware is found
- $found_new_hardware = 0;
- # check for first run
- check_first_run();
- check_root();
- check_spool();
- # ToDo: check if last upload failed -> $found_new_hardware = 1;
- create_fingerprint_pci();
- create_fingerprint_usb();
- #print "PCI: \n";
- #print "Known: @known_pci_array \n";
- #print "Scan: @foundarray_pci";
- #print "\nUSB: \n";
- #print "Known: @known_usb_array \n";
- #print "@foundarray_usb";
- check_for_new_ids();
- if ($found_new_hardware == 1) {
- upload_data();
- } else {
- print "No new hardware found \n";
- }
- make_clean();
- exit 0;
- #
- # create_fingerprint_pci
- #
- # read pci IDs of hardware
- #
- sub create_fingerprint_pci {
- # modified by Charles K Barcza <blackPanther OS> because the
- #/var/tmp or $ENV{HOME}/tmp better if system use tmpfs!
- system ("mkdir -p $TMPDIR/.LHG_".$sid);
- system ("lspci -nnk > $TMPDIR/.LHG_".$sid."/lspci.txt");
- open(FILE, "<", "$TMPDIR/.LHG_".$sid."/lspci.txt");
- open(FILEN, "<", "$TMPDIR/.LHG_".$sid."/lspci.txt");
- $nextline=<FILEN>;
- $i=0;
- #print "PCI Fingerprint: \n";
- while ( <FILE> ) {
- $nextline = <FILEN>;
- #print "Line: $_";
- $pciid = grab_pciid($_);
- if ($pciid != "") {
- #print "PCIID: $pciid \n";
- $subsystemid = grab_subsystemid($nextline);
- #print "Subsystem: $subsystemid \n";
- }
- if ($pciid != "") {
- #print "$pciid,";
- @foundarray_pci = (@foundarray_pci, $pciid);
- }
- #
- #
- }
- #print "\n";
- }
- sub grab_subsystemid {
- my $line = shift;
- #print "-------\nLine: $line ";
- if ( ($line =~ m/Subsystem/) and ($line =~ m/\w\w\w\w:\w\w\w\w/) ) {
- my $pciid = substr($line,$-[0],9);
- #print "PCIID: $pciid \n";
- return $pciid;
- } else {
- #print " no match \n";
- }
- }
- #
- #
- ### USB subroutines
- #
- #
- sub create_fingerprint_usb {
- system ("lsusb > $TMPDIR/.LHG_".$sid."/lsusb.txt");
- open(FILE, "<", "$TMPDIR/.LHG_".$sid."/lsusb.txt");
- $i=0;
- #print "USB Fingerprint: \n";
- while ( <FILE> ) {
- #print "Line: $_ \n";
- $usbid = grab_usbid($_);
- #$usbname = grab_usbname($_);
- if ($usbid ne "") {
- #print "$usbid,";
- @foundarray_usb = (@foundarray_usb, $usbid);
- }
- $i++;
- #print "\n";
- }
- }
- sub grab_usbid {
- $line = shift;
- #print "-------\nLine: $line ";
- if ($line =~ m/\w\w\w\w:\w\w\w\w/) {
- return substr($line,$-[0],9);
- } else {
- #print " no match \n";
- }
- }
- sub grab_pciid {
- $line = shift;
- #print "-------\nLine: $line ";
- if ( ($line =~ m/\w\w\w\w:\w\w\w\w/) and !($line =~ m/Subsystem/) ) {
- $pciid = substr($line,$-[0],9);
- #print "PCIID: $pciid \n";
- return $pciid;
- } else {
- #print " no match \n";
- }
- }
- sub check_root {
- if ( $< != 0 ) {
- #print "WARNING:\n";
- #print "========\n";
- print "Sorry! This script needs to be running with root privileges. ";
- print "\n";
- exit 1;
- }
- }
- sub check_spool {
- if ( -e "/var/spool/lhg-tools/pcidata" ) {
- #print "pcidata exists";
- # read in known data from spool file
- @known_pci_array = lhg_readdata("/var/spool/lhg-tools/pcidata");
- } else {
- #print "pcidata does not exist";
- $found_new_hardware = 1;
- system ("mkdir -p /var/spool/lhg-tools/");
- system ("touch /var/spool/lhg-tools/pcidata");
- system ('echo "# List of PCI data \n" >> /var/spool/lhg-tools/pcidata');
- }
- if ( -e "/var/spool/lhg-tools/usbdata" ) {
- #print "usbdata exists";
- @known_usb_array = lhg_readdata("/var/spool/lhg-tools/usbdata");
- } else {
- #print "usbdata does not exist";
- $found_new_hardware = 1;
- system ("mkdir -p /var/spool/lhg-tools/");
- system ("touch /var/spool/lhg-tools/usbdata");
- system ('echo "# List of USB data \n" >> /var/spool/lhg-tools/usbdata');
- # read in known data from spool file
- }
- }
- sub make_clean {
- # ToDo: cleanup files
- }
- sub lhg_readdata {
- $FILE = shift;
- @data_array = ();
- open(FILE, $FILE);
- while (<FILE>) {
- if (substr($_,0,1) eq "#") {
- # skip comment lines
- } else {
- $id = $_;
- $id =~ s/(\r)|(\n)//g;
- @data_array = (@data_array, $id);
- }
- }
- # ToDo: read data from file
- return @data_array;
- }
- sub check_for_new_ids {
- #print "\n";
- # compare PCI IDs
- foreach (@foundarray_pci)
- {
- $searchfor = $_ ;
- #print "checking $searchfor ...";
- #if ($_ ~~ @known_pci_array) {
- if ( grep( /^${searchfor}/, @known_pci_array ) ) {
- #print "found \n";
- }else {
- #print "not found \n";
- $found_new_hardware = 1;
- # add to file
- system ('echo "'.$searchfor.'" >> /var/spool/lhg-tools/pcidata');
- }
- }
- # compare USB IDs
- foreach (@foundarray_usb)
- {
- $searchfor = $_ ;
- #print "checking $searchfor ...";
- #if ($_ ~~ @known_usb_array) {
- if ( grep( /^${searchfor}/, @known_usb_array ) ) {
- #print "found \n";
- }else {
- #print "not found \n";
- $found_new_hardware = 1;
- # add to file
- system ('echo "'.$searchfor.'" >> /var/spool/lhg-tools/usbdata');
- }
- }
- }
- sub upload_data {
- my $exit_val;
- my $pid;
- print "found new HW -> starting upload \n";
- if ( -e "./scan_hardware") {
- print "executing HW scan \n";
- $pid = fork;
- die "Fork failed: $!" unless defined $pid;
- unless ($pid) {
- #exec "./long.pl"; # Some long running process.
- exec ("./scan_hardware");
- }
- }elsif ( -e "/usr/bin/scan_hardware") {
- print "executing HW scan \n";
- $pid = fork;
- die "Fork failed: $!" unless defined $pid;
- unless ($pid) {
- #exec "./long.pl"; # Some long running process.
- #exec ("./scan_hardware");
- exec ("/usr/bin/scan_hardware");
- }
- }
- #wait for children to finish
- waitpid $pid, 0;
- #print "PID was: $pid \n";
- $exit_val = $? >> 8;
- #print "Exit Val: $exit_val \n";
- if ($exit_val > 0) {
- # upload did not work. store for later
- my @chars = (0 .. 9, "A".."Z", "a".."z");
- my $rndstring;
- $rndstring .= $chars[rand @chars] for 1..8;
- system ("mkdir -p /var/spool/lhg-tools/missed_uploads.$rndstring");
- system ("cp $TMPDIR/.LHG_$pid/* /var/spool/lhg-tools/missed_uploads.$rndstring/");
- } else {
- # ToDo: upload done, also upload missed files
- }
- }
- sub check_first_run {
- $conf_filename = '/etc/lhg-tools.conf';
- $pci_filename = '/var/spool/lhg-tools/pcidata';
- if ( (-e $conf_filename) && (-e $pci_filename) ){
- # files exists, program was run before
- }else {
- #enforcing hardware scan
- $found_new_hardware = 1;
- }
- }
- sub check_missed_uploads {
- @files = glob("/var/spool/lhg-tools/missed_uploads.*");
- foreach (@files) {
- # do something
- #print "uploading $_ ";
- upload_cached_files($_);
- }
- }
- sub upload_cached_files {
- my $TMPDIR = shift;
- my $fname = $TMPDIR;;
- $fname =~ s/\/var\/spool\/lhg-tools\///;
- my @allfiles = ("alsa_cards.txt","alsa_devices.txt","aplay.txt","cpuinfo.txt",
- "dd.err.txt","dd.txt","dmesg.txt","lsb_release.txt","lspci.txt",
- "lsusb.txt","lsusb_v.err.txt","lsusb_v.txt","scanimage.txt",
- "version.txt","hdparm.txt","hdparm_direct.txt");
- my $sid;
- use LWP::UserAgent;
- use HTTP::Request::Common;
- #get session ID
- $request = HTTP::Request->new(GET => $upload_server."/uploadrequest.php?v=0.2");
- $ua = LWP::UserAgent->new;
- $response = $ua->request($request);
- $sid = $response->content;
- #$ua = LWP::UserAgent->new;
- #upload files
- $response = $ua->request(POST $upload_server."/uploadfiles.php",
- Content_Type => 'form-data',
- Content => [ sid => $sid,
- file1 => ["$TMPDIR/".$allfiles[0]],
- file2 => ["$TMPDIR/".$allfiles[1]],
- file3 => ["$TMPDIR/".$allfiles[2]],
- file4 => ["$TMPDIR/".$allfiles[3]],
- file5 => ["$TMPDIR/".$allfiles[4]],
- file6 => ["$TMPDIR/".$allfiles[5]],
- file7 => ["$TMPDIR/".$allfiles[6]],
- file8 => ["$TMPDIR/".$allfiles[7]],
- file9 => ["$TMPDIR/".$allfiles[8]],
- file10 => ["$TMPDIR/".$allfiles[9]],
- file11 => ["$TMPDIR/".$allfiles[10]],
- file12 => ["$TMPDIR/".$allfiles[11]],
- file13 => ["$TMPDIR/".$allfiles[12]],
- file14 => ["$TMPDIR/".$allfiles[13]],
- file15 => ["$TMPDIR/".$allfiles[14]],
- file16 => ["$TMPDIR/".$allfiles[15]],
- ]
- );
- print " -> ";
- print "(".$response->code.") :";
- print $response->content;
- print "\n";
- #print "DEBUG: mv /var/spool/lhg-tools/$fname /var/spool/lhg-tools/done_$fname";
- if ($response->code == HTTP::Status::HTTP_REQUEST_TIMEOUT) {
- print "Connection timeout. Upload failed \n";
- }elsif ($response->code == 500 ) {
- print "Bad Hostname, upload failed. Please check your internet connection. \n";
- }else{
- #print "upload successful";
- #print "mv /var/spool/lhg-tools/$fname /var/spool/lhg-tools/done_$fname";
- system "mv /var/spool/lhg-tools/$fname /var/spool/lhg-tools/done_$fname";
- }
- #exit 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement