0

I have a script that checks IPs for ping reply then tries to run an snmpwalk on them. I am trying to schedule the script to run weekly in cron. When I run the script in the shell, it runs perfectly. When I execute the cron in webmin, it skips the second do/done set.

The script does a ping of IPs .21 through .30 and writes them to the 'hostip' file. It then runs each line of that file through the snmpwalk command.

#!/bin/bash
#USAGE: âscriptname 10.1.1â³
set +x
for i in {21..30};
do fping -t300 -a $1.$i >> hostip
done
set -x
cat hostip | while read line;
do
echo snmpwalk -v 2c -c public "$line" iso.3.6.1.2.1.17.4.3.1;
snmpwalk -v 2c -c public "$line" iso.3.6.1.2.1.17.4.3.1;
done
wait
rm -Rf hostip

When I run in the interactive shell (SSH) the hostfile is created and the snmpwalks are performed. However, when I click Run Now on the root cron in Webmin, I see:

+ set +x
+ cat hostip
+ read line
+ wait
+ rm -Rf hostip

It does not return the snmpwalk commands. How do I get the script to run in cron the same as it runs in inveractive shell? I have tried researching online but cannot find cases where echo, or the while read line causes problems. The script is executable

4
  • Where exactly do you think the cwd (working directory) is? Are you sure your cron script has write access in that directory? Shouldn't you really be explicitly setting a working directory or giving a full path to the hostip file? Also wouldn't it be wise to start off you script with something like echo -n '' >hostip just to make sure the file is empty when you start?
    – Zoredache
    Sep 27, 2014 at 0:26
  • cwd is /opt/axxcheck, I defined this in the command of the cron (cd /opt/axxcheck && sh -x script.sh 192.168.0 >> output.log). The script is running as root, so I assumed that it would have write access to /opt/axxcheck. It would be wise to check that hostip is blank, thanks for that tip.
    – kdawg4
    Sep 27, 2014 at 1:06
  • Move set -x above the for loop. I'd suspect the file is empty. Sep 27, 2014 at 1:10
  • I removed the rm -Rf hostip. When run in interactive shell, the file is created properly hence the snwmpwalk working. When cron runs the script, it creates the file but does not add any lines to the file.
    – kdawg4
    Sep 27, 2014 at 1:36

1 Answer 1

0

You might try this instead.

fping -t300 -a $1.{21..30} | while read online_ip; do
  echo snmpwalk -v 2c -c public $online_ip iso.3.6.1.2.1.17.4.3.1;
  snmpwalk -v 2c -c public $online_ip iso.3.6.1.2.1.17.4.3.1;
done

This will do the same thing, but instead of piping the output of fping to a file, you can simply pipe that output into a while loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .