0

I am trying to get the uptime of servers using SNMP. First I check if the server is online, then I try to retrieve the uptime. The issue is that I want to run the command multiple times to decrease latency in script, but I am not getting the expected output into the array.

for ip in "${Iparray[@]}"
do
    uparray=($(snmpwalk -Os -c public -v 1 $ip DISMAN-EVENT-MIB::sysUpTimeInstance | grep "^sysUpTimeInstance" | awk '{print $5}')) &
done
tail /proc/uptime | grep -o '^\S*'
wait $(jobs -p)
for ti in "${uparray[@]}"
do
    echo $ti
done

The output that I get when running the command outside of the script is:

5:58:27.35

But inside the script I get:

Timeout: No Response from 192.168.0.2
Timeout: No Response from 192.168.0.3    
Timeout: No Response from 192.168.0.5
Timeout: No Response from 192.168.0.4
Timeout: No Response from 192.168.0.1
Timeout: No Response from 192.168.0.6
Timeout: No Response from 192.168.0.12
Timeout: No Response from 192.168.0.14
Timeout: No Response from 192.168.0.13
Timeout: No Response from 192.168.0.10
Timeout: No Response from 192.168.0.21
Timeout: No Response from 192.168.0.24
Timeout: No Response from 192.168.0.22
Timeout: No Response from 192.168.0.101
Timeout: No Response from 192.168.0.23
Timeout: No Response from 192.168.0.254
Timeout: No Response from 192.168.0.107

Any help would be great. Probably a small issue in the script I have over looked.

8
  • There's nothing parallel about this code at all. Did you leave something out? Commented Nov 13, 2016 at 16:10
  • ...and the quoting is rather badly broken. Run it through shellcheck.net and fix what it finds. Commented Nov 13, 2016 at 16:10
  • ...if your intent is to have separate script instances contribute to a single variables's contents, then it's just "nope, doesn't work that way": each instance is its own program with its own memory, and can only change its own variables. I'd suggest using the filesystem as a combined store -- write all your results into individual files in a temporary directory, collect them all when done. Commented Nov 13, 2016 at 16:11
  • Sorry yes I forgot to add the & to the end of the code, just updated it then Commented Nov 13, 2016 at 16:13
  • (You could also use GNU parallel to generate a single, consistent output stream). Commented Nov 13, 2016 at 16:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.