Register
It is currently Thu Oct 30, 2014 10:38 am

Detecting OS script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 14 posts ] 
Author Message
 PostPosted: Mon Oct 15, 2007 11:37 am   

Joined: Mon Oct 15, 2007 11:34 am
Posts: 1
This script is used to detect the OS of a system so we can fetch other scripts to secure that type of OS.

The error I get now is [: too many arguments and I am stumped since I am new to bash scripting.

Code:
#!/bin/sh
#
# File:      postinstall.sh
#
# Purpose:   automate hardening by detecting OS
#
#
# Date:    Sat Oct  9 10:17 EST 2007
#
#
# License:   GPL version 2


if [ `uname -r | egrep '(6.2-RELEASE|6.1-RELEASE|5.5-RELEASE|6.1-STABLE|5.4-RELEASE|6.0-RELEASE|5.3-RELEASE|4.10-RELEASE|4.11-RELEASE)'` ]; then

   echo " Free BSD detected..."
   

   
   elif [ -f /etc/redhat-release ]; then

      
      var1[1]='Fedora'
      var1[2]='CentOS'
      var1[3]='Red hat'

      var2[1]=fedorapostinstall.sh
      var2[2]=Centospostinstall.sh
      var2[3]=Redhatpostinstall.sh 

      for i in `seq 1 3`;
           do
                 if [ `egrep ${var1[$i]} /etc/redhat-release` ]; then
            echo " ${var1[$i]} detected..."
            fi
           done   
           
   



   elif [ -f /etc/debian_version ]; then
      
      echo " Debian Base detected..."
      

   
fi

cd
rm postinstall.sh

exit 0


Top
 Profile  
 PostPosted: Fri Oct 19, 2007 4:33 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
This is a neat one! It's got to do with your "if" statement where you grep the redhat-release file. It returns the contents of /etc/redhat-release, which is too many arguments (seperated by spaces) for "if" to handle!

Basically, without the quotes, you're testing this:

Code:
if [ Red Hat Linux Advanced Server release 2.1AS (Slurm) ]; then


So it's trying to test each word, and it doesn't know what to do with that, so it freaks out! BUT if you put quotes around the whole thing, it treats it as a single argument and all is right with the world :)

If you put quotes around the whole thing, like so:

Code:
if [ "$(egrep "${var1[$i]}" /etc/redhat-release)" ]; then


then "if" is happy! You also needed some quotes around your variable, or else it treats both "Red" and "Hat" as seperate items. I also took some artistic license in using $() instead of `backticks` ;) Makes it easier for me to read.

I hope this helps!
-jeo


Top
 Profile YIM  
 PostPosted: Tue Apr 01, 2008 11:27 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
Here's a bit for Arch Linux:

Code:
if [ -f /etc/arch-release ]; then
    echo 'Arch Linux detected'
fi


Top
 Profile  
 PostPosted: Wed Apr 23, 2008 2:10 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Daenyth wrote:
Here's a bit for Arch Linux:

Code:
if [ -f /etc/arch-release ]; then
    echo 'Arch Linux detected'
fi



yeah......... we MUST have the Arch bit included ;)

(yes...... i love Arch Linux :D )


Top
 Profile WWW  
 PostPosted: Thu Jun 26, 2008 9:40 pm   

Joined: Tue Apr 01, 2008 12:33 pm
Posts: 2
you can always use something like: cat /proc/version_signature or cat /proc/version


Top
 Profile  
 PostPosted: Fri Jun 27, 2008 11:16 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
jeo wrote:
Code:
if [ "$(egrep "${var1[$i]}" /etc/redhat-release)" ]; then



Surely you mean
Code:
if egrep "${var1[$i]}" /etc/redhat-release >/dev/null; then


Top
 Profile  
 PostPosted: Mon Jun 30, 2008 2:42 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Daenyth wrote:
jeo wrote:
Surely you mean
Code:
if egrep "${var1[$i]}" /etc/redhat-release >/dev/null; then


>/dev/null is messy in these situations, I advise using grep's -q switch which stops output


Top
 Profile  
 PostPosted: Mon Jun 30, 2008 11:21 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
I'm aware of the -q switch, but this script is supposed to be portable -- some versions of grep do not support -q.


Top
 Profile  
 PostPosted: Wed Aug 27, 2008 9:21 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Okay, I discovered this the other day while trying to find a universal way to check for the distribution and version on any system. I'm not sure how long it's been around, but it works on every system I've tested it on so far. Does anybody have an OS that doesn't have this command?

Code:
$ lsb_release -idrc
Distributor ID: Debian
Description:    Debian GNU/Linux 4.0r4a (etch)
Release:        4.0r4a
Codename:       etch


Thanks!
-Jeo


Top
 Profile YIM  
 PostPosted: Tue Sep 02, 2008 11:04 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
Arch Linux is not (and cannot ever be) LSB compliant, so I doubt that it has this tool. I haven't checked though.


Top
 Profile  
 PostPosted: Mon Sep 29, 2008 10:14 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
You're right. It's not even always there in Debian. Oh well!


Top
 Profile YIM  
 PostPosted: Tue Nov 30, 2010 9:51 am   
User avatar

Joined: Wed Nov 17, 2010 1:02 pm
Posts: 9
Location: North Bethesda, MD
How would you detect SuSe Linux?


Top
 Profile WWW  
 PostPosted: Wed Dec 01, 2010 11:22 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
SuperFly wrote:
How would you detect SuSe Linux?

Either by lsb_release -idrc, as mentioned before, or by /etc/SuSE-release (works for openSuSE 11.3 at least).


Top
 Profile  
 PostPosted: Mon Dec 20, 2010 11:03 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Ok, this is an old thread, but here's a script that I wrote as an example for someone the other day (and I don't think it's a necro post if it's the second most recently updated thread =P )

Code:
#!/bin/bash

# osdetect.sh
# One giant 'if' statement to print a short
# one-word description of the operating
# system where the script is run.

# Check for FreeBSD in the uname output
# If it's not FreeBSD, then we move on!
if [ "$(uname -s)" == 'FreeBSD' ]; then
  OS='freebsd'

# Check for a redhat-release file and see if we can
# tell which Red Hat variant it is
elif [ -f "/etc/redhat-release" ]; then
  RHV=$(egrep -o 'Fedora|CentOS|Red.Hat' /etc/redhat-release)
  case $RHV in
    Fedora)  OS='fedora';;
    CentOS)  OS='centos';;
   Red.Hat)  OS='redhat';;
  esac

# Check for debian_version
elif [ -f "/etc/debian_version" ]; then
  OS='debian'

# Check for arch-release
elif [ -f "/etc/arch-release" ]; then
  OS='arch'

# Check for SuSE-release
elif [ -f "/etc/SuSE-release" ]; then
  OS='suse'

fi

# Tell the user what we found!
echo "$OS"



Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP