Hello,
Suppose i am logged in server 1. I want to ssh to server 2. There I want to check for the presence of a particular type of files ( for eg .dbf files in /somepath). Then if the file is present, it has to exit ssh and trigger another script (for eg backup.ssh at /somepath2) which is there in server 1. But if the file is not present in server 2 then nothing should happen.
You should be able to use ssh and ls to list the files you want.
Then grep the output and if present run that other script:
ssh server2 ls "/somepath/*.dbf" | grep -q "/somepath/.*\.dbf"
if [ $? -eq 0 ]; then
/somepath2/backup.ssh
fi
Note: This assumes that you can use ls instead of using find to search a whole tree.
>can I use: ssh server2 test -f /somepath/*.dbf
No. -f only takes one file, not a pattern.
>What will $? -eq 0 do?
This checks the exit status of the previous command. In this case, grep found some matches in the ls output.
>I want to ssh to the server and want to login using root credentials.
You need to search for topics about setting up ssh:
http://h30499.www3.hp.com/t5/System-Administration/Cannot-passwordlessly-ssh-using-root/m-p/5381979/
http://h30499.www3.hp.com/t5/System-Administration/SSH-question/m-p/4299800/
http://h30499.www3.hp.com/t5/General/SSH-Host-based-Authentication-setup/m-p/4902860/
>Can I use local user to run the script? What is the procedure to run the script by local user?
You don't need to be root, you just need to be able to list those directories.
You have to do the same ssh setup, except you don't need to give away the keys to the city by using root.
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,
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.
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,
>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?
>>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,
>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
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,
>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
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,
> /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.
Hi dennis,
Thnx for the script. I have tested a test script based on above script and it is working successfully. Just one more concern i m having. I am providing the test script which i have tested:
#!/usr/bin/sh
ls /home/rtns/testdir/*.abc | sed -e 's:.*/::g' > serverA_files
ssh root@austsapdr 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
sh /home/rtns/x/a.sh
else
: echo "Some files not matched:"
: comm -3 serverA_files serverB_files
fi
rm -f serverA_files serverB_files
>>if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh
If the exit status is not 0 then the script will not run. But if the exit status is zero and the script when runs, then after that an ssh should be done to serverB to run a script on serverB. Or any other way to do this??
Regards,
>Thnx for the script. I have tested a test script based on above script and it is working successfully.
(If you are happy, please click on the Kudos! star of the posts that helped.)
>sh /home/rtns/x/a.sh
There is no need to use "sh" on this line. Just make sure your script is executable and has "#!..." on the first line.
>>if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh
>if the exit status is zero and the script runs, then after that an ssh should be done to serverB to run a script on serverB. Or any other way to do this?
You can either put that ssh into a.sh (but it would have to know about serverB) or add that ssh right after.
Do you also need to check the exit status of a.sh?
>>if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh
>>Do you also need to check the exit status of a.sh?
No. I dont need the exit status of a.sh. I am concern of only that if a.sh runs (i.e. status of [ $? -eq 0 ] is zero) then only ssh is to do done to serverB to run a script there and if a.sh do not runs (i.e. status of [ $? -eq 0 ] is non-zero), then ssh should not be done to serverB.
Regards,
>I am concern of only that if a.sh runs then only ssh is to do done to serverB to run a script there ...
That "if" will do that. You just need to add that ssh in the "then" block after a.sh.
I tried adding another "then" as below :
#!/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
sh /home/rtns/x/a.sh; then
ssh root@serverB sh /home/rtns/testdir1/testscript
else
: echo "Some files not matched:"
: comm -3 serverA_files serverB_files
fi
rm -f serverA_files serverB_files
But it given the following error :
root@serverA:/home/rtns>./testscript2 (testscript2 is the above script)
Password:
Password:
./testscript2[5]: Syntax error at line 7 : `then' is not expected.
Regards,