1

I am developing a generic bash script which could be executed in different distro.

I have only a CentOS virtual machine and I would be interested to test my script on a virtual terminal (online or as a software, without writing access, just able read some standard files from the OS) but like I am running it in a different distro (possibility to switch between distro).

One example is to make a distro detection and I do not know what will be the expected info I will get from other distro.

For example on my VM I get:

$> test.sh
Your linux distribution is: CentOS
$>

I just do not know what will be the result in RedHat, Ubuntu, Debian, etc...

The code for the test.sh is here.

This idea comes from the front-end development where you have similar tools to test your web pages in different browsers without having all really installed. I know an OS is quite more complex than just a browser, but the question remains after few searches on Google.

рüффп
  • 1,677
  • 4
  • 27
  • 35
  • Side note: determining the distribution as part of a larger process is almost certainly not the appropriate way to determine which code path to follow. – Chris Down Jan 09 '14 at 10:40
  • @ChrisDown I am doing it for mainly REDHAT + CENTOS + FEDORA = rpm+yum support, and DEBIAN + UBUNTU = deb+apt-get support, the rest of the process will be generic. If you have a link to a better solution I am interested. – рüффп Jan 09 '14 at 10:43
  • 4
    That is the wrong way to solve the problem. Detect the tool you want to use, not the distribution. – Chris Down Jan 09 '14 at 10:43
  • That is just an example, even bad, OK, but I mean the script can be anything else similar. – рüффп Jan 09 '14 at 12:16
  • The links given [here](http://superuser.com/a/699479/151431) might be what you're looking for. – terdon Jan 10 '14 at 11:49

3 Answers3

2

As Chris Down said in the comments, detecting the distribution is really a bad way of detecting the package system. For example, you mentioned "REDHAT + CENTOS + FEDORA = rpm+yum support, and DEBIAN + UBUNTU = deb+apt-get". OK, what about Mint, LMDE, Kali, Backtrack, Crashbang and all the other distros using dpkg/apt-get? Or Scientific Linux, Yellow Dog Linux and Oracle Linux all of whom use yum? Not to mention other RPM-based distros that don't use yum like SuSe, OpenSuSe, Mandriva Linux, Rosa Linux or Mageia?

A much better way would be to detect the tool you want to use. I would also suggest you use the rpm system directly rather than yum, why limit yourself to the 6 RPM-based distros that use yum?

I would simply test if the system you find yourself on uses rpm or dpkg to manage their packages:

if [ $(rpm -qa 2>/dev/null | wc -l ) -gt 10 ]; then 
  system="rpm" 
elif [ $(dpkg -l 2>/dev/null | wc -l ) -gt 10 ]; then 
  system="dpkg" 
else 
  system="unk"
fi 
echo $system

Since rpm can be installed on dpkg based distros and vice versa, I am testing whether there are more than 10 packages installed with each tool.

For a better way to detect the actual distribution see the accepted answer here, that should give you an idea of how complicated it is. Do you really want to write distro-specific tests for dozens of distros when you could easily simply detect the packaging system used instead?

terdon
  • 234,489
  • 66
  • 447
  • 667
  • We do not want to support all kind of Distros just RHEL, Centos & Ubuntu (Fedora is still not in our scope), that is why we start with these tests. – рüффп Jan 09 '14 at 12:13
  • However, this is not answering my question. My question is "Is there any terminal emulator that simulates different distros?" – рüффп Jan 09 '14 at 12:15
  • 1
    @ruffp if that's all you want, see the script in the answer I link to in my last paragraph. I _strongly_ recommend you don't take this route though, what if the distro changes the way it sets up its `lsb` response? If you simply check for `rpm` vs `dpkg` you will know what to do. Anyway, you should also check the file `/etc/os-release` which should be present in all the distros you mention. – terdon Jan 09 '14 at 12:35
  • As for not answering your question, you're right but this type of question is a bad fit for our site. In fact, we've been discussing this type of request for online tools [here](http://meta.unix.stackexchange.com/q/2642/22222). On this site we like specific, technical problems with specific technical answers. Not things that you can find by googling. You might want to ask on our [chat] if anyone knows of something that could help you. – terdon Jan 09 '14 at 12:37
  • You are right by the way of testing the presence of rpm / dpkg, but your script is a bit slow, I think you can improve by using the which command to see if the program is installed. – рüффп Jan 09 '14 at 12:44
  • For your second comment, it is not necessary an online tool, it can be a package or something small, but I understand your point, but googling was not sufficient, that is why I asked here. – рüффп Jan 09 '14 at 12:48
  • @ruffp no. First of all, `which` is [not a good idea](http://unix.stackexchange.com/q/85249/22222), use `type` and anyway, the presence of the binary means nothing. I have both `rpm` and `dpkg` installed on my debian and that can often be the case if you've used something like `alien` to install packages from different systems. That's why I check that the command returns something. It took less than a second in the 3 systems (debian, ubuntu and suse) I tried it on. – terdon Jan 09 '14 at 12:53
2

Python

#!/usr/bin/env python
import platform

"""

Fingerprint the following operating system

        * Mac OS X
        * Ubuntu
        * Red Hat/CentOS
        * FreeBSD
        * SunOS

"""

class OpSysType(object):
        """ Determine OS type using platform module """
        def __getattr__(self,attr):
                if attr == "osx":
                        return "Mac OS X"
                elif attr == "rhel":
                        return "redhat"
                elif attr == "ubu":
                        return "ubuntu"
                elif attr == "fbsd":
                        return "FreeBSD"
                elif attr == "sun":
                        return "SunOS"
                elif attr == "unknown":
                        return "unknown"
                else:
                        raise AttributeError,attr

        def linuxtype(self):
                """ Use various method to determine Linux Type """
                if platform.dist()[0] == self.rhel:
                        return self.rhel
                elif platform.uname()[1] == self.ubu:
                        return self.ubu
                else:
                        return self.unknown

        def queryos(self):
                p = platform.system()
                if p  == "Darwin":
                        return self.osx
                elif p == "Linux":
                        return self.linuxtype()
                elif p == self.sun:
                        return self.sun
                elif p == self.fbsd:
                        return self.fbsd

def fingerprint():
        type = OpSysType()
        print type.queryos()

if __name__ == '__main__':
        fingerprint()
Rahul Patil
  • 24,281
  • 25
  • 80
  • 96
-1

You can easily do with just uname:

OS=`uname`
  if [ "$OS" == "Linux" ];then
    distro="Linux"
elif[ "$OS" == "SunOS" ];then
    distro="Solaris"
else
    echo "CRITICAL - Unknown os"
    exit 2
klerk
  • 2,779
  • 2
  • 16
  • 15
  • This is not detecting the distro, and anyway this is not my original question. My (bad) example was just to illustrate a case. – рüффп Jan 09 '14 at 12:17