Reputation: 1364
Using the library to connect to a remote server and copy a file. I have the process working fairly well but have some smaller things which I cant seem to resolve as documentation for the library is fairly thin.
I have two routines working. One using the Tamir.SharpSsh class and the other using the Tamir.SharpSsh.jsch class.
Using the Tamir.SharpSsh class I am able to copy the file from the local server to the remote server and tap into the pogress event. What I can't do is determine if a particular file on the remote server say /Report/data.txt exists on the server. I need to take different actions if it exists or if doesn't exist. Also how would I rename a file on the remote server. Ive tried using SshExec with a 'rename', 'rn', and 'mv' command and it doesn't seem to work.
Using the Tamir.SharpSsh.jsch I can copy the file from the local server to the remote server. I can also rename the file on the remote server. What I cant do with this class is to tap into the progress event to keep track of the copy progress. Also I cant seem to find a good way to test to see if a particular file exists on the server. What I have come up with is crude and the only way that I could come up with to test and that is to use
Dim c As ChannelSftp
Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
Dim cnt As Integer = vct.Count
When one or more file exists I get a count no problem. When there is no file then an exception is thrown.
Anyway, I have the routines working its just some minor things I need help with.
tia AGP
Upvotes: 0
Views: 3908
Reputation: 1364
yeah I tried something similar with Tamir.SharpSsh.jsch but it seems odd to me that you have to catch the exception to detect non-existence of a file. here is what i did after I posted:
Private Function FileExistsOnServer(ByVal c As ChannelSftp, ByVal sRemoteFile As String) As Boolean
Try
'get a file listing of the file
Dim vct As Tamir.SharpSsh.java.util.Vector = c.ls(sRemoteFile)
Dim cnt As Integer = vct.Count
'if the count is greater than zero then the file already exists. if its 0 then the file does
'not exist on the server
If cnt > 0 Then
Return True
Else
Return False
End If
Catch ex As Exception
'if we get an exception then assume the file does not exist on the server
Return False
End Try
End Function
Upvotes: 0
Reputation: 4841
You can call the Tamir.SharpSsh.Sftp.GetFile
method using the path of the file you want to check exists (example in C#, sorry):
private bool FileExists(string filePath)
{
try
{
SftpConnection connection = new SftpConnection(_host, _username, _password);
connection.Connect(_port);
connection.Get(filePath, _toDir);
}
catch(JSchException)
{
return false;
}
return true;
}
I have also noticed a few other issues through my use of this library - like a lack of a GetFileInfo
method or recursive Gets and Puts. But overall it get the job done.
The simple fact is, Tamir.SharpSsh can't rename a file remotely - it just does not implement that functionality. You can purchase a better library that has far more features, such as:
or you could extend SharpSsh, since it is open source.
Upvotes: 2
Reputation: 14160
Your issues are because of limitations of SFTP protocol. - to check file existence, try to return attributes of that file; - most servers don't support file renaming for now.
Upvotes: 0