0
#!/bin/sh
#
# found if this IP is receiving data
#
#echo hexIpAdd=`sh convertIPAddressToHex $1`

hexIpAdd='C0 A8 01 02'

#`snmpwalk -v 2c -c public 192.168.252.187 .1.3.6.1.4.1.231.7.1.2.2.1.4.7.13.1.1.3301 | grep '$hexIpAdd' | wc -l`

if [ `snmpwalk -v 2c -c public 192.168.252.187 .1.3.6.1.4.1.231.7.1.2.2.1.4.7.13.1.1.3301 | grep '$hexIpAdd' | wc -l` -gt 0 ]; then
       echo "Value FOUND!!"
else
       echo "Value NOT FOUND!!"
fi

The code is very simple, I convert my an regular IP to a hexadecimal one, and query via SNMPWALK a network equipment to see if the converted IP is receiving streaming.

The problem is, that the grep pipe is failing to use the VARIABLE, with means if I replace the "grep '$hexIpAdd'" with this "grep 'C0 A8 01 02'" it works.

2
  • The duplicate is your main issue, although I would also suggest changing your line to if snmpwalk -v 2c -c public 192.168.252.187 .1.3.6.1.4.1.231.7.1.2.2.1.4.7.13.1.1.3301 | grep -q "$hexIpAdd"; then Commented Dec 18, 2015 at 10:34
  • Thank You... it's working now Commented Dec 18, 2015 at 11:58

1 Answer 1

0
  1. Don't use single quote for quoting variable in shell
  2. Use grep -q to avoid command substitution, piping with wc and [...] expression:

Have it this way:

hexIpAdd='C0 A8 01 02'

if snmpwalk -v 2c -c public 192.168.252.187 .1.3.6.1.4.1.231.7.1.2.2.1.4.7.13.1.1.3301 | 
    grep -q "$hexIpAdd"; then
       echo "Value FOUND!!"
else
       echo "Value NOT FOUND!!"
fi
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You... it's working now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.