14

using scp to copy files from 1 unix server to another regularly and performing certain actions. to do this quickly I wish to use a unix script which does the scp and inputs the password required to complete the scp.

I have tried the expect command to send the password throught the unix command line however unable to achieve this so far.

sample commands

scp ./abc.txt hostname/abc.txt
expect "*password:*"
send "mypassword\r"

I get these errors:

couldn't read file "password: ": no such file or directory
myscript.sh[5]: send: not found [No such file or directory]

am I missing something?

cherry2891
  • 215
  • 1
  • 3
  • 13

6 Answers6

27

Just pass with sshpass -p "your password" at the beginning of your scp command

sshpass -p "your password" scp ./abc.txt hostname/abc.txt
Alexander
  • 9,737
  • 4
  • 53
  • 59
Akki
  • 557
  • 3
  • 7
  • 16
5

You should use better authentication with open keys. In these case you need no password and no expect.

If you want it with expect, use this script (see answer Automate scp file transfer using a shell script ):

#!/usr/bin/expect -f

# connect via scp
    spawn scp "user@example.com:/home/santhosh/file.dmp" /u01/dumps/file.dmp
#######################
expect {
-re ".*es.*o.*" {
    exp_send "yes\r"
    exp_continue
}
-re ".*sword.*" {
    exp_send "PASSWORD\r"
}
}
interact

Also, you can use pexpect (python module):

def doScp(user,password, host, path, files):
 fNames = ' '.join(files)
 print fNames
 child = pexpect.spawn('scp %s %s@%s:%s' % (fNames, user, host,path))
 print 'scp %s %s@%s:%s' % (fNames, user, host,path)
    i = child.expect(['assword:', r"yes/no"], timeout=30)
    if i == 0:
        child.sendline(password)
    elif i == 1:
        child.sendline("yes")
        child.expect("assword:", timeout=30)
        child.sendline(password)
    data = child.read()
    print data
    child.close()
Community
  • 1
  • 1
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
5

One of the ways to get around login issues with ssh, scp, and sftp (all use the same protocol and sshd server) is to create public/private key pairings.

Some servers may disallow this, but most sites don't. These directions are for Unix/Linux/Mac. As always, Windows is a wee bit different although the cygwin environment on Windows does follow these steps.

  • On your machine, create your public/private key using ssh-keygen. This can vary from system to system, but the program should lead you through this.
  • When ssh-keygen is finished, you will have a $HOME/.ssh directory on your machine. This directory will contain a public key and a private key. There will be two more files that are generated as you go along. One is known_hosts which contains the fingerprints of all known hosts you've logged into. The second will be called either authorized_keys or authorized_keys2 depending upon your implementation.
  • If it's not there already, log into the remote host, and run ssh-keygen there too. This will generate a $HOME/.ssh directory there as well as a private/public key pair. Don't do this if the $HOME/.ssh directory already exists and has a public and private key file. You don't want to regenerate it.
  • On the remote server in the $HOME/.ssh directory, create a file called authorized_keys. In this file, put your public key. This public key is found on your $HOME/.ssh directory on your local machine. It will end with *.pub. Paste the contents of that into authorized_keys. If authorized_keys already exists, paste your public key in the next line.

Now, when you log in using ssh, or you use scp or sftp, you will not be required to enter a password. By the way, the user IDs on the two machines do not have to agree. I've logged into many remote servers as a different user and setup my public key in authorized_keys and have no problems logging directly into that user.

Doing Private Public Key Authentication on Windows

If you use Windows, you will need something that can do ssh. Most people I know use PuTTY which can generate public/private keys, and do the key pairing when you login remotely. I can't remember all of the steps, but you generate two files (one contains the public key, one contains the private key), and configure PuTTY to use both of those when logging into a remote site. If that remote site is Linux/Unix/Mac, you can copy your public key and put it into the authorized_keys file.

If you can use SSH Public/Private keys, you can eliminate the need for passwords in your scripts. Otherwise, you will have to use something like Expect or Perl with Net::SSH which can watch the remote host and enter the password when prompted.

Community
  • 1
  • 1
David W.
  • 105,218
  • 39
  • 216
  • 337
  • Thank you for the explanation, perhaps I should have mentioned that the server pair I'm working with don't allow using a SSH key pair – cherry2891 Dec 09 '13 at 11:21
  • Then, your only hope is to use Expect or Net::SSH in Perl or the SSH package in Python (I'm not familiar with that usage. The Perl and I believe the Python packages will allow you to automatically send a password to SSH. There is no command line argument you can pass to the SSH password. That's done for security reasons. The only way around this is some sort of program that can _pretend_ to type in the password at the prompt. – David W. Dec 09 '13 at 15:43
  • @cherry2891 Yes, the administrator can disable ssh key/pairing (as I mentioned in my answer), and it's allegedly done for _security reasons_. Of course, if you have to automate logging in with the password, you end up someone writing an Expect/Perl/Python script with the plaintext password sitting there for the whole world to read. ``That's really secure.`` Go with the Expect/Perl/Python route (PowerShell can probably do it too if you're on Windows), or talk to the administrator about the need for automated downloads. – David W. Dec 09 '13 at 15:48
3

// copy /tmp/abc.txt to /tmp/abc.txt (target path)

// username and password of 10.1.1.2 is "username" and "password"

sshpass -p "password" scp /tmp/abc.txt username@10.1.1.2:/tmp/abc.txt

// install sshpass (ubuntu)

sudo apt-get install sshpass
KunMing Xie
  • 1,613
  • 17
  • 15
1

Firts as mentioned by David, we need to set up public/private key.

Then using below command had worked for me, means it didn't prompt me for password as we are passing private key in the command using -i option

scp -i path/to/private_key path/to/local/file remoteUserId@remoteHost:/path/to/remote/folder

Here path/to/private_key is private key file which we generated while setting up public/private key.

Manjunath D R
  • 371
  • 2
  • 8
-4

Here is how I resolved it.

It is not the most secure way however it solved my problem as security was not an issue on internal servers.

Create a new file say password.txt and store the password for the server where the file will be pasted. Save this to a location on the host server.

scp -W location/password.txt copy_file_location paste_file_location

Cheers!

cherry2891
  • 215
  • 1
  • 3
  • 13