| 104 | rodolico | 1 | #! /usr/bin/env perl
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | use strict;
 | 
        
           |  |  | 4 | use warnings;
 | 
        
           |  |  | 5 |   | 
        
           | 123 | rodolico | 6 | # checks different things to try and figure out what operating system we are running
 | 
        
           |  |  | 7 | # pretty basic stuff, actually. Looks for the existence of a file or directory first.
 | 
        
           |  |  | 8 | # if it can not find one that matches, will then do a uname and attempt to locate a
 | 
        
           |  |  | 9 | # particular string in that.
 | 
        
           | 104 | rodolico | 10 |   | 
        
           | 123 | rodolico | 11 | # opnsense is freebsd, but does not have the same capabilities as freebsd, so we look for the file
 | 
        
           |  |  | 12 | # /conf/config.xml which appears to be uniq. We could also look for the /usr/local/opnsense directory
 | 
        
           |  |  | 13 | # if we don't find that, then we look in uname -a for debian or mx or devuan (it is a debian system),
 | 
        
           |  |  | 14 | # ipfire (it is an ipfire router) or freebsd (it is a freebsd system)
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 | # returns a string (debian, ipfire, freebsd or opnsense) without a line return on success.
 | 
        
           |  |  | 17 | # on failure, returns an empty string and a failure return code
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | our $VERSION = '1.0';
 | 
        
           |  |  | 20 |   | 
        
           | 104 | rodolico | 21 | my %osDefinitions = (
 | 
        
           |  |  | 22 |                   'debian' => {
 | 
        
           |  |  | 23 |                      'regex'  => '(debian|mx|devuan)',
 | 
        
           |  |  | 24 |                   },
 | 
        
           |  |  | 25 |                   'ipfire' => {
 | 
        
           |  |  | 26 |                      'regex'  => 'ipfire',
 | 
        
           |  |  | 27 |                   },
 | 
        
           |  |  | 28 |                   'freebsd' => {
 | 
        
           |  |  | 29 |                      'regex' => 'freebsd',
 | 
        
           |  |  | 30 |                   },
 | 
        
           |  |  | 31 |                   'opnsense' => {
 | 
        
           |  |  | 32 |                      'fileexists' => '/conf/config.xml',
 | 
        
           |  |  | 33 |                   },
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 |                 );
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 | # script which determines which operating system we are running on.
 | 
        
           |  |  | 38 | my $os = shift;
 | 
        
           |  |  | 39 | my $osType;
 | 
        
           |  |  | 40 | # first, look through our entries for fileexists
 | 
        
           |  |  | 41 | unless ( $os ) {
 | 
        
           |  |  | 42 |    foreach $osType ( keys %osDefinitions ) {
 | 
        
           |  |  | 43 |       next unless defined $osDefinitions{$osType}{'fileexists'};
 | 
        
           |  |  | 44 |       if (  -e $osDefinitions{$osType}{'fileexists'} ) {
 | 
        
           |  |  | 45 |          print $osType;
 | 
        
           |  |  | 46 |          exit;
 | 
        
           |  |  | 47 |       } # if
 | 
        
           |  |  | 48 |    } # foreach
 | 
        
           |  |  | 49 | } # unless
 | 
        
           |  |  | 50 | # next, look for uname and see if that gives us a clue
 | 
        
           |  |  | 51 | unless ( $os ) {
 | 
        
           |  |  | 52 |    my $osString = `uname -a`;
 | 
        
           |  |  | 53 |    foreach $osType ( keys %osDefinitions ) {
 | 
        
           |  |  | 54 |       next unless defined $osDefinitions{$osType}{'regex'};
 | 
        
           |  |  | 55 |       if ( $osString =~ m/$osDefinitions{$osType}{'regex'}/i ) {
 | 
        
           |  |  | 56 |          print $osType;
 | 
        
           |  |  | 57 |          exit;
 | 
        
           |  |  | 58 |       }
 | 
        
           |  |  | 59 |    } # foreach
 | 
        
           |  |  | 60 | } # unless
 | 
        
           |  |  | 61 | exit 0; # we were not able to determine operating system
 |