2 Replies Latest reply on Jul 10, 2013 3:17 AM by srini2411

    Custom Scripts to Email Command Outputs

      I am looking for a custom script that could be scheduled as a cron job to do the following:


      1. Run a command such as: "df -h" or "cf cluster status" or "uptime" or "vmstat" or "mailq"... etc.

      2. Write the output to a file

      3. Email the output either as an attachment or in the body of a message


      I'm not a skookum scripter so if anyone has some helpful hints or solutions, please respond to this discussion.


      Thanks for any help!

        • 1. Re: Custom Scripts to Email Command Outputs

          The firewall runs on FreeBSD; a simple script like this from a forum on the internet should work on the firewall.  I guess I'm saying: search the 'net for shell scripting examples to get some more help also.  By default, when users are created on the firewall, their SHELL variable is set to tcsh, which is an enhanced version of csh (so, any tcsh or csh shellscripts you find on the web should work [there are some caveats to that statement, but in general they should work]).


          Crontab is stored in the 'cf database'; you can run cf cron q to output all the cron jobs.  Running cf cron q will help you see how the current cron jobs look so you can figure out how to make your own.  Do a man cf_crontab to learn how the command works.  If you're not familiar with the cf command you can read this blog post I wrote about it.


          The firewall includes the mail command.  You can use that command to send email off the box.  Do a man mail or search the internet to learn how it works.


          This simple script below writes out system information to a file.  It writes out some static information first and then does a loop, writing the rest of the commands out to a file every 180 seconds (it appends the information to the file actually, using the >> shell command):




            touch system.info.txt

            /sbin/dmesg                   >>system.info.txt

            /bin/df                       >>system.info.txt

            /usr/bin/vmstat -sm           >>system.info.txt

            while [ 1 ]


                echo "=================================" >> system.info.txt

                /usr/bin/top -u -s 20 -b -d 3 -S  20     >> system.info.txt

                        /usr/bin/netstat -i                      >> system.info.txt

                /usr/bin/netstat -f inet                 >> system.info.txt

                /usr/bin/netstat -m                      >> system.info.txt

                /usr/bin/netstat -s                      >> system.info.txt

                sleep 180




          You can save this text as a file on the firewall (like systeminfo.sh) then chmod it to make it executable (chmod 744 systeminfo.sh) and then run it (sh ./systeminfo.sh).  You stop it with CTRL+C.  The file it outputs is called system.info.txt and it's in the directory you run the command from.


          I use the edit command to edit files via the command-line of the firewall.  The File Editor in the GUI is also useful for...editing files.

          Good luck.

          • 2. Re: Custom Scripts to Email Command Outputs

            The script was really useful, thanks guys. I did some changes as per my need and it works beautifully.


            Is there a way to run this on multiple firewalls from another machine ?