erlang 如何引用真实的sftp并将文件复制到我的目录

x7rlezfr  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(134)

我是Erlang新手。告诉我如何引用sftp并使用Erlang代码复制文件。有人能提供类似示例或文档的链接吗?谢谢!

7qhs6swi

7qhs6swi1#

Host = "...". %% remote host
Port = 22. %% port
Options = [{user, "..."}, %% ssh remote user
           {silently_accept_hosts, true}, %% 
           {user_dir, "/path/to/.ssh"} %% ssh directory on local machine
          ].

RemoteFilePath = "". %% path to where you need to sftp local file
{ok, FileData} = file:read_file("/path/to/local/file").

%% connect to remote host
{ok, SshConnection} = ssh:connect(Host, Port, Options).

%% start sftp channel
{ok, Channel} = ssh_sftp:start_channel(SshConnection).

%% ftp the file
ok = ssh_sftp:write_file(Channel, RemoteFilePath, FileData).

%% stop sftp channel
ssh_sftp:stop_channel(Channel).

%% close ssh connection
ssh:close(SshConnection).

要使代码正常工作,您需要使用SSH密钥在本地计算机和远程主机之间设置SSH访问。

cbeh67ev

cbeh67ev2#

问题已解决。选项= [{用户,“..."},{密码,“..."},...]谢谢大家

相关问题