Want to terminate command execution when string found in the command output

Hi Experts,

I am very much new to linux scripting, I am currently working on reducing my manual work and hence writing a script to automate few task.

I am running below command to snmpwalk the router..

snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv 182.19.96.13 |grep ae0.784

get below output..

Did not find 'MplsVpnName' in module MPLS-VPN-MIB (/usr/local/share/snmp/mibs/mib-jnx-ldp.txt)
IF-MIB::ifDescr.2500 = STRING: ae0.784
Timeout: No Response from 182.19.96.13

this command finds the string and show it in output..but when I run below command..

snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv 182.19.96.13 |grep ae0.784 |awk '{print $2}'

get below output..

Did not find 'MplsVpnName' in module MPLS-VPN-MIB (/usr/local/share/snmp/mibs/mib-jnx-ldp.txt)
Timeout: No Response from 182.19.96.13
=

command is verymuch fine..but i am not getting why it shows timeout when i try to print 2 column...

also one more requirement is program should terminate once match is found and also print given column in some output file..

looking for help from the experts...

thanks in advance!!!

Regards,
Hanuamnt

Did not find 'MplsVpnName' in module MPLS-VPN-MIB (/usr/local/share/snmp/mibs/mib-jnx-ldp.txt)
IF-MIB::ifDescr.2500 = STRING: ae0.784
Timeout: No Response from 182.19.96.13

Not all those lines which you get in the output are seen by the grep command. The 1st and 3rd lines have been written by your command snmpwalk to standard error (by default the terminal). The 2nd line has been filtered by the grep command.

Is this what you need?

snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv 182.19.96.13 2>/dev/null|awk '/ae0\.784/{print $NF}'

If you want a command to terminate after some output has been detected ...

If the command will continue to generate output after the match is found, and if the wait time (could be a fraction of a second, could be weeks) until the command next attempts to write data to a pipe is acceptable, then a simple filter (awk, sed, etc...), which exits immediately after finding the match, could be sufficient. When the command next attempts to write to the pipe, since the filter that was on the other end is now gone, it will be sent a SIGPIPE (default response to this signal is program termination).

If the command to be killed does not write to the pipe regularly, then you need to send it a signal yourself when the match is detected. For this, you need to know the PID of that command.

Regards,
Alister

Hi team,

thanks elixir_sinari

the below command given by elixir works for me but in my output I want
some other column to be displayed(it is displaying the same output as i m searching for) and also it should write it to some other file, for e.g. output will be like..
IF-MIB::ifDescr.2500 = STRING: ae0.784

then I want highlighted part to be displayed in output..

snmpwalk -v 3 -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv 1.1.1.1 2>/dev/null|awk '/ae0\.784/{print $NF}'

Kindly help on this.

thanks..

Not every awk out there allows for this, but you can try to set the awk field separator to "[: ]" and then print $3 .

Hi RudiC,

thanks for the reply...could you please write same command and put awk whereever require..I am not getting where to use awk as suggested by you...

thanks in advance!!!

Regards,
Hanumant

As elixir_sinari and alister pointed out, there are some improvements possible to your command line to get where you want.

  1. Two of the three lines you show in post #1 are error msgs output to stderr. No piped filter will see those unless you redirect stderr.
  2. The grep cmd is pointless. awk can do this.
  3. If you want do stop after the first matched output, you could close the pipe.

Given your post #1 sample, my comments to elixir_sinari's suggestion combined with alister's might do the job:

snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv 182.19.96.13 2>/dev/null|awk -F"[: ]" '/ae0\.784/{print $3; exit}' > outputfile

It will suppress error msgs, print the ifDescr.2500 to the outputfile as desired, and then exit and close the pipe, (hopefully) causing the snmpwalk cmd to terminate. Pls check out and come back with results.

Hi All,

the command RudC is working fine alone..but I need this to check for multiple addresses and here my script to get ifdescr for multiple addresses, it is not giving me proper output(no output) can anyonce suggest what is wrong here..

#!/bin/bash
{
if [ -f ifdesc.out ];
then
`rm ifdesc.out`
fi
exec<ifdesc.csv
while IFS=',' read -r f1 f2
do
snmpwalk -v 3  -u snmp_com -a MD5 -A vfies -x DES -X VfIeis -l authPriv $f1 2>/dev/null|awk -F"[: ]" '/$f2/{print $3; exit}' > ifdesc.out
done
} > /dev/null 2>&1

where $f1=ipaddres and $f2 is the interface name.

Please help out with this..

Thanks,
Hanumant

pls. use code tags!
You can't have shell variables expanded within single quotes. The correct way to get f1 into the awk command is to add an awk variable assignment before or after the awk expression and use this inside the command:

awk -F"[: ]" '/F2awk/{print $3; exit}' F2awk=$f2

That's incorrect usage of an awk variable in a regexp match. That'll be treated as a match for the pattern "F2awk" and not that for the value contained in the variable by the same name. And, use double quotes to prevent the shell from messing with the data with whitespaces.

The correct usage is:

awk -F"[: ]" '$0 ~ F2awk{print $3; exit}' F2awk="$f2"

Hi,

thanks to both of you for reply..

here is the updated script...still not working..when i run it it keeps on running but don't stop..and also no output in output file,

#!/bin/bash
{
if [ -f ifdesc.out ];
then
`rm ifdesc.out`
fi
exec<ifdesc.csv
while IFS=',' read -r f1 f2
do
snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv $f1 2>/dev/null|awk -F"[: ]" '$0 ~ F2awk{print $3; exit}' F2awk="$f2" > ifdesc.out
done
}

we are near to close this ..plz help..

---------- Post updated at 07:15 AM ---------- Previous update was at 07:09 AM ----------

hey ...here I got the output in my output file..but i feed 5 IP and I got output of only one IP i.e. last IP in the list..plz help me...it should print new output for new ip address on new line..and this will be done...

thanks ...

Use >> .
> deletes old data and writes new data to the file..:slight_smile:

snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l  authPriv $f1 2>/dev/null|awk -F"[: ]" '$0 ~ F2awk{print $3; exit}'  F2awk="$f2" >> ifdesc.out

You're right. I did it on the fly and had no chance to test. Sorry for the premature post.

Hiii...yupeeii...I am getting the required output in my output file now....thaks to all those who helped me to acheive this..but still need to make it more finetune..

here my script...

#!/bin/bash
{
if [ -f ifdesc.out ];
then
`rm ifdesc.out`
fi
exec<ifdesc.csv
while IFS=',' read -r f1 f2
do
snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv $f1 2>/dev/null|awk -F"[: ]" '$0 ~ F2awk{print $3; exit}' F2awk="$f2" >> ifdesc.out
done
}

In this script..it may be possible that few devices may never give out any result i.e. snmpwalk output in this case it will directly print the next result in series..what I need is put the IP Address in one column and its respective result (interface ifindex) in next column so that it will be very easy to identify which IP have not responded to SNMP query.

Thanks again to ALL experts!!!

---------- Post updated 10-19-12 at 12:45 AM ---------- Previous update was 10-18-12 at 08:41 AM ----------

Hi All,

please reply to my bove query.

regards,
hanumant

try using this..

#!/bin/bash
{
if [ -f ifdesc.out ];
then
`rm ifdesc.out`
fi
exec<ifdesc.csv
while IFS=',' read -r f1 f2
do
result=$(snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv $f1 2>/dev/null|awk -F"[: ]" '$0 ~ F2awk{print $3; exit}' F2awk="$f2")
echo $f1 $f2 $result >> ifdesc.out
done
}

If you tell us where to get the IP address then this might be doable. pamu assumed it to be one of your input parameters. If that's true, adapt the awk command like

awk -F"[: ]" '$0 ~ F2awk{print F1awk, F2awk, $3; exit}' F2awk="$f2" F1awk="$f1"

It won't work as per the OP's comment.

Actually, I'm having a difficult time getting what the requestor really needs. If snmpwalk outputs the next device if actually requested device's data are not found, then both approaches will fail unless snmpwalk outputs sth. to compare to the requested device.
Pls post snmpwalk output for success and for failure.

Hi All,

thanks for the reply..actually when I use script given it gives output of only those devices for which snmpwalk works, it is not giving out anything else in the otput file(i.e. devices for which snmpwalk doesn't work..) in general when we do snmpwalk i success we should get either our output and in case of failure it is Response timeout.
..hope this clarifies requirement..

Have you tried my code..?