Tuesday, November 19, 2013

Shell script to determine OS

I look after hundreds of UNIX and Linux machines, and often need to roll a change out to them, but the change might be slightly different based on the server's operating system. I often use the following script to determine the OS, and execute the appropriate commands on each server (populate serverlist with your list of servers to check):

#!/bin/bash

for x in $(cat serverlist)
do
        echo Checking ${x}:

        # Determine OS

        os=$(ssh root@${x} "uname")

        case $os in

        SunOS)  echo "SunOS detected, doing something.."
                ssh -oStrictHostKeyChecking=no root@${x} "something Solaris specific"
                ;;

        Linux)  echo "Linux detected, doing something"
                ssh -oStrictHostKeyChecking=no root@${x} "something Linux specific"
                ;;

        *)      echo "Something unheard of"
                ;;

        esac

done

No comments:

Post a Comment