如何将密码传递给scp?
我知道这不是build议的,但它是完全可能将用户的密码传递给SCP?
我想通过scp复制文件作为批处理作业的一部分,接收服务器当然需要密码,不,我不能轻易将其更改为基于密钥的身份validation。
你可以使用像预期的工具来编写脚本(也有方便的绑定,比如Python的Pexpect )。
使用sshpass :
sshpass -p "password" scp -r user@example.com:/some/remote/path /some/local/path 或者密码不显示在bash历史中
 sshpass -f "/path/to/passwordfile" scp -r user@example.com:/some/remote/path /some/local/path 
以上内容复制了从远程主机到本地的path。
安装:
-   Ubuntu的/ Debian的
-  apt install sshpass
 
-  
-   CentOS的/ Fedora的
-  yum install sshpass
 
-  
-   mac w / macports
-  port install sshpass
 
-  
-   mac w / brew
-  brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb
 
-  
 如果从Windows连接到服务器,则可以使用putty版本的scp(“pscp”)将密码与-pw参数一起传递。 
这在这里的文档中提到。
只需要生成一个ssh密钥:
 ssh-keygen -t rsa -C "your_email@youremail.com" 
 复制~/.ssh/id_rsa.pub的内容,最后将其添加到远程机器~/.ssh/authorized_keys 
 确保远程机器0700 for ~./ssh folder具有权限0700 for ~./ssh folder 0600 for ~/.ssh/authorized_keys具有权限0600 for ~/.ssh/authorized_keys 
你可以在unix / terminal上使用'expect'脚本
例如创build'test.exp':
 #!/usr/bin/expect spawn scp /usr/bin/file.txt root@<ServerLocation>:/home set pass "Your_Password" expect { password: {send "$pass\r"; exp_continue} } 
运行脚本
 expect test.exp 
我希望有帮助。
 下面是一个如何使用expect工具来实现的例子: 
 sub copyover { $scp=Expect->spawn("/usr/bin/scp ${srcpath}/$file $who:${destpath} +/$file"); $scp->expect(30,"ssword: ") || die "Never got password prompt from + $dest:$!\n"; print $scp 'password' . "\n"; $scp->expect(30,"-re",'$\s') || die "Never got prompt from parent +system:$!\n"; $scp->soft_close(); return; } 
没有人提到它,但腻子scp (pscp)有一个-pw选项的密码。
文档可以在这里find: https : //the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter5.html#pscp
- 
确保你有“期待”的工具,如果没有,做到这一点 # apt-get install expect
- 
用下面的内容创build一个脚本文件。 (#vi / root / scriptfile) spawn scp /path_from/file_name user_name_here@to_host_name:/path_toexpect "password:"send put_password_here\n;interact
- 
用“expect”工具执行脚本文件 # expect /root/scriptfile
另一种方法是将用户密钥的公共一半添加到目标系统上的授权密钥文件中。 在启动传输的系统上,您可以运行一个ssh-agent守护进程,并将该私钥的一半添加到代理。 然后可以将批处理作业configuration为使用代理获取私钥,而不是提示密钥的密码。
这应该可以在UNIX / Linux系统或使用pageant和pscp的Windows平台上执行。
curl可以用来替代scp来复制文件,并且它在命令行上支持密码。
 curl --insecure --user username:password -T /path/to/sourcefile sftp://desthost/path/ 
 您可以使用ssh-copy-id添加ssh密钥: 
 $which ssh-copy-id #check whether it exists 
如果存在:
 ssh-copy-id "user@remote-system" 
 一旦你设置ssh-keygen如上所述,你可以做 
 scp -i ~/.ssh/id_rsa /local/path/to/file remote@ip.com:/path/in/remote/server/ 
 如果你想减less每次input,你可以修改你的.bash_profile文件并放置 
 alias remote_scp='scp -i ~/.ssh/id_rsa /local/path/to/file remote@ip.com:/path/in/remote/server/ 
 然后从你的terminal做source ~/.bash_profile 。 之后,如果您在terminal中键入remote_scp ,则应该运行不带密码的scp命令。 
我发现这真的很有帮助。
 rsync -r -v --progress -e ssh user@remote-system:/address/to/remote/file /home/user/ 
不仅可以传递密码,还可以在复制时显示进度条。 非常棒。