Ssh in the background

Hi,

I am trying to execute an ssh command in my script.
ssh abcd@server_name
After this command it actually logs in to the server asking for password prompt and then actually logs in to the server.

I want all this to be happening in the background and show noithing in the output of my script.
I just want to login to the server in the background and check if the file exists and sftp it.

How do i do it ? without showing anything on my script.

You should work with private/public keys to avoid the login and use scp instead of sftp.

Redirect stdout and stderr to a log file (for error analysis) or to /dev/null.

1 Like

Let me get this straight. After connecting via ssh to a remote server, you want to check whether a certain file exists there and if it doesn't, upload it from your localhost using sftp, correct? Please confirm and we'll be more than glad to help.

1 use password less ssh concept into use.
2 do validation of required files
3 if pass, sftp the file

Yes thats correct....just that mre than uploading I would be getting some files from that server.

I tried what rudic suggested and it has helped.
Would like to know now:

Once I have logged in...i want to sftp file to another server .,....how can I do that ?
I do not wan the sftp> prompt to be shown.
The file should be sftped in the background.

Can you please help ?

Ok. So you're logged in to your first server, and from there you're going to sftp the file to another server.
Try this:

sftp user@2ndServer <<END_SEND
	cd $directory
	put $file
	quit
END_SEND

This code snippet makes the following assumptions:

  1. the 1st server is both a server (from your localhost's point of view) and a client (from the 2nd server point of view).
  2. The remote sftp service is listening on the default port, that is, port 22 (other wise, you need to add the -oPort switch, like this: -oPort=1111 , for example).
  3. In your script, before you use this code, you need to set the following variables: directory (for remote directory, use an absolute path), file (the file you want to sftp to the 2nd server)

Let me know how it goes.

Thanks.
Will try the above ...just wanted to know what does END_SEND stand for here ?

END_SEND is an identifier of what is called a here document. Check this link for a more thorough explanation of here documents and what they are usually used for. Also, Wikipedia has a nice article about here documents with examples.