我知道不建议这样做,但是是否有可能将用户的密码传递给scp?
我想通过scp复制一个文件,作为批处理作业的一部分,接收服务器当然需要密码,不,我不能轻易地将其更改为基于密钥的身份验证。
我知道不建议这样做,但是是否有可能将用户的密码传递给scp?
我想通过scp复制一个文件,作为批处理作业的一部分,接收服务器当然需要密码,不,我不能轻易地将其更改为基于密钥的身份验证。
当前回答
您可以在unix/terminal上使用'expect'脚本
例如,创建'test。经验值:
#!/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;
}
上面提到的所有解决方案都可以工作,只有当你安装了应用程序,或者你应该有管理权限安装除sshpass。
我发现这个非常有用的链接可以简单地在后台启动scp。
$ nohup scp file_to_copy user@server:/path/to/copy/the/file > nohup.out 2>&1
https://charmyin.github.io/scp/2014/10/07/run-scp-in-background/
您可以使用像expect这样的工具编写脚本(也有方便的绑定,比如Python的Pexpect)。
从Windows以非交互方式使用SCP:
安装社区版netcmdlets 导入模块 使用Send-PowerShellServerFile -AuthMode password -User MyUser -Password not-secure -Server YourServer -LocalFile C:\downloads\test.txt -RemoteFile C:\temp\test.txt发送非交互式密码的文件
一旦像上面解释的那样设置了ssh-keygen,就可以这样做了
SCP -i ~/。Ssh /id_rsa /local/path/to/file remote@ip.com:/path/in/remote/server/
如果希望减少每次输入的次数,可以修改.bash_profile文件并将
alias remote_scp='scp -i ~/.ssh/id_rsa /local/path/to/file remote@ip.com:/path/in/remote/server/
然后从您的终端执行source ~/.bash_profile。然后,如果您在终端中输入remote_scp,它应该运行没有密码的scp命令。