Execute commands after sshpass login in the script

时间:2017-06-09 12:49:15

标签: bash shell ubuntu

I'm an Ubuntu bash newbie. I successfully login to an sFTP server using sshpass. But once the connection is established I also need to download a directory from the server. My script cannot seem to pass the connection line though. This is what I have in my script (.sh) file:

#!/bin/bash
sshpass -p 'MY_PASSWORD' sftp -o StrictHostKeyChecking=no -o HostKeyAlgorithms=+ssh-dss MYUSER@MYSFTPSERVERADDRESS
echo "hello"
get -r Export

In the snipped above, my echo and my get are not executed. The terminal is waiting for my input with a sftp> prompt.

1 个答案:

答案 0 :(得分:1)

某些人使用scp代替sftp和共享密钥而不是将密码放在脚本中(如果可以的话),但是如果必须使用sftp,则会更好原因是,它可以从heredoc开始执行命令:

sshpass -p 'MY_PASS' sftp -o StrictHostKeyChecking=no -o HostKeyAlgorithms=+ssh-dss MYUSER@MYSFTPSERVERADDRESS <<EOF
get -r Export
EOF

请注意echo不是有效的sftp命令。

您可以在sftp之前执行您想要执行的任何命令EOF,并依次执行这些命令。

如果您只想获得该目录,如果可以的话,使用scp可能更简单:

sshpass -p 'MY_PASSWORD' scp -o StrictHostKeyChecking=no -o HostKeyAlgorithms=+ssh-dss -r MYUSER@MYSFTPSERVERADDRESS:Export .