Search code examples
bashcentosargumentscommandline

bash - using a command line argument (hostname) to run an external command


First time post, please forgive any missing information.

I have a script that is supposed to work with icinga. I need icinga to log into my Linux box and run a command like "script ". The script will then run a command to that hostname like sudo /etc/init.d/apache2 status then report back "running or unused" and an exit status of 0 or 2. I'm wondering how I could add another command and have it one or the other run depending on what hostname it's given. Half of them need apache2 to be running and the other half need to have a process called dss to be running. I'd rather not have two separate scripts. Here is the working script and sorry it's sloppy but I haven't done any clean up and I'm not real good at bash yet.

so the user would run the script ./chkdss2 or

#!/bin/bash
ec=0
ec1=2
var3=run
var4=unused

for host in "$@"
do
  var1=`ssh $host sudo /etc/init.d/dss status|awk '{print $6}'`
  var2="$( echo $var1 | cut -c 3-5 )"
if [[ "$var2" == "$var3" ]]; then
    echo "$host is running"
    echo $ec
  else
    echo "$host is not running"
    echo $ec1
fi
done

Solution

  • There are a couple ways to test if a particular hostname is for apache or dss. You only need to have a list of hostnames for each case, and check if the received hostnames are included in said lists.

    Method 1: using arrays

    #!/bin/bash
    
    # Method 1, using array lists of hosts
    apachehosts=('ap1' 'ap2' 'ap3')
    dsshosts=('dss1' 'dss2' 'dss3')
    
    for host in "$@"
    do
        if printf '%s\n' "${apachehosts[@]}" | grep -Fxq "$host"
        then
            echo "$host: APACHE HOST"
        elif printf '%s\n' "${dsshosts[@]}" | grep -Fxq "$host"
        then
            echo "$host: DSS HOST"
        else
            echo "ERROR, $host: unknown host"
        fi
    done
    

    To modify the lists of hosts, simply add or remove values in the declaration of arrays apachehosts and dsshosts.


    Method 2: using case

    #!/bin/bash
    
    # Method 2, using case
    for host in "$@"
    do
        case "$host" in
            'ap1'|'ap2'|'ap3')
                echo "CASE, $host: APACHE HOST"
                ;;
            'dss1'|'dss2'|'dss3')
                echo "CASE, $host: DSS HOST"
                ;;
            *)
                echo "ERROR CASE, $host: unknown host"
                ;;
        esac
    done
    

    Here, you edit the patterns in each case.


    Method 3: using if

    #!/bin/bash
    
    # Method 3, using if
    for host in "$@"
    do
        if [[ "$host" == 'ap1' || "$host" == 'ap2' || "$host" == 'ap3' ]]
        then
            echo "IF, $host: APACHE HOST"
        elif [[ "$host" == 'dss1' || "$host" == 'dss2' || "$host" == 'dss3' ]]
        then
            echo "IF, $host: DSS HOST"
        else
            echo "IF, $host: unknown host"
        fi
    done
    

    Here you modify the if conditions. I prefer the other methods, since this one is more complicated to edit, it is not as clear, especially if your list of hosts is long.


    Method 4: condition on the hostnames

    If you are lucky, there is some pattern to your hostnames. Ex. all apache servers start with letters ap, all your dss servers include dss in the name, ...

    You can then simply use 2 if statements to decide which is which.

    #!/bin/bash
    
    # Method 4, patterns
    for host in "$@"
    do
        if [[ $(echo "$host" | grep -c -e "^ap") -ne 0 ]]
        then
            echo "PATTERNS, $host: APACHE HOST"
        elif [[ $(echo "$host" | grep -c -e "dss") -ne 0 ]]
        then
            echo "PATTERNS, $host: DSS host"
        else
            echo "PATTERNS, $host: unknown host"
        fi
    done
    

    Note: hostname apdss1 would come out as an Apache server here. Previous methods would respond "unknown host". You patterns must be strict enough to avoid mismatches.