bash 如何与 SFTP 服务器连接以获取自动 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51162395/
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
How to connect with SFTP server for automated shell script
提问by Piyush Patel
I m trying to connect to SFTP server to fetch the data files regularly using automated shell script. I connect to the server as
我正在尝试连接到 SFTP 服务器以使用自动 shell 脚本定期获取数据文件。我连接到服务器
user@$server
where it asks for password and I enter the password then I can run regular SFTP commands get
and mget
to fetch files.
user@$server
它要求输入密码,然后我输入密码,然后我可以运行常规 SFTP 命令get
并mget
获取文件。
The concern is that I have to provide password manually and it opens interactive shell. How can I run all these in just one command or without manual intervention.?
问题是我必须手动提供密码并打开交互式 shell。如何仅在一个命令中运行所有这些或无需人工干预。?
Although How to run the sftp command with a password from Bash script?answers little bit, I don't have privileges to install additional commandline utilities on my server.
尽管如何使用 Bash 脚本中的密码运行 sftp 命令?稍微回答一下,我没有在我的服务器上安装其他命令行实用程序的权限。
I was wondering if there is any other way to solve this issue.
我想知道是否有其他方法可以解决此问题。
回答by GuiGWR
You can try the expectcommand:
你可以试试expect命令:
#!/usr/bin/expect -c "
spawn sftp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no <USER>@<SERVER>
expect \"*?assword:\"
send \"<PASSWORD>\r\"
expect \"sftp>\"
#more commands
#exit sftp when you're done
send \"exit\r\"
expect -exact \"$\"
"
It'll enter the password without manual intervention.
它会在没有人工干预的情况下输入密码。
UserKnownHostsFile=/dev/null
will disable the host key checking and StrictHostKeyChecking=no
will disable strict host key checking.
UserKnownHostsFile=/dev/null
将禁用主机密钥检查StrictHostKeyChecking=no
并将禁用严格的主机密钥检查。
You can add that example in the #more commands
section:
您可以在该#more commands
部分添加该示例:
#Example with get (from remote to local)
send \"get <remotefilepath> <localfilepath>\r\"
expect \"sftp>\"