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

Trinadh Sidda
  • 69
  • 1
  • 2
  • 8
  • Does this help: http://stackoverflow.com/questions/5386482/how-to-run-the-sftp-command-with-a-password-from-bash-script?rq=1 – anubhava Nov 11 '13 at 14:54

3 Answers3

3

Try with this below option,

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

--EOF--

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
cmdilip
  • 99
  • 1
  • 3
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
Tux
  • 247
  • 2
  • 14
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.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • 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 Sidda Nov 11 '13 at 14:34
  • 1
    @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. – Brian Campbell Nov 11 '13 at 14:44
  • @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. – David W. Nov 11 '13 at 14:47