如何正确使用 sshpass 以及密码和不同的端口?

How to make correct use of sshpass along with password and different port?

我正在 jenkins 中构建一个作业,我需要将文件夹和文件从 jenkins 服务器复制到另一个服务器。服务器有端口 802 和密码= "PASSWORD" 如何将文件夹复制到另一台服务器?

sshpass -p'PASSWORD'scp -P 802 $SSH_OPTS  -r $WORKSPACE/target/abc.war root@2xx.xx.xxx.xx:/root/war/

Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
   -f filename   Take password to use from file
   -d number     Use number as file descriptor for getting password
   -p password   Provide password as argument (security unwise)
   -e            Password is passed as env-var "SSHPASS"
   With no parameters - password will be taken from stdin

   -P prompt     Which string should sshpass search for to detect a password prompt
   -v            Be verbose about what you're doing
   -h            Show help (this screen)
   -V            Print version information
At most one of -f, -d, -p or -e should be used
Build step 'Execute shell' marked build as failure
Finished: FAILURE

最好的方法是使用 expect 脚本,您可以使用以下代码:

!/usr/bin/expect -f

用法sshsudologin.expect(主机)(ssh 用户)(ssh 密码)

设置超时 10

spawn scp -r "/folder_to_copy/" [lindex $argv 1]@[lindex $argv 0]:"destination_dir/"

期待“*?assword”{发送“[lindex $argv 2]\r”}

期待eof