Re: script regarding ssh (842 Views)
Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 26 of 56 (902 Views)

Re: script regarding ssh

>I tried adding another "then" as below:

 

You don't need another one:  (Nor should you need those "sh" commands.)

if [ $? -eq 0 ]; then
   /home/rtns/x/a.sh

   ssh root@serverB /home/rtns/testdir1/testscript
else
:  echo "Some files not matched:"
:  comm -3 serverA_files serverB_files
fi

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 27 of 56 (898 Views)

Re: script regarding ssh

>>ssh root@serverB /home/rtns/testdir1/testscript

The above script is not running on serverB although it is not giving any error.

 

Regards,

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 28 of 56 (896 Views)

Re: script regarding ssh

>The above script is not running on serverB although it is not giving any error.

 

Add the following (after the #! line):  set -x

 

This will trace the commands in the script.

You could also do it in the original script on serverA to make sure you are getting to that ssh.

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 29 of 56 (884 Views)

Re: script regarding ssh

Hi Dennis,

 

The script i am running on serverB through ssh is as below. I used set -x option also in the script.

 

#!/usr/bin/sh
set -x
for i in abc*.xyz
do
if  (test -f  "$i")
then
newname=${i#abc}
/usr/bin/mv $i /home/rtns/testdir2/$newname
fi
done

 

But it is giving the output as:

+ test -f abc*.xyz

 

I run the above script on serverB itself and it is running successfully. But using ssh to run this script giving the above message.

 

Regards,

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 30 of 56 (880 Views)

Re: script regarding ssh

>The script I am running on serverB through ssh is as below.
>for i in abc*.xyz

 

Are you running this script in the right directory?  Otherwise with ssh, it would default to the home directory.


>if  (test -f  "$i")

 

I wouldn't use test(1).  I would just use:

   if [ -f "$i" ]; then


>But it is giving the output as: + test -f abc*.xyz
>I run the above script on serverB itself and it is running successfully. But using ssh to run this script giving the above message.

 

As I guessed above, it depends on what directory you start in.

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 31 of 56 (904 Views)

Re: script regarding ssh

>> Are you running this script in the right directory?  Otherwise with ssh, it would default to the home directory.

 

I am running the script as:

ssh root@serverB /home/rtns/testdir1/testscript

The script is placed in as above path, but still it is not running.

 

>>I wouldn't use test(1).  I would just use:

   if [ -f "$i" ]; then

 

I tried as above also. But still it is giving the same message as :

+ [ -f abc*.xyz ]

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 32 of 56 (900 Views)

Re: script regarding ssh

>> Are you running this script in the right directory?  Otherwise with ssh, it would default to the home directory.

 

>I am running the script as:

>ssh root@serverB /home/rtns/testdir1/testscript

 

So the answer to my question is: No

 

You need to invoke it as:

ssh root@serverB "cd XXXX; /home/rtns/testdir1/testscript"

Where XXXX is the directory you want to check.

 

Or you could pass this directory on the command line of your script and with some modifications you can look at the script parms.

 

>But still it is giving the same message as: + [ -f abc*.xyz ]

 

Of course, my suggestion just makes it faster and cleaner, doesn't fix anything.  ;-)

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 33 of 56 (886 Views)

Re: script regarding ssh

HI Dennis,

 

>>else
:  echo "Some files not matched:"
:  comm -23 testhoueccp1_files testaustsapdr_files
fi

 

For the above statement if the files are not matced then the files with the above subject line should be mailed to 4 email addresses. How can i put that in the above statement the 4 addresses.

 

Regards,

 

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 34 of 56 (882 Views)

Re: script regarding ssh

[ Edited ]

>How can I put that in the above statement the 4 addresses?


  comm -23 testhoueccp1_files testaustsapdr_files | mailx -s "Some files not matched" alias1 ...

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 35 of 56 (875 Views)

Re: script regarding ssh

>>comm -23 testhoueccp1_files testaustsapdr_files | mailx -s "Some files not matched" alias1 ...

 

I tried with the above command. But i am not receiving the mail. It is showing the below message when running the script :

 

Null message body; hope that's ok

 

Regards,

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 36 of 56 (871 Views)

Re: script regarding ssh

>>comm -23 testhoueccp1_files testaustsapdr_files | mailx -s "Some files not matched" alias1 ...
>I tried with the above command. But I am not receiving the mail: Null message body;

 

This indicates that you have extra files on testaustsapdr_files but not on testhoueccp1_files.

If you want to see both lists use:  comm -3 ...

 

If you don't care, then you'll need to check to see of the output is empty:

comm -23 testhoueccp1_files testaustsapdr_files > testhoueccp1_only

if [ -s testhoueccp1_only ]; then

   mailx -s "Some files not matched" alias1 ... < testhoueccp1_only

fi

rm -f testhoueccp1_only

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 37 of 56 (869 Views)

Re: script regarding ssh

>>comm -3 testhoueccp1_files testaustsapdr_files | /usr/bin/mailx -s "Some files not matched:" rahul_rtns@domain.com

 

I am using above to send the mail. When running the script, though it is not giving any message or error, but i am not getting any mail in my mailbox. In my previous post also, i have mentioned this. :)

 

Regards,

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 38 of 56 (863 Views)

Re: script regarding ssh

I assumed your script looked like:

cmp -s testhoueccp1_files testaustsapdr_files

if [ $? -eq 0 ]; then
   /home/rtns/x/a.sh

   ssh root@serverB /home/rtns/testdir1/testscript
else

   comm -3 testhoueccp1_files testaustsapdr_files | /usr/bin/mailx -s "Some files not matched:" rahul_rtns@domain.com

fi

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 39 of 56 (862 Views)

Re: script regarding ssh

Hi Dennis,

 

My script is like this :

 

#!/usr/bin/sh
ls /home/rtns/testdir/*.abc | sed -e 's:.*/::g' > serverA_files
ssh root@serverB ls "/home/rtns/a/*.abc" | grep "MPF.*\.abc" | sed -e 's:.*/MPF::g' > serverB_files
cmp -s serverA_files serverB_files
if [ $? -eq 0 ]; then
/home/rtns/x/a.sh;
ssh root@serverB "cd /home/rtns/testdir1; /home/rtns/testdir1/testscript"
else
comm -3 serverA_files serverB_files | /usr/bin/mailx -s "Some files not matched:" rahul_rtns@domain.com
fi
rm -f serverA_files serverB_files

 

All the script is ok, except that i am not getting the mail in my mailbox. Though the script is not giving any error or message.

 

Regards,

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 40 of 56 (860 Views)

Re: script regarding ssh

[ Edited ]

>I am not getting the mail in my mailbox.

 

Perhaps you don't have your system configured properly to send mail?

What does this do:

echo hello | mailx -s "test mail" rahul_rtns@domain.com

 

Take a look at /var/adm/syslog/mail.log.

 

If your problem is not obvious, search the "Messaging" board.  And then if no solutions, post another topic there.

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 41 of 56 (855 Views)

Re: script regarding ssh

I tried
>> echo hello | mailx -s "test mail" rahul_rtns@domain.com

In the mail.log it is giving log as :
Dec 20 00:58:01 houspiqa1 sm-mta[1945]: pBJ9FMjf003896: to=rahul_rtns@domain.com, ctladdr=rtns (116/20), delay=21:42:39, xdelay=00:00:00, mailer=relay, pri=4080152, relay=barbbh1.bwes.net., dsn=4.0.0, stat=Deferred: Connection timed out with barbbh1.bwes.net.
Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 42 of 56 (853 Views)

Re: script regarding ssh

>the mail.log it is giving log as:

>stat=Deferred: Connection timed out with barbbh1.bwes.net.

 

Can you do: nslookup barbbh1.bwes.net

 

But before we go to far, you need to create another topic in the correct board.

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 43 of 56 (851 Views)

Re: script regarding ssh

Hi Dennis,

 

The problem is not got ressolved. :)

 

Thank you very much for the prompt support. It was really wonderfull to learn a lot of things from you. Hope to communicate with you in near future.

 

Thanks once again..

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 44 of 56 (844 Views)

Re: script regarding ssh

Hi Dennis,

 

Just one more point:

 

>>cmp -s serverA_files serverB_files
if [ $? -eq 0 ]; then

In csh the statement if [ $? -eq 0 ]; is not working. May i know how to check the exit status in csh.

 

Regards,

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 45 of 56 (842 Views)

Re: script regarding ssh

[ Edited ]

>In csh the statement if [ $? -eq 0 ]; is not working. May I know how to check the exit status in csh.

 

Don't even think of using the scummy C shell.  :-(

mr google says it is something like: if ( $status == 0 ) then

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 46 of 56 (840 Views)

Re: script regarding ssh

I used if ($status == 0); then

But it is giving message as :

if: Empty if.

Regards,
Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 47 of 56 (839 Views)

Re: script regarding ssh

[ Edited ]

>I used if ($status == 0); then

 

It looks like I was missing some spaces but that ";" shoudn't be there.

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 48 of 56 (832 Views)

Re: script regarding ssh

>>ls /home/rtns/testdir/*.abc | sed -e 's:.*/::g' > serverA_files
     ssh root@serverB ls "/home/rtns/a/*.abc" | grep "MPF.*\.abc" | sed -e 's:.*/MPF::g' > serverB_files
     cmp -s serverA_files serverB_files
    

Can i have the statement for matching only the last file of *.abc pattern in both the servers. Actually there are some more files afer *.abc in both the servers. So it is not working. I want to match only the last file of *.abc pattern. That will work for me..

 

Regards,

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 49 of 56 (830 Views)

Re: script regarding ssh

>Can I have the statement for matching only the last file of *.abc pattern in both the servers.

 

Just pipe it to tail:

ssh root@serverB ls "/home/rtns/a/*.abc" | grep "MPF.*\.abc" | sed -e 's:.*/MPF::g' | tail -1 > serverB_files

Frequent Advisor
rahul_rtns
Posts: 43
Registered: ‎11-23-2011
Message 50 of 56 (800 Views)

Re: script regarding ssh

Hi,

 

I am having a little confusion about the feasiblity of the script that i have to prepart to match the files in two servers and if the files matched then trigger the archiving script in server A. The script that i have prepared is :

 

#!/usr/bin/csh
ls /oracle/MP1/oraarch/*.dbf | sed -e 's:.*/::g' > serverA_files
ssh root@serverB ls "/oracle/MP1/oraarch/*.dbf" | grep "MPFarch.*\.dbf" | sed -e 's:.*/MPFarch::g' > serverB_files
cmp -s serverA_files serverB_files
if ( $status == 0 ) then
/sapbasis/common_scripts/backup_archive.sh
else
comm -3 houeccp1_files austsapdr_files | mailx -s "archiving in prod failed" alias1

endif
rm -f serverA_files serverB_files

 

I am having confusion that when the files in both the servers has matched the backup_archive.sh script will run. But after comparison of the files in both the servers while the backup_archive.sh is running if a new file is generated in serverA, then that file will also get archived by the backup_archive.sh script, so will miss that newly generated file. So is there any way to reduce this risk.

 

Will really appreciate your help on this.

 

Regards,

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.