From 5e772a607853f942819ec1c978f630f874c71ec6 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Thu, 26 Jan 2012 06:23:16 +0100 Subject: [PATCH] First push... --- README | 13 ++++++++ rmachines.sh | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 README create mode 100755 rmachines.sh diff --git a/README b/README new file mode 100644 index 0000000..dd23bbb --- /dev/null +++ b/README @@ -0,0 +1,13 @@ +rMachines + +This is the very first relase of the rMachines script. +It is a very simple Bash script to collect uptime and who data from remote +machines and then presenting that data on a very simple HTML page. +For this to work you need to setup SSH keys on the remote user on the machines +that will be used to collect the data. +The keys need to be password free becuase it's a non-interactive script. +Usually this is a bad idea considering it's securtiy implications. +So use it on your own risk!! + +Jack-Benny Persson +jake@cyberinfo.se diff --git a/rmachines.sh b/rmachines.sh new file mode 100755 index 0000000..92ba6f5 --- /dev/null +++ b/rmachines.sh @@ -0,0 +1,92 @@ +#!/bin/bash + +################################################################################ +# # +# Copyright (C) 2011 Jack-Benny Persson # +# # +# This program is free software; you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation; either version 2 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program; if not, write to the Free Software # +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # +# # +################################################################################ + +### rMachines version 0.1 ### + +### Add your machines to MACHINES. WEBPAGE is the output HTML file. +### The HTML file must to writable by the user running the script. +### If you add the -d option to the script, it will run over and over +### again and update the info every $SLEEP seconds. Add a & to run it +### in the background. For a cron job, don't add the -d option! +### RUSER is the remote user account. That user must have a valid SSH key. + +MACHINES=("localhost host2 host3 host4") +WEBPAGE="test.html" +SLEEP=30 +RUSER="jake" + + +##Sanity checks + +if [ ! -w "$WEBPAGE" ]; then + printf "You don't have write permission to ${WEBPAGE}\n"; + exit 192 +fi + +for i in ${MACHINES[*]}; do + ssh -o PasswordAuthentication=no -l ${RUSER} $i uptime > /dev/null + if [ "$?" -gt 0 ]; then + printf "There was a problem accessing $i \n" + exit 192 + fi +done + + +#Main routine (run checks and build HTML page) + +Main() +{ + printf "My rMachines\n\n\n" >\ + ${WEBPAGE} + printf "

\nMy rMachines\n

\n" >> ${WEBPAGE} + printf "" >> ${WEBPAGE} + + for i in ${MACHINES[*]}; do + printf "$i" >> ${WEBPAGE} + printf "\n
\n======================\n
\n" >> ${WEBPAGE} + ssh -l ${RUSER} $i uptime >> ${WEBPAGE} + printf "
\n" >> ${WEBPAGE} + ssh -l ${RUSER} $i who >> ${WEBPAGE} + printf "

\n" >> ${WEBPAGE} + done + + printf "\n\n\n" >> ${WEBPAGE} + printf "\n" >> ${WEBPAGE} +} + + +## Check if we want to run the script infinitive times (for background jobs) + +if [[ "$1" = "-d" ]]; then + while true + do + Main + sleep ${SLEEP} + done + +## Run just once (for cron jobs) +else + Main + +fi + +exit 0