1748002 Members
4589 Online
108757 Solutions
New Discussion

script regarding ssh

 
rahul_rtns
Frequent Advisor

script regarding ssh

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.

55 REPLIES 55
Dennis Handly
Acclaimed Contributor

Re: script regarding ssh

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.

rahul_rtns
Frequent Advisor

Re: script regarding ssh

Instead of using

ssh server2 ls "/somepath/*.dbf" | grep -q "/somepath/.*\.dbf"

can i use :

ssh server2 test -f /somepath/*.dbf

Regards
Dennis Handly
Acclaimed Contributor

Re: script regarding ssh

>can I use: ssh server2 test -f /somepath/*.dbf

 

No.  -f only takes one file, not a pattern.

rahul_rtns
Frequent Advisor

Re: script regarding ssh

Thnx..

May i know the meaning of this line :

if [ $? -eq 0 ];

What will $? -eq 0 do?

Regards
Dennis Handly
Acclaimed Contributor

Re: script regarding ssh

>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.

rahul_rtns
Frequent Advisor

Re: script regarding ssh

Thnx Dennis,
Just one more point :

I want to ssh to the server and want to login using root credentials. May i know how in every ssh it will login with root and will not ask for password. If the password need to be given, then how can it be applied in the script.

Regards..
rahul_rtns
Frequent Advisor

Re: script regarding ssh

thnx..

Can i use local user to run the script. What is the procedure to run the script by local user..

Regards
Dennis Handly
Acclaimed Contributor

Re: script regarding ssh

>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.