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?
// 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 [email protected]:/tmp/abc.txt
// install sshpass (ubuntu)
sudo apt-get install sshpass
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.
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 "[email protected]:/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()
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.
ssh-keygen
. This can vary from system to system, but the program should lead you through this.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.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.$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.
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.
Just pass with sshpass -p "your password"
at the beginning of your scp
command
sshpass -p "your password" scp ./abc.txt hostname/abc.txt
Source: Stackoverflow.com