Operating System - HP-UX
1833771 Members
2148 Online
110063 Solutions
New Discussion

ssh within a script not working for some reason

 
SOLVED
Go to solution
dev44
Regular Advisor

ssh within a script not working for some reason

Hi,

The point of the following script is to go out to all our servers and if the /var/mail/root file is current (today's date); then tail the last 100 lines to a file. Then send me the file. Now out of the whole list, there are about 8 servers that have a current mail log. However, my script is only outputting the first entry. If I just "echo $a" it will echo all servers with a current date....but when I ssh $a and tail the mail log it stops after the first one.

#! /usr/bin/ksh

m=`date +%b`
d=`date +%e`
HOME=/home/me/scripts
LIST=$HOME/serverlists/all.servers
OUT=/home/me/tmp/mail.out
OUT2=/home/me/tmp/maildat.out
MAIL=/var/mail/root
MAILLIST=me@example.com

if [ -f $OUT ]; then rm $OUT; fi
if [ -f $OUT2 ]; then rm $OUT2; fi

for x in `cat $LIST`; do
echo "$x \t `ssh $x ls -la $MAIL |awk '{print $6, $7}'`" >>$OUT2
done

cat $OUT2| while read a b c
do
if [ $b = $m ] && [ $c -eq $d ]; then
echo "_________________________________________________________________________\n" >>$OUT
echo "\n $a \n" >>$OUT
echo "_________________________________________________________________________\n" >>$OUT
ssh $a "tail -100 $MAIL" >>$OUT
fi
done
cat $OUT |mailx -s "MAIL Log" $MAILLIST
whatever
4 REPLIES 4
Bill Hassell
Honored Contributor
Solution

Re: ssh within a script not working for some reason

Use the -n option for all your scripted ssh commands (ssh -n host "some command"). This option redirects stdin for ssh which is only used for interactive sessions.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: ssh within a script not working for some reason

Hi:

In addition to adding the '-n' to your 'ssh' as Bill noted, change:

for x in `cat $LIST`; do
echo "$x \t `ssh $x ls -la $MAIL |awk '{print $6, $7}'`" >>$OUT2
done

...to:

while read x
do
echo "$x \t $(ssh -n $x ls -la $MAIL|awk '{print $6, $7}')"
done < ${LIST} > ${OUT2}

This eliminates the extra 'cat' process letting the shell do the read of the input file. The code also uses the Posix '$()' notation instead of the backticks to run a command. This is much more readable.

Regards!

...JRF...



Steven E. Protter
Exalted Contributor

Re: ssh within a script not working for some reason

Shalom,

debug:

ssh -vvv

redirect the output to a file so you can see the error message.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
dev44
Regular Advisor

Re: ssh within a script not working for some reason

That did it Bill....thanks!!!
whatever