3

I have an sftp connection to a server in Unix. Without password, I use the syntax to connect and execute command

sftp -b $user@$server_name

Can anyone suggest me how can I write a shell script to connect a remote server non interactively using a password

1

3 Answers 3

3

Try with this below option,

lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit

--EOF--

Sign up to request clarification or add additional context in comments.

Comments

1

You could use ~/.ssh/config file.

#
# ~/.ssh/config
#

Host servername
Hostname 127.127.127.127
Port 22
User root

#EOF: config

Then simply connect with "ssh servername" and if you don't want to use password you can use SSH key. Here is good tutorial on how to do that > http://www.cyberciti.biz/tips/linux-multiple-ssh-key-based-authentication.html

If you just want to pass user/server from terminal, you can do this.

#!/bin/bash
sftp -b "$1"@"$2"

then use it like this './sftp.sh user server'

use SCP like this;

scp -P 22 user@server:/dir/file.tgz ~/Desktop/

use SFTP like this;

sftp user@server:/dir/file.tgz ~/Desktop/file.tgz

You can also try this;

sftp user@host <<EOF
get /dir/file.tgz
rm /dir/file.tgz
EOF

3 Comments

I don't have root access or don't have the external access. I need to utilize the existing details and use
I am trying to download the files from the external server
after successful transfer, I am supposed to delete the files as well
0

The best way to do this would be to create a key pair on the client, and add the key to the target user's ~/.ssh/authorized_keys.

To create a key pair, run ssh-keygen and when it asks for a password, just hit return to indicate "no password". Then either run ssh-copy-id $user@$server_name or manually create a ~/.ssh/authorized_keys on the server and copy the contents of the ~/.ssh/id_rsa.pub from the client into it (ssh-copy-id isn't available on all machines, so on some you'll have to do it manually).

Now you should be able to run ssh or scp without a password, as it should use your key instead. If it doesn't work, make sure that the permissions on your ~/.ssh/ directory and contents are correct on both machines; the directory should be 0700 (drwx------) and the files should be 600 (-rw-------). Also check that key authentication is enabled on both the client and the server.

3 Comments

I have neither root access nor the control on external server. I can connect to the remote server using sftp connecting by entering the password
@Trinadh If that's the case, then the answer about using expect is probably the best you can do. But if possible, you should see if the administrator can set up key based authentication for you, as it's simpler and more secure.
@Trinadh Can you log into that remote machine. Then you don't need root or control privileges. ssh-keygen operates on the user level. You run the command on the server (to create a private key) and on your client (to create a public key). You then copy your private key into the authorized_keys file. I do it all of the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.