Reputation: 65
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:
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
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
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