Damian
Damian

Reputation: 65

Bash command if command succeed

How can I change the output of a command accordding to the output of that command?

I do an snmp validation, but there are possible answers:

  1. The command was executed but there was no answer.
  2. The command was executed OK but the result was not the one I expecte (in this case, this would be the output: SNMPv2-MIB::sysUpTime.513 = No Such Instance currently exists at this OID)
  3. he command was executed OK and the result was the one Expected ( a different answer than No Such Instance currently exists at this OID)

I'd like that people running this script, in case option 1 or 2 are received read: " SNMP not available "

And for the 3rd option: " SNMP Available "

In my script, in option 2 my output goes to OK instead of going to Not OK

#!/bin/bash
hostname=$1
read -p "Introduce IP Address: " hostname
community="snmpcommunity"
echo "snmp validation:"
sysUpTime=`snmpget -v2c -c $comunidad $hostname 1.3.6.1.2.1.1.3.513`
if [ $? -eq 0 ]; then
    echo "snmp1 ok"
else
    echo "snmp1 not ok"
fi

Upvotes: 0

Views: 1111

Answers (2)

repzero
repzero

Reputation: 8402

!/bin/bash

hostname=$1
read -p "Introduce IP Address: " hostname
community="snmpcommunity"
echo "snmp validation:"
sysUpTime=`snmpget -v2c -c $comunidad $hostname 1.3.6.1.2.1.1.3.513`
if [ $? -ne 0 -o "$sysUpTime" = "SNMPv2-MIB::sysUpTime.513 = No Such Instance currently exists at this OID)" ]; then
            echo "SNMP not available"
else
      echo "SNMP available"      
fi

this part of code

   if [ $? -ne 0 -o "$sysUpTime" = "SNMPv2-MIB::sysUpTime.513 = No Such Instance currently exists at this OID)" ]

if there is an error or the sysUptime variable is equal to your string and it will print SNMP not available

Upvotes: 1

vijayalakshmi d
vijayalakshmi d

Reputation: 726

If the result of the snmpget command comes into sysUpTime variable, adding below if condition inside the first if condition should work.

if [[ $sysUpTime == *"No Such Instance currently exists at this OID"* ]]
then
  echo "snmp1 ok"
else
  echo "snmp1 not ok - Instance already exist."
fi

Upvotes: 0

Related Questions