1

I have the following command:

$ snmpnetstat -v2c -c public -Cs -Cp udp 10.10.0.32 

that has the output:

udp:
           198 total datagrams received
            65 datagrams to invalid port
             0 datagrams dropped due to errors
           265 output datagram requests

I want to write a bash script that returns the following:

Received Datagrams: 198 Invalid port:65 Dropped datagrams: 0 Datagram requests: 256

I started with:

#!/bin/bash

#!/bin/bash
rs=$(snmpnetstat -v2c -c public -Cs -Cp udp 10.10.0.32)
ReceivedDatagrams=$(echo $rs | cut -d"/" -f1)
InvalidPort=$(echo $rs | cut -d"/" -f2)
DroppedDatagrams=$(echo $rs | cut -d"/" -f3)
DatagramRequests=$(echo $rs | cut -d"/" -f4)
echo "Received Datagrams:$ReceivedDatagrams Invalid port:$InvalidPort Dropped datagrams:$DroppedDatagrams Datagram requests:$DatagramRequests"



echo "Received Datagrams:$ReceivedDatagrams Invalid port:$InvalidPort Dropped datagrams:$DroppedDatagrams Datagram requests:$DatagramRequests"

The output is:

    zsz@bme-ib112-05:~/bash_scripts$ ./script.sh         
Received Datagrams:udp: 242 total datagrams received 37 datagrams to invalid port 0 datagrams dropped due to errors 638 output datagram requests Invalid port:udp: 242 total datagrams received 37 datagrams to invalid port 0 datagrams dropped due to errors 638 output datagram requests Dropped datagrams:udp: 242 total datagrams received 37 datagrams to invalid port 0 datagrams dropped due to errors 638 output datagram requests Datagram requests:udp: 242 total datagrams received 37 datagrams to invalid port 0 datagrams dropped due to errors 638 output datagram requests

The output values are repeating over and over and not as I wanted.

Tim Luka
  • 147
  • 2
  • 8

3 Answers3

2

There are a couple of problems with your script. First, echo swallows all newlines, unless you specify the -e switch. So after this:

rs=$(snmpnetstat -v2c -c public -Cs -Cp udp 10.10.0.32)
echo $rs

the output will be:

udp: 198 total datagrams received 65 datagrams to invalid port 0 datagrams dropped due to errors 265 output datagram requests

Second, you specified / as delimiter for cut, but the output doesn't contain this character, so it is of no use using that for delimiter.

Third, you wanted to cut certain fields from the output, but this will not work, mainly because the first point. Had echo not swallowed newlines, it could work, but that you would need to cut only the first field, since for every line, that field will contain the number you need. But then you still need to convert the resulting lines to an array, and use that array to output what you need.

Like this:

output=$(snmpnetstat -v2c -c public -Cs -Cp udp 10.10.0.32)
thearray=($(echo -e "$output"|tail -n +2|awk '{print $1}'))
echo "Received Datagrams:${thearray[0]} Invalid port:${thearray[1]} Dropped datagrams: ${thearray[2]} Datagram requests: ${thearray[3]}"
Lacek
  • 7,233
  • 24
  • 28
1

bash doesn't allow spaces around the = for an assignment:

rs=$(snmpnetstat -v2c -c public -Cs -Cp udp 10.10.0.32)

In the end, bash is even more picky about its whitespace than python...

Sven
  • 98,649
  • 14
  • 180
  • 226
  • Thanks! it solved the problem. However, it prints not as I want: `Received Datagrams:udp: 155 total datagrams received 37 datagrams to invalid port 0 datagrams dropped due to errors 405 output datagram requests Invalid port:udp: 155 total datagrams received 37 datagrams to invalid port 0 datagrams dropped due to errors 405 output datagram requests Dropped datagrams:udp: 155 total datagrams received 37 datagrams to invalid port 0..` values are repeating over and over not as I wanted. – Tim Luka Jul 03 '19 at 12:18
1

Maybe something like this

#!/bin/bash
rs=$(snmpnetstat -v2c -c public -Cs -Cp udp 10.10.0.32)
# Create an array with one numberset per entry
num=($(grep -o "[0-9]*" file.txt))
# text output using the values within the array
echo "Received Datagrams:${num[0]} ReceivedDatagrams Invalid port:${num[1]} Dropped datagrams: ${num[2]} Datagram requests:${num[3]}"
pToker
  • 51
  • 4