我正在寻找.net的免费DLL来处理SFTP连接。
我发现了这个项目SharpSSH,但它没有文档。
我花了很多时间来计算dll是如何工作的。我创建了一个测试项目,然后开始测试不同的功能。某些功能正在起作用,例如删除文件。
我有putfile()函数和getfile()的问题。
这是一个例子:
Dim ssh As SFTPUtil
ssh = New SFTPUtil("MY SERVER", "MY USER", "MY PW")
ssh.GetFile("/home/sftptest/test111.xml", "C:\\text.xml")
请注意,getfile()参数为:
Public Sub GetFile(remotePath As String, localPath As String)
我介入了这些功能,但我没有找到传递这些参数的正确方法。
我真的不知道我是否应该使用斜杠(/)或反斜杠()。我知道Linux使用(/)
我注意到例如" C:\"已被转变为" C:\\"。
提到SFTP是在linux机器上。
谢谢。答案 0 :(得分:6)
这是我应该做的(vb.net代码)与THIS library (SSHnet)建立连接,我做不使用SharpSHH:
Private Sub ButtonAction_Click(sender As Object, e As System.EventArgs) Handles ButtonAction.Click
Dim PasswordConnection = New PasswordAuthenticationMethod("test", "test")
Dim KeyboardInteractive = New KeyboardInteractiveAuthenticationMethod("test")
Dim ConnectionInfo = New ConnectionInfo("192.168.1.1", 22, "test", PasswordConnection, KeyboardInteractive)
AddHandler KeyboardInteractive.AuthenticationPrompt, _
Sub(_sender As Object, _e As Renci.SshNet.Common.AuthenticationPromptEventArgs)
For Each prompt In _e.Prompts
Debug.Print(prompt.Request)
If Not prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) = -1 Then
prompt.Response = "test"
End If
Next
End Sub
sftp = New SftpClient(ConnectionInfo)
sftp.Connect()
sftp.disconnect()
End Sub
答案 1 :(得分:1)
这个库充满了bug。我在11/2012下载了版本,即使使用像Disconnect这样的简单功能,我也有很大的问题。应用程序将冻结,您必须重新启动它。
答案 2 :(得分:0)
我有一些问题需要了解你想要什么,所以这里是来自SSH.NET包的示例代码。 (不是SharpSSH)
string IP = "192.168.1.1", User = "Testuser", Pass = "123";
SftpClient sftp;
private void UploadFileSFTP()
{
sftp = new SftpClient(IP, User, Pass);
sftp.Connect();
Uploader();
Downloader();
sftp.Disconnect();
}
string FilePath="C:\\folder\\", Filename = "Filename.extention",
DeliveryPath = "/tmp/";
private void Uploader()
{
using (var file = File.OpenRead(FilePath + Filename))
{
sftp.UploadFile(file, DeliveryPath + Filename);
}
}
//there is possibly a simpler way to download but this is how i did it.
string FromPath = "/tmp/testfile.txt", StoragePath = "";
private void Downloader()
{
if (File.Exists(StoragePath))
File.Delete(StoragePath);
if (!Directory.GetDirectories(Path.GetTempPath()).Contains("WorkFiles"))
{
Directory.CreateDirectory(Path.GetTempPath() + "WorkFiles");
}
StoragePath = Path.GetTempPath() + "WorkFiles\\testFile.txt";
Int64 iSize = sftp.ReadAllBytes(FromPath).Length;
Int64 iRunningByteTotal = 0;
using (Stream streamRemote = sftp.OpenRead(FromPath))
{
using (Stream streamLocal = new FileStream(StoragePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
int iByteSize = 0;
byte[] byteBuffer = new byte[iSize];
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
streamLocal.Write(byteBuffer, 0, iByteSize);
iRunningByteTotal += iByteSize;
}
}
}
}