Finding out the operating system version

Discussion in 'Developers' Forum' started by Taleman, Aug 24, 2019.

  1. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    I plan to enhance the htf-common-issues.php script (from http://gitplace.net/pixcept/ispconfig-tools/blob/master/htf-common-issues.php) so it writes also the operating system name and version. I have a short test script to verify how the OS version can be determined on different systems. It works well on Ubuntu and Debian systems since they have lsb_release installed by default (at least on all systems I have checked). But for other systems I would like help.
    So run the attached PHP script and post result here. The script is hopefully simple to check it does not send me your password file or do other nasty stufs.
    PHP:
    #!/usr/bin/php -q

    <?php

    /* Test which operation system script is running on
       Taleman 2019
    */

    ini_set('display_errors''On');

    $lsbrel='/usr/bin/lsb_release';
    $rhatrel '/etc/redhat-release';
    $os_version '';
    /* echo "Next is if\n"; */
    if (file_exists($lsbrel)) {
     
      echo 
    "Found lsbrel\n";
      
    $os_version shell_exec($lsbrel.' -d');
    } elseif (
    file_exists($rhatrel)) {
      
    /* echo "redhatrel does exist\n"; */
      
    $os_version shell_exec('cat '.$redhatrel);
    } else {
      
    $lslisting shell_exec('ls -lhd /etc/*versio* /etc/*releas* /etc/*issue*');
    }
    if (
    $os_version == '') {
      echo 
    "ls listing:\n".$lslisting;
    } else {
      echo 
    "OS version is ".$os_version;
    }
     
  2. Steini86

    Steini86 Member

    I just tried something similar in a project. I found out that the file "/etc/os-version" should be the new standard (at least it is used by systemd systems [1]). It should work on recent Debian, OpenSUSE, Fedora, ArchLinux..
    Someone already shared a code snipped to read that file into an array[2]
    Hope this helps

    [edit]
    Sorry, wanted to share the links but the system doesn't like me yet. Links are:
    Code:
    [1] www.freedesktop.org/software/systemd/man/os-release.html
    [2] https://stackoverflow.com/a/42397673
    
     
  3. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Thanks, this does help.
    But [1] you referred says the name of the file is /etc/os-release, not os-version. On Debian 9.9 at least that file does not show the subversion, it only says it is Debian 9.
    I made version 2 that examines /etc/os-release if info has not been found before that. On my Debian 9.9 this writes:
    Code:
    $ ./os-version-check.php
    
    Found lsbrel
    This scipt is version 2
    OS version is Description:    Debian GNU/Linux 9.9 (stretch)
    
    Here is the new code:
    PHP:
    #!/usr/bin/php -q

    <?php

    /* Test which operation system script is running on
       Taleman 2019
    */

    ini_set('display_errors''On');

    $progversion="2";
    $lsbrel='/usr/bin/lsb_release';
    $rhatrel '/etc/redhat-release';
    $osrelease='/etc/os-release';
    $os_version '';
    /* echo "Next is if\n"; */
    if (file_exists($lsbrel)) {
     
      echo 
    "Found lsbrel\n";
      
    $os_version shell_exec($lsbrel.' -d');
    } elseif (
    file_exists($rhatrel)) {
        
    /* echo "redhatrel does exist\n"; */
        
    $os_version shell_exec('cat '.$redhatrel);
    } elseif (
    file_exists($osrelease)) {
        
    $os_version shell_exec('cat '.$osrelease.' | grep "PRETTY_NAME"');
    } else {
        
    $lslisting shell_exec('ls -lhd /etc/*versio* /etc/*releas* /etc/*issue*');
    }
    echo 
    "This scipt is version ".$progversion."\n";
    if (
    $os_version == '') {
      echo 
    "ls listing:\n".$lslisting;
    } else {
      echo 
    "OS version is ".$os_version;
    }
    ?>
     
  4. ahrasis

    ahrasis Well-Known Member

    ISPConfig monitor php file should have that code.
     
    Taleman likes this.
  5. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    I found the code in ISPConfig source, but that is very complicated and long. There was, however, /etc/*release files for a few additional operating systems, so useful find.
    Meanwhile, this is from Debian Buster without lsb_release installed:
    Code:
    [email protected]:/tmp# ./foo.php
    
    This scipt is version 2
    OS version is PRETTY_NAME="Debian GNU/Linux 10 (buster)"
    [email protected]:/tmp# type -a lsb_release
    -bash: type: lsb_release: not found
    
     
  6. ahrasis

    ahrasis Well-Known Member

    May be a one line code
    Code:
    lsb_release -ds 2>/dev/null || awk -F= '$1=="PRETTY_NAME" { print $2 ;}' /etc/*elease | tr -d '"'
    Or like this:
    Code:
    <?php
    /* Test which operation system script is running on
        (c) Taleman 2019
    */
    
    ini_set('display_errors', 'On');
    
    $progversion="2";
    $os_version = shell_exec('lsb_release -ds 2>/dev/null || awk -F= \'$1==\"PRETTY_NAME\" { print $2 ;}\' /etc/*elease | tr -d \'\"\'');
    $lslisting = shell_exec('ls -lhd /etc/*versio* /etc/*releas* /etc/*issue*');
    echo "This scipt is version: ".$progversion."\n";
    if (!empty($os_version))
        echo "OS version is: ".$os_version;
    else
        echo "ls listing:\n".$lslisting;
    ?>
    
    Tested only in debian / ubuntu. I don't know if this will work on other linux.

    Edited: I dropped cat /etc/redhat-release from the suggestion as redhat or itsderivative also seems to use os_release.
     
    Last edited: Aug 27, 2019
  7. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Here is version 3 of the script. I added Gentoo and SuSE detection after looking at the code in ISPConfig. os-release is used as last resort, since it does not show the subversion of the OS.
    Code:
    PHP:
    #!/usr/bin/php -q

    <?php

    /* Test which operation system script is running on
       Taleman 2019
    */

    ini_set('display_errors''On');

    $progversion="3";
    $lsbrel='/usr/bin/lsb_release';
    $rhatrel '/etc/redhat-release';
    $osrelease='/etc/os-release';
    $suserel '/etc/SuSE-release';
    $gentoorel '/etc/gentoo-release';

    $os_version '';
    /* echo "Next is if\n"; */
    if (file_exists($lsbrel)) {
      echo 
    "Found lsbrel\n";
      
    $os_version shell_exec($lsbrel.' -d');
    } elseif (
    file_exists($rhatrel)) {
        
    /* echo "redhatrel does exist\n"; */
        
    $os_version shell_exec('cat '.$redhatrel);
    } elseif (
    file_exists($suserel)) {
        
    $os_version shell_exec('cat '.$suserel);
    } elseif (
    file_exists($gentoorel)) {
        
    $os_version shell_exec('cat '.$gentoorel);
    } elseif (
    file_exists($osrelease)) {
        
    $os_version shell_exec('cat '.$osrelease.' | grep "PRETTY_NAME"');
    } else {
        
    $lslisting shell_exec('ls -lhd /etc/*versio* /etc/*releas* /etc/*issue*');
    }
    echo 
    "This scipt is version ".$progversion."\n";
    if (
    $os_version == '') {
      echo 
    "ls listing:\n".$lslisting;
    } else {
      echo 
    "OS version is ".$os_version;
    }
    ?>
     
    ahrasis likes this.
  8. Croydon

    Croydon ISPConfig Developer ISPConfig Developer

    lsb_release has a -s argument to not show the label. This might be better to use.
     
    ahrasis likes this.
  9. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Good grief, had not noticed that. Fixed that in gitplace.
     
    ahrasis likes this.
  10. ahrasis

    ahrasis Well-Known Member

    I did mention that in my suggestion.

    @Taleman I already tested your code in Ubuntu 18.04 and it is working fine.

    But I also consider your code is a bit long since usingl sb_release -ds or checking os_release is quite a standard and sufficient while checking others has become quite obsolete and unnecessary.
     
    Last edited: Aug 31, 2019
  11. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    I started writing this addition to htf-common-issues after several times willing to help ISPConfig users but not knowing what OS they have seeing that helping is unnecessarily difficult. So I wanted someting that always shows what OS is involved, so I copied the alternatives from ISPConfig source. I agree that Debian and Ubuntu together are the majority of ISPConfig installations, and adding CentOS the proportion is close to 100%. But I find it is not that many lines of code and it should show something useful in all cases.
    I am not a PHP coder, I stopped with PHP when PHP 5 came out and all my PHP 4 code needed rewriting. So the code most likely could be better, but at least now it is not very complicated.
     

Share This Page