[英]Doing sudo with SSH.NET SFTP to access files
我正在尝试使用SSH.NET通过 SFTP 从服务器读取/下载文件。 连接工作; 我可以枚举感兴趣的目录; 但是当我尝试读取文件内容时,出现消息“权限被拒绝”的异常。 (下面的堆栈跟踪;我没有看到任何其他有用的东西——没有内部例外。)
这可能是问题所在:当我ssh
进入服务器(使用相同的密钥)时,我必须执行sudo vim
来查看文件内容,否则它也会给我“权限被拒绝”。 请注意,它不会让我输入密码来使用sudo
。 我只是不确定这如何通知我的代码更改。
using Renci.SshNet;
using System;
using System.IO;
using System.Text;
...
try {
ConnectionInfo connectionInfo;
using (var keyStream = new MemoryStream(Encoding.UTF8.GetBytes(GetSshKey())))
{
connectionInfo = new ConnectionInfo(Domain, Username,
new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(keyStream)));
}
// Connect to the server with SFTP
using var client = new SftpClient(connectionInfo);
client.Connect();
if (!client.Exists(RemoteFolder))
{
throw new Exception("The backup folder was not found!");
}
// Save block blobs to local files
foreach (var file in client.ListDirectory(RemoteFolder))
{
if (!file.IsRegularFile) continue; // Skip any directories or links
var readStream = client.OpenRead(file.FullName); // Exception thrown HERE!
var writeStream = File.OpenWrite($"C:\\temp\\{file.Name}");
readStream.CopyTo(writeStream);
writeStream.Dispose();
readStream.Dispose();
}
} catch (Exception e)
{
throw e;
}
堆栈跟踪:
at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path, Flags flags, Boolean nullOnError)
at Renci.SshNet.Sftp.SftpFileStream..ctor(ISftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize)
at Renci.SshNet.SftpClient.Open(String path, FileMode mode, FileAccess access)
at Renci.SshNet.SftpClient.OpenRead(String path)
at Playground.Program.Main(String[] args) in C:\...\Program.cs:line 40
我尝试了DownloadFile
以及BeginDownloadFile
和EndDownloadFile
,但在这些情况下, DownloadFile
和EndDownloadFile
分别引发了类似的“权限被拒绝”错误。 堆栈跟踪:
at Renci.SshNet.Common.AsyncResult.EndInvoke()
at Renci.SshNet.Common.AsyncResult`1.EndInvoke()
at Renci.SshNet.Sftp.SftpSession.EndOpen(SftpOpenAsyncResult asyncResult)
at Renci.SshNet.ServiceFactory.CreateSftpFileReader(String fileName, ISftpSession sftpSession, UInt32 bufferSize)
at Renci.SshNet.SftpClient.InternalDownloadFile(String path, Stream output, SftpDownloadAsyncResult asyncResult, Action`1 downloadCallback)
at Renci.SshNet.SftpClient.DownloadFile(String path, Stream output, Action`1 downloadCallback)
at Playground.Program.Main(String[] args) in C:\...\Program.cs:line 41
您用于登录 SSH.NET 的帐户没有文件的读取权限。
SFTP 协议中没有与sudo
等效的功能。
但是,如果您要连接到 OpenSSH 服务器,则可以使用以下命令指示它以提升的权限运行 SFTP 子系统/服务器:
sudo /bin/sftp-server
但是您不能直接使用 SSH.NET 执行此操作。 不过,通过对其代码进行一些修改,它可能是可行的。 毕竟 SSH.NET 是开源的。
有关更多背景信息,请参阅 WinSCP 常见问题解答如何在登录后更改用户(例如 su root)?
在你开始破解这个之前,先在 WinSCP GUI 中尝试它以确保它是可行的。
另一种选择是在 shell ( SshClient.RunCommand
) 中使用sudo
来制作可由您的 SFTP 帐户访问的文件的副本。 但这取决于您的sudo
权限是什么。
或者您可以尝试使用WinSCP .NET 程序集,代码如下:
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "example.com",
UserName = "username",
Password = "password",
SshHostKeyFingerprint = "...",
};
sessionOptions.AddRawSettings("SftpServer", "sudo /bin/sftp-server");
using (Session session = new Session())
{
session.Open(sessionOptions);
// Your code
}
(我是 WinSCP 的作者)
最好的选择是直接使用具有文件读取权限的帐户登录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.