Sponsored Content
Top Forums Shell Programming and Scripting Need some help in bash scripting with ssh Post 302640115 by muogli on Monday 14th of May 2012 06:59:41 AM
Old 05-14-2012
Need some help in bash scripting with ssh

Hi @ all

I have the following scenario:

As Admin of a cupple of servers I tried to write the following script to figure out, if the machine is up and available and if some directory´s were available. But my script is having some probs, while running. Maybe some of you have a better way to solve the same issue:

Code:
#!/bin/bash
while read server
do
if [ ! -z $server ]
then
        ping -c 1 $server
        if [ $? -eq 0 ] ; then
                echo -e "Das System "$server" ist per Ping erreichbar und wird jetzt abgefragt!"
                echo -e "Das System "$server" ist erreichbar und steht zur Verfuegung" >> erreichbare-server-log.txt
                WLS=$(ssh -f -x -n $server 'ls -d /opt/wls*[0-9] 2>/dev/null|wc -l')
                OHS=$(ssh -f -x -n $server 'ls /opt/*/*/Apache/Apache/logs/httpd.pid 2>/dev/null|wc -l')
                WAS=$(ssh -f -x -n $server 'ls -d /opt/IBM/WebS*[A-Za-z0-9] 2>/dev/null|wc -l')
                IHS=$(ssh -f -x -n $server 'ps -ef | grep -i HTTPServer | grep root | grep start |grep -v grep 2>/dev/null|wc -l')
                Apache=$(ssh -f -x -n $server 'ls -d /usr/local/apache/bin/apache*[A-Za-z0-9] 2>/dev/null|wc -l')
                JBoss=$(ssh -f -x -n $server 'ps -ef |grep -i org.jboss.Main 2>/dev/null|grep -v grep|wc -l')
                TomCat=$(ssh -f -x -n $server 'ps -ef |grep -i catalina.home 2>/dev/null|grep -v grep|wc -l')
                MySQL=$(ssh -f -x -n $server 'ps -ef |grep -i mysql 2>/dev/null|grep -v grep|wc -l')
                echo -e "$server; \t\t $WLS; \t $OHS; \t $WAS; \t $IHS; \t\t $Apache; \t \t $JBoss; \t \t $TomCat; \t \t $MySQL" >> servers2.csv
        else
                echo "Maschine ist nicht erreichbar"
                echo -e "Das System "$server" ist aktuell nicht erreichbar!" >> nicht-erreichbare-server-log.txt
                set -e

        fi
else
        echo "IP Address is empty"
fi
done < serverip.txt
cat servers2.csv | awk -F';''BEGIN(WLS=0;OHS=0; WAS=0; IHS=0; Apache=0; JBoss=0; TomCat=0; MySQL=0}{WLS+=$2;OHS+=$3;WAS+=$4;IHS+=$5;Apache+=$6;JBoss+=$7;TomCat+=$8;MySQL+=$9}END{printf("Gesamtanzahl; \t \t %d; \t %d; \t %d; \t %d; \t\t %d; \t\t %d; \t \t %d; \t \t %d\n", WLS, OHS, WAS, IHS, Apache, JBoss, TomCat, MySQL)}' >> servers2.csv

echo -e "Dieses Script wurde ausgefuehrt: " DATE=`/bin/date +%d-%m-%y_time_%H-%M-%S` >> servers2.csv
Time() >> servers2.csv

echo -e "FERTIG"

The list of servers were stored in a seperated file where from I get the IP´s. Any suggestions are welcomed

Last edited by methyl; 05-14-2012 at 08:04 AM.. Reason: Change SCRIPT tags to CODE tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

scripting an ssh session?

I know the root login/password for a machines, and I want to automate some commands like this from each: ssh root@remoteHost1 "tail /var/log/messages" ssh root@remoteHost2 "tail /var/log/messages" ssh root@remoteHost3 "tail /var/log/messages" ssh root@remoteHost4 "tail /var/log/messages" ssh... (2 Replies)
Discussion started by: jjinno
2 Replies

2. Shell Programming and Scripting

need help scripting tar over ssh

Hello - I've used 'expect' in FTP scripts before without any problems, but am unsure as to how I would script a tar over ssh session? I need to send password for authentication to the remote Suse machine. :confused: Any assistance provided would be GREATLY appreciated. (6 Replies)
Discussion started by: rm -r *
6 Replies

3. Shell Programming and Scripting

ssh scripting

Hi, I'm trying to write a script that will ssh into a number of boxes and run 'top' and 'ps', then output the results to a file with the hostname. The script only seems to run top or ps on the local machine though. Any help would be apperciated #ssh into box while read box do ssh -n $box ... (3 Replies)
Discussion started by: Brimak86
3 Replies

4. Shell Programming and Scripting

Bash commands to an 'ssh' within an ssh'

I've struggled to find a solution to this problem from searching so I thought I'd write a post to see what can be done. I'm attempting to connect and run commands on 'server2' but because of security limitations I cannot access it directly. I can however ssh into 'server1' and then into... (7 Replies)
Discussion started by: mcintosh.jamie
7 Replies

5. Shell Programming and Scripting

SSH and AWK Scripting

I am trying to put an awk command in ssh, for example: ERRCOUNT=`ssh -n $HOST "ps -ef | grep .job | grep -v grep | grep -v alert_jobs_still_running |wc -l"` From korn shell prompt this works: awk '/^Jul 12 16/ {print $0}' /u01/app/oracle/jobs/adhoc/test.dat | wc -l My data file:... (3 Replies)
Discussion started by: bradyd
3 Replies

6. Linux

ssh and passwd scripting execution problems on linux

I'm having a problem here and I was wondering if anyone could help me? I'm putting together a password script. First off, I don't have root access. I have sudo access. Lets say the User ID is Trevor1, the password is H!rry23! and the server name is Linux1234 This is how the script begins ... (5 Replies)
Discussion started by: wdog17
5 Replies

7. Shell Programming and Scripting

Scripting ssh question

I am new here so I apologize if this question is in the wrong section or outside of the realm of this board. Also, this is just my first week into shell programming so I am probably doing lots of things wrong. I am trying to write a script to ssh to a machine as one user and then run a command... (0 Replies)
Discussion started by: Parva
0 Replies

8. Shell Programming and Scripting

SSH to remote hosts in shell scripting

Hi There, I have a file contaning some 100 servers names one by one the file called redhat_servers.txt I want to prepare a script where it should give me the host name and kernal version. I wrote like this, #!/bin/bash while read line do ssh $line "uname -nr" done <... (3 Replies)
Discussion started by: kumar85shiv
3 Replies

9. Shell Programming and Scripting

Help needed on ssh in bash scripting

HI I have the following requirement I have a script a.sh which will deploy files in multiple servers .The argument for the a.sh is abc.gz host1.conf where abc.gz is a zip file and one.conf will contain all the database connection string . Now I have to write a b.sh which will... (7 Replies)
Discussion started by: harry00514
7 Replies

10. Shell Programming and Scripting

ksh scripting SSH to Compare File Sizes

Hello, I currently have very little experience with Shell scripting and trying to create a script for the purpose of collecting the size of a couple sizes on 4 different Hosts. The Idea is to collected the information from the files in which the script is kicked off on, store the values into... (17 Replies)
Discussion started by: Abstract3000
17 Replies
ZGREP(1)                                                      General Commands Manual                                                     ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 02:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy