Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Why this script is not working as 'expected' when doing ssh with while read ... really confused? Post 303035056 by newbie_01 on Monday 13th of May 2019 06:50:08 AM
Old 05-13-2019
Why this script is not working as 'expected' when doing ssh with while read ... really confused?

Hi,

I have a script below that is running ssh <host> <command> on some servers.

Below is more or less the script. I have to modify it somehow to get rid of the 'confidential' hostnames

Code:
check_log.bash

#!/bin/bash
#

myPID=$$

parse_log ()
{
   sub="parse_log"
   host=${1}
   service=${2}

   echo
   echo "----------------------------------------------------------------"
   echo
   echo "... Parsing listener log on ${host}"

   cat /dev/null > ${sub}.${host}.tmp
   ssh ${host} find /u01/app/grid/diag/tnslsnr/${host} -name "listener*log" -print | tee -a ${sub}.${host}.tmp

   while read log
   do
      echo "     --> Searching ${log} for ${service} on ${host} ..."
      #ssh ${host} grep -i "${service}" ${log}
      #echo
   done < ${sub}.${host}.tmp

   echo
   echo "----------------------------------------------------------------"
   echo
}


########
# MAIN #
########

parse_log "host1" "test_app"
parse_log "host2" "test_app"
parse_log "host3" "test_app"

I am running the script on a mounted filesystem that is accessible to all hosts, i.e. for example /nfs/scripts. So the script is /nfs/scripts/check_log.bash and I just run it from host1 as ./check_log.bash.

So the first ssh below runs fine, it does the find and redirect the result to the .tmp file

Code:
   cat /dev/null > ${sub}.${host}.tmp
   ssh ${host} find /u01/app/grid/diag/tnslsnr/${host} -name "listener*log" -print | tee -a ${sub}.${host}.tmp

I then want to grep for a script like "test_app" from each file which is why I have the following while loop.

Code:
   while read log
   do
      echo "     --> Searching ${log} for ${service} on ${host} ..."
      #ssh ${host} grep -i "${service}" ${log}
      #echo
   done < ${sub}.${host}.tmp

So far so good, I tested and it does shows me the " Searching ..." string. However when I am now ready to run it the ssh and has uncommented the
Code:
#ssh ${host} grep -i "${service}" ${log}

. The while loop is not going thru all the file in the .tmp file, it is only doing it on the first file. It's as if after it run the first ssh it is breaking out of the while loop?

At first I thought maybe it has to do with using /bin/bash. Using /bin/ksh gives the same result.

Not sure what am doing wrong :-) Checking the .tmp file, I can see that each of them has about 6-7 files.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

error "test: [-d: unary operator expected" very confused.

Im trying to check if a series of directory exists and if not create them, and am having issues. All the instances of test return with the error "test: #!/bin/bash location_Parent=~/Documents/sight_of_sound location_IMG=~/Documents/Sight_of_sound/IMG location_AUD=~/Documents/Sight_of_sound/AUD... (4 Replies)
Discussion started by: orionrush
4 Replies

2. Shell Programming and Scripting

Read statement not working in a script

I have a script consisting of certain functions whose input is a file at same location. In that file i have written the name of anothe file at same location. The third file contains a word which act as a function in the first script.Let me give an example i have a scrip file say 1.sh in which i am... (7 Replies)
Discussion started by: sumitdua
7 Replies

3. Shell Programming and Scripting

ssh is not working while calling through expect shell script

Hi, Please share you experience and way out on below error:--> #!/bin/bash -xv FILE=login.txt + FILE=login.txt CONNECT=sshlogin.exp + CONNECT=sshlogin.exp SERVERNAME=$1 + SERVERNAME=192.168.12.1 MyServer="" + MyServer= MyUser="" + MyUser= MyPassword="" + MyPassword= exec 3<&0 +... (6 Replies)
Discussion started by: manish_1678
6 Replies

4. Shell Programming and Scripting

Read command not working as expected

I was trying to write a simple script which will read a text file and count the number of vowels in the file. My code is given below - #!/bin/bash file=$1 v=0 if then echo "$0 filename" exit 1 fi if then echo "$file not a file" exit 2 fi while read -n... (14 Replies)
Discussion started by: linux_learner
14 Replies

5. Shell Programming and Scripting

Script not working as expected

Hi, I have prepared a script and trying to execute it but not getting expected output. Could you please help and advise what is going wrong. "If else" part in below script is not working basically. I am running it on HP-UX. for i in slpd puma sfmdb do echo "******\t$i\t*******" echo... (10 Replies)
Discussion started by: sv0081493
10 Replies

6. Shell Programming and Scripting

awk not working as expected in script

Dear all, I had script which used to work, but recently it is not working as expected. I have command line in my shell script to choose the following format from the output_elog and perform some task afterwards on As you see, I want all numbers in foramt following RED mark except for... (12 Replies)
Discussion started by: emily
12 Replies

7. Shell Programming and Scripting

awk matching script not working as expected

This is my ubuntu version: $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.4 LTS Release: 16.04 Codename: xenial $ /bin/awk -V | head -n1 bash: /bin/awk: No such file or directory I have gotten a script that helps me to parse,... (14 Replies)
Discussion started by: delbroooks
14 Replies

8. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

9. UNIX for Beginners Questions & Answers

Passing Arguments to shell script from file is not working as expected.

Hi All, I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination. # file_copy.sh source_dir = ${source_dir} target = ${target_dir} cp source_dir target and my parameter file is like below #parameter_file.txt source_dir =... (4 Replies)
Discussion started by: Narasimhasss
4 Replies

10. UNIX for Beginners Questions & Answers

Is this sort working as it is supposed to be? A bit confused whether it is sorting correctly or not

Hi, Below is the sample file: $ cat x.txt MDSYS|OGIS_GEOMETRY_COLUMNS|TABLE MDSYS|OGIS_SPATIAL_REFERENCE_SYSTEMS|TABLE MDSYS|SDO_IDX_TAB_SEQUENCE|SEQUENCE MDSYS|SDO_PREFERRED_OPS_USER|TABLE MDSYS|SDO_ST_TOLERANCE|TABLE MDSYS|SDO_TOPO_DATA$|TABLE MDSYS|SDO_TOPO_RELATION_DATA|TABLE... (4 Replies)
Discussion started by: newbie_01
4 Replies
ssh-keysign(1M) 														   ssh-keysign(1M)

NAME
ssh-keysign - ssh helper program for host-based authentication SYNOPSIS
ssh-keysign ssh-keysign is used by ssh(1) to access the local host keys and generate the digital signature required during host-based authentication with SSH protocol version 2. This signature is of data that includes, among other items, the name of the client host and the name of the client user. ssh-keysign is disabled by default and can be enabled only in the global client configuration file /etc/ssh/ssh_config by setting Host- basedAuthentication to yes. ssh-keysign is not intended to be invoked by the user, but from ssh. See ssh(1) and sshd(1M) for more information about host-based authen- tication. /etc/ssh/ssh_config Controls whether ssh-keysign is enabled. /etc/ssh/ssh_host_dsa_key /etc/ssh/ssh_host_rsa_key These files contain the private parts of the host keys used to generate the digital signature. They should be owned by root, readable only by root, and not accessible to others. Because they are readable only by root, ssh-keysign must be set-uid root if host-based authentication is used. ssh-keysign will not sign host-based authentication data under the following conditions: o If the HostbasedAuthentication client configuration parameter is not set to yes in /etc/ssh/ssh_config. This setting cannot be overri- den in users' ~/.ssh/ssh_config files. o If the client hostname and username in /etc/ssh/ssh_config do not match the canonical hostname of the client where ssh-keysign is invoked and the name of the user invoking ssh-keysign. In spite of ssh-keysign's restrictions on the contents of the host-based authentication data, there remains the ability of users to use it as an avenue for obtaining the client's private host keys. For this reason host-based authentication is turned off by default. See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWsshu | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ ssh(1), sshd(1M), ssh_config(4), attributes(5) AUTHORS
Markus Friedl, markus@openbsd.org HISTORY
ssh-keysign first appeared in Ox 3.2. 9 Jun 2004 ssh-keysign(1M)
All times are GMT -4. The time now is 12:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy