0

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

2 Answers 2

1
!/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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.