如何在脚本中使用sftp而不需要用户交互(非交互式)。例如登录到匿名ftp服务器而无需手动操作。
在您的计算机上
cd ~/.ssh
ssh-keygen -t dsa
在每个提示处按下回车键。
Generating public/private dsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_dsa.
Your public key has been saved in /home/user/.ssh/id_dsa.pub.
The key fingerprint is:
ad:98:43:13:c9:ea:66:8e:d0:d9:66:59:d8:3a:f7:29
The key's randomart image is:
+--[ DSA 1024]----+
| |
| . . |
| + |
| + . . |
| o = S . |
| . + = + . |
|. o @ = . |
| . B oEo . |
| . . .o |
+-----------------+
id_dsa
和id_dsa.pub
,使用scp或其他工具将文件复制到您的服务器。请注意保留HTML标签,但不要写出解释。scp ~/.ssh/id_dsa.pub user@host:~/.ssh/
在您的服务器上
将新密钥添加到文件 ~/.ssh/authorized_keys 中。
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
验证访问模式是否正确 ~
ls -ld ~
chmod 700 ~
为了修正您的家庭访问权限。
退出并重新登录
输入以下命令:
ssh-keygen
按 Enter 键,直到出现提示符
ssh-copy-id -i root@ip_address
(它将要求您输入主机系统的密码)
ssh root@ip_address
现在您应该可以无需任何密码登录了
如果你想在使用FTP脚本文件进行FTP时关闭SFTP每次传输文件都要询问YES/NO的问题,可以使用-n -i
命令行参数。
ftpscript.in
-----------------
user username pwd
get sourcefile targetfileonlocal
bye
然后,您可以使用ftp -n -i servername<ftpscript.in
运行此脚本,以避免出现“是否要传输源文件?y/n”等问题。如果要在没有用户名、密码的情况下登录到FTP服务器,则需要像Wesley所述那样允许匿名登录到服务器FTP。