cancel
Showing results for 
Search instead for 
Did you mean: 

Re: script regarding ssh

 
Highlighted
Frequent Advisor

Re: script regarding ssh

Hello..

 

As per you given script above i have made a test script as below :

 

root@serverA:/home/rtns>cat testscript
#!/usr/bin/sh
ssh rets@serverB ls "/home/rtns/a/*.abc" | grep -q "MPF*.abc"
if [$? -eq 0]; then
rmdir abc  (abc dirctory i have temporarily made in /home/rtns)
fi

 

Already i am able to do passwordless ssh and able to list the files. But when running this script i am gettin below error :

 

root@serverA:/home/rtns>./testscript
./testscript[3]: [1:  not found.

 

following test files i have created in serverB in /home/rtns/a :

 

 1_66.abc
 2_66.abc
 MPF1_1_66.abc
 MPF1_2_66.abc
 MPF1_3_66.abc

 

Please mention in the script where i am doing a mistake.

 

Regards,

 

 

Acclaimed Contributor

Re: script regarding ssh

if [$? -eq 0]; then
   rmdir abc  # (abc directory I have temporarily made in /home/rtns)
fi

 

>when running this script I am getting below error :

./testscript[3]: [1:  not found.

 

Spacing is important when using [ ] or [[ ]].  You need to cut&paste from the post:

if [ $? -eq 0 ]; then

 

Note the "1" comes from $? which indicates the grep failed.  There is a typo there in the regex, change to:

   ssh rets@serverB ls "/home/rtns/a/*.abc" | grep -q "MPF.*\.abc"

 

Note: grep takes a regex but ls(1) takes a pattern.

 

>rmdir abc

 

This isn't likely to work if abc still contains files.

Highlighted
Frequent Advisor

Re: script regarding ssh

Hi Dennis,

 

Thank u very much for ur support.

 

Your solution worked..

 

But i m having one more concern:

 

ssh rtns@serverB ls "/home/rtns/a/*.abc" | grep -q "MPF.*\.abc"

 

The above command checks all the files of MPF*.abc in serverB. The concern i m having is in serverA the files are generated with .abc extension and with serial nos. For e.g. 1_46446_652110140.abc, 1_46447_652110140.abc and so on. And in serverB they are shipped as MPF1_46446_652110140.abc, MPF1_46447_652110140.abc and so on. I want script that should check that a particular no. of file that has been generated on serverA has been shipped on serverB or not. Say in above case like if 46th file is generated on serverA and if it is shipped in serverB, then it should check the presence of 46th file on serverB and then only it should trigger the other script in serverA. And if the file is not present nothing should happen.

 

Thanks in advance,

 

Regards,

Highlighted
Acclaimed Contributor

Re: script regarding ssh

>serverA:         1_46446_652110140.abc, ...

>serverB: MPF1_46446_652110140.abc, ...

>I want script that should check that a particular no. of file that has been generated on serverA has been shipped on serverB or not. Say in above case like if 46th file is generated on serverA and if it is shipped in serverB, then it should check the presence of 46th file on serverB and then only it should trigger the other script in serverA.

 

Do you want to check to see if there is a match on ALL files that are copied to serverB?

Or do you want to invoke that script if the matching files are on both machines?

 

And do you want to invoke the script for each match or only once if any matches?

Highlighted
Frequent Advisor

Re: script regarding ssh

>>Do you want to check to see if there is a match on ALL files that are copied to serverB?

Or do you want to invoke that script if the matching files are on both machines?

 

I will run the script through crontab. I want to invoke the script if the matching files are on both machines. And the script should not run if the file that is present on serverA has not shipped to serverB. That is if file is present on serverA and not present on serverB, then script should not run. That match should be regarding the particular no. of file.

 

Please revert if any more concern is there.

 

Regards,

 

 

Highlighted
Acclaimed Contributor

Re: script regarding ssh

>I want to invoke the script if the matching files are on both machines.

 

So you don't care if there are extra files on serverA or serverB.

 

# get basenames of files on each server

ls /serverA-path/*.abc | sed -e 's:.*/::g' > serverA_files

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

 

# find matches:

comm -12 serverA_files serverB_files > matched_files

if [ -s matched_files ]; then  # some matches

   run-script-here

fi

rm -f matched_files # cleanup

Highlighted
Frequent Advisor

Re: script regarding ssh

Hi dennis,

 

Thank u very much for the script.

 

I m having some queries about the script that i am mentioning below :

 

>># get basenames of files on each server

     ls /serverA-path/*.abc | sed -e 's:.*/::g' > serverA_files

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

 

What are the serverA_files and serverB_files. Are they the folders that i have to make in respective directories. In the severB the .abc files are having MPF extension also. Whether that MPF will be removed while matching the files.

 

>>comm -12 serverA_files serverB_files > matched_files

     if [ -s matched_files ]; then  # some matches 

    run-script-here

    fi

    rm -f matched_files # cleanup

 

If some files are matched only, then also the script will run or the script will run only after all files are matched. My concern is to invoke the script only after all files on serverA and serverB are matched. If only some files are matched, then script should not run.

 

Regards,

 

 

 

 

Highlighted
Acclaimed Contributor

Re: script regarding ssh

>What are the serverA_files and serverB_files. In the serverB the .abc files are having MPF extension also. Whether that MPF will be removed while matching the files.

 

These are files that contain the list of files on each machine.

The sed command will remove the leading "MPF".

 

>My concern is to invoke the script only after all files on serverA and serverB are matched. If only some files are matched, then script should not run.

 

Ok, then:

# find matches:
cmp -s serverA_files serverB_files
if [ $? -eq 0 ]; then  # all matched
   run-script-here

else

   echo "Some files not matched:"

   comm -3 serverA_files serverB_files

fi

rm -f  serverA_files serverB_files # cleanup

Highlighted
Frequent Advisor

Re: script regarding ssh

Hi dennis,

 

Thank u very much for the script. I am having a couple of questions for the above script:

 

>>  if [ $? -eq 0 ]; then  # all matched

       /pathtoscript/backup.sh

 

      whether the script will successfully run by typing the script as above. Or is there any other way to run the script.

 

>>else

    echo "Some files not matched:"

    comm -3 serverA_files serverB_files

 

I will run the script through cron entry per hour. So where the output of the above echo command will be printed.

 

Regards,


  

 

Highlighted
Acclaimed Contributor

Re: script regarding ssh

>     /pathtoscript/backup.sh

>whether the script will successfully run by typing the script as above. Or is there any other way to run the script?

 

I don't know how to run your script or what parms it takes??

 

>    echo "Some files not matched:"

>    comm -3 serverA_files serverB_files

 

>I will run the script through cron entry per hour. So where the output of the above echo command will be printed.

 

It will be mailed to you.  I left it there in case you wanted some debugging output.

You could comment it out like:

   :  echo "Some files not matched:"

   : comm -3 serverA_files serverB_files

 

I used ":" vs "#" since there must be something in the else block.