bash 使用 scp 将文件从一台服务器复制到另一台服务器时发送密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20430119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:30:04  来源:igfitidea点击:

Send password when using scp to copy files from one server to another

bashshellunixkshscp

提问by cherry2891

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.

使用 scp 将文件从 1 个 unix 服务器定期复制到另一个服务器并执行某些操作。为了快速做到这一点,我希望使用一个 unix 脚本来执行 scp 并输入完成 scp 所需的密码。

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

我已尝试使用 expect 命令通过 unix 命令行发送密码,但到目前为止无法实现。

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?

我错过了什么吗?

采纳答案by cherry2891

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.txtand store the password for the server where the file will be pasted. Save this to a location on the host server.

创建一个新文件password.txt并存储将粘贴文件的服务器的密码。将其保存到主机服务器上的某个位置。

scp -W location/password.txt copy_file_location paste_file_location

Cheers!

干杯!

回答by Akki

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

只需sshpass -p "your password"scp命令的开头传递

sshpass -p "your password" scp ./abc.txt hostname/abc.txt

回答by Igor Chubin

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):

如果您想要它expect,请使用此脚本(请参阅回答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):

此外,您可以使用pexpect(python 模块):

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()

回答by David W.

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

其中一个要解决登录问题与方式sshscp以及sftp(全部使用相同的协议和sshd服务器)是建立公共/私有密钥对。

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 cygwinenvironment on Windows does follow these steps.

某些服务器可能不允许这样做,但大多数站点不允许。这些说明适用于 Unix/Linux/Mac。尽管Windows 上的cygwin环境确实遵循这些步骤,但与往常一样,Windows 有点不同。

  • 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-keygenis finished, you will have a $HOME/.sshdirectory 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_hostswhich contains the fingerprintsof all known hosts you've logged into. The second will be called either authorized_keysor authorized_keys2depending upon your implementation.
  • If it's not there already, log into the remote host, and run ssh-keygenthere too. This will generate a $HOME/.sshdirectory there as well as a private/public key pair. Don't do this if the $HOME/.sshdirectory already exists and has a public and private key file. You don't want to regenerate it.
  • On the remote server in the $HOME/.sshdirectory, create a file called authorized_keys. In this file, put your public key. This public key is found on your $HOME/.sshdirectory on your local machine. It will end with *.pub. Paste the contents of that into authorized_keys. If authorized_keysalready exists, paste your public key in the next line.
  • 在您的机器上,使用ssh-keygen. 这可能因系统而异,但程序应引导您完成此操作。
  • ssh-keygen完成后,你将有$HOME/.ssh你的机器上的目录。该目录将包含一个公钥和一个私钥。随着您的进行,将生成另外两个文件。一种是known_hosts包含您已登录的所有已知主机的指纹。第二个将被调用authorized_keysauthorized_keys2取决于您的实现。
  • 如果它不存在,请登录远程主机,并在ssh-keygen那里运行。这将在$HOME/.ssh那里生成一个目录以及一个私钥/公钥对。如果$HOME/.ssh目录已经存在并且有公钥和私钥文件,请不要这样做。你不想再生它。
  • $HOME/.ssh目录中的远程服务器上,创建一个名为authorized_keys. 在此文件中,放置您的公钥。此公钥$HOME/.ssh位于您本地计算机上的目录中。它将以*.pub. 将其内容粘贴到authorized_keys. 如果authorized_keys已经存在,请将您的公钥粘贴到下一行。

Now, when you log in using ssh, or you use scpor 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_keysand have no problems logging directly into that user.

现在,当您使用ssh、 或 使用scp或登录时sftp,您将不需要输入密码。顺便说一下,两台机器上的用户ID不必一致。我以不同的用户身份登录了许多远程服务器并设置了我的公钥,authorized_keys并且直接登录该用户没有问题。

Doing Private Public Key Authentication on Windows

在 Windows 上进行私钥公钥身份验证

If you use Windows, you will need something that can do ssh. Most people I know use PuTTYwhich can generate public/private keys, and do the key pairingwhen 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_keysfile.

如果您使用 Windows,您将需要一些可以做的事情ssh。我认识的大多数人都使用PuTTY,它可以生成公钥/私钥,并在您远程登录时进行密钥配对。我不记得所有的步骤,但是您生成了两个文件(一个包含公钥,一个包含私钥),并配置 PuTTY 以在登录远程站点时使用这两个文件。如果该远程站点是 Linux/Unix/Mac,您可以复制您的公钥并将其放入authorized_keys文件中。

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 Expector Perl with Net::SSHwhich can watch the remote host and enter the password when prompted.

如果您可以使用 SSH 公共/私人密钥,则可以消除脚本中对密码的需要。否则,您将不得不使用像Expect或 Perl 之类的东西和Net::SSH,它们可以监视远程主机并在出现提示时输入密码。

回答by KunMing Xie

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

// 复制/tmp/abc.txt 到/tmp/abc.txt(目标路径)

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

// 10.1.1.2的用户名和密码是“用户名”和“密码”

sshpass -p "password" scp /tmp/abc.txt [email protected]:/tmp/abc.txt

// install sshpass (ubuntu)

// 安装 sshpass (ubuntu)

sudo apt-get install sshpass

回答by Manjunath D R

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

然后使用下面的命令对我有用,这意味着它没有提示我输入密码,因为我们使用 -i 选项在命令中传递私钥

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.

这里 path/to/private_key 是我们在设置公钥/私钥时生成的私钥文件。