如何根据该命令的输出更改命令的输出?
我进行snmp验证,但有可能的答案:
我希望运行此脚本的人员,如果收到选项1或2,请阅读: " SNMP不可用"
对于第三种选择: " SNMP可用"
在我的脚本中,在选项2中,我的输出变为OK,而不是转到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
答案 0 :(得分: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
这部分代码
if [ $? -ne 0 -o "$sysUpTime" = "SNMPv2-MIB::sysUpTime.513 = No Such Instance currently exists at this OID)" ]
如果有错误或sysUptime变量等于你的字符串,它将打印SNMP不可用
答案 1 :(得分:0)
如果snmpget命令的结果进入sysUpTime变量,如果第一个if条件中的条件应该有效,则在下面添加。
if [[ $sysUpTime == *"No Such Instance currently exists at this OID"* ]]
then
echo "snmp1 ok"
else
echo "snmp1 not ok - Instance already exist."
fi