JSchException verify: False C# .NET Scp SSH - c#

I want to connect to remote server to upload some files via SCP/SSH.
I am using SharpSSH for this but it doesnt work.
Everytime the ScpClint wants to connect i get the JSchException verify: False.
Here is my code:
Scp scpClient = new Scp();
scpClient.To(#"C:localPath", "hostIP", "/var/...", sshUser, sshPwd);
Does anybody an have any suggestions on how to fix this problem? Maybe another library? I also tried Renci.SshNet but there I can connect or it doesn't give any errors, but no matter what I try to upload nothing is to find on the Server.

Which version of the library are you using? That code is fairly old and unmaintained. My usual recommendation is to try SSHNet since that's a much more solid library, written for .NET, but you said you've already tried that. Perhaps it's a problem on the remote side if you cannot connect with either of the libraries?

Related

Why is SFTP fingerprint from FileZilla not accepted by WinSCP .NET assembly in C#?

I'm setting up to put files using SFTP to a client's server. I have an SFTP server to test and developed a client using WinSCP in C# VS2022 on Windows Server 2019. I initially logged on to the test server using FileZilla and copied the fingerprint by double clicking it. I pasted that into my C# client and it worked as expected.
I did the very same thing with the client's SFTP server, but I get the error: "SSH host key fingerprint does not match pattern". The format does look slightly different from mine:
SHA256:tUlL3I72i+NOA.......DLTVoH35szgNVJiOGGhRV/ic
Mine has SHA256 [space] then the footprint. I'm not sure what the :tUlL3I72i+ is all about but I've tried removing it, turning the + into a space, etc. but same error.
What am I missing and how can I fix this?
FileZilla's Unknown host key box looks like this:
In WinSCP .NET assembly, that maps to the following value of Session.SshHostKeyFingerprint:
"ssh-ed25519 255 1hI1HqP3IzoOWCABHGS7+GsrP2JUVsSs7oskK7HGP1E"
Though way easier is to use WinSCP GUI to generate a code template for you:

Modifying a remote file while connected to a server through SSH using .NET

I have successfully established a connection to a remote Linux server using the SSH.NET package with the following code (I am using a ShellStream because I have to use sudo su):
using (var client = new SshClient(server, username, password))
{
client.Connect();
List<string> commands = new List<string>();
commands.Add("sudo su - user");
commands.Add("vi test.properties");
ShellStream shellStream = client.CreateShellStream("xterm", 80, 24, 800, 600, 1024);
// Execute commands under root account
foreach (string command in commands) {
WriteStream(command, shellStream);
}
client.Disconnect();
}
private static void WriteStream(string cmd, ShellStream stream)
{
stream.WriteLine(cmd + "; echo this-is-the-end");
while (stream.Length == 0)
Thread.Sleep(500);
}
I am trying to edit the test.properties file that is in the remote Linux server using a C# function that I created.
My code (using C# in Visual Studio) that is used to modify the text file is uses System.IO.File.ReadAllText, but it does not recognize the path of the remote server, for example:
The text file in the Linux server is in this location: /home/user/test.properties, so I am using this in my code:
System.IO.File.ReadAllText("/home/user/test.properties")
I am getting the following error:
Could not find a part of the path 'C:\home\user\test.properties'
For some reason it tries to look in my local file system instead of the remote server.
Is there a different approach I should be taking?
Thanks in advance!
In general, to modify remote files, use SFTP. In SSH.NET that's what SftpClient is for.
Though as you seem to need to use elevated privileges (su) – an important factor that your question title fails to mention – it's way more difficult. The right solution is to avoid the need for su. See somewhat related:
Allowing automatic command execution as root on Linux using SSH.
Another option would be to try to execute the SFTP server under su. Though that would require modification of SSH.NET code. See related Java question:
Using JSch to SFTP when one must also switch user
If you want to keep your current shell approach with su, you are stuck with simulating shell commands. Note that connecting to SSH server won't make other .NET classes (like the File) magically be able to work with remote files (even if SFTP was possible, let only when it is not, due to the su requirement).
The easiest way to read remote file using shell is using the cat command:
cat /home/user/test.properties
Alright, so after finishing the task, this is what I did since asking the question here:
1.After your(#Martin Prikryl) response I've tried using a combination of SSH and WinSCP:
WinSCP to download the file.
.NET to modify the locally downloaded file.
WinSCP to upload the file(and deleting it from the local folder afterwards).
SSH to move the file to its appropriate location in the server.
I discarded this solution because it worked pretty well in the lower environment,
but in the production I had permissions issues so I couldn't even download it, let alone that it might be a security issue(its a sensitive file).
2.My next solution was using only SSH to simulate shell commands, as you previously mentioned, I was limited to that because I was stuck using sudo su.
I connected to the server with SSH and used the 'sed' command to only show lines that contain specific words(instead of using cat to get the whole file).
I then used my .NET code to pull the values that I needed for my GET operation
For the POST operation I used 'sed' again to replace lines.

ErrorMessage while open connection to Denodo via npgsql in c#

sorry, if this is question is (to) easy for stackoverflow community.
I am trying to connect to Denodo (Version 7), via c# code.
I installed npqsql and created a connection string with: {host} {port} {username} {passwd} {database} and the SSL-Mode as required.
(user and pwd combination was checked via Launch Pad)
For trying I use a simple select statement.
If I call Open() on my NpqsqlConnection object I get the error message {"Received unexpected backend message ParseComplete. Please file a bug."}
For beeing sure I tried to ping the host, which works fine. And if I try without SSl, the error message indicates that the SSl is required, therefor I think the correct backend is found.
Could someone give me a hint?
Thanks
Jessi
I checked the Denodo Community, and the given examples: https://community.denodo.com/docs/html/browse/6.0/vdp/developer/access_through_an_ado.net_data_provider/access_through_an_ado.net_data_provider
https://community.denodo.com/answers/question/details?questionId=90670000000XcbkAAC&title=How+to+execute+C%23+program+using+vdp-clients-ADO.NET

Renci SSH.NET: How to create symlinks?

I am currently uploading files to a unix server using SFTP using Renci SSH.NET, and it works fine. However, I now would like to upload files to a "Symlink container", and then create a symlink in another directory pointing to these files. Is that possible? I haven't found a class to manage symlinks, so how can this be achieved?
Use the SftpClient.SymbolicLink method.
public void SymbolicLink(string path, string linkPath)
Note that the meaning of the paths is a mess. The most common SFTP server, the OpenSSH, is buggy and uses the paths in a wrong order. Many other SFTP servers follow the bug for a compatibility. But not all. So you have to test which order is used by your servers.
See
https://bugzilla.mindrot.org/show_bug.cgi?id=861
http://bugs.proftpd.org/show_bug.cgi?id=4080
I have worked with SSH.net and I don't have found something about symlinks. However I found that SSH.net supports custom commands therefor you can write the Linux command (I think that it's ln) to create the symblink.
In this url you can get an example about how to use a custom command with SSH How to run several commands with SSH.Net?
Maybe if you need to create a lot symlinks, you can create a class or you can download the source code and add a new method.
I hope this can help you.

how to transfer file from PC to Windows-mobile and from Windows-mobile to PC?

i need to transfer Text.txt file from PC (using WebService)
to Windows-mobile 5.0 - and from Windows-mobile to PC
can i get any sample code in C# for the WebService and the Windows-mobile ?
thanks in advance
You cannot directly transfer files. you should try ActiveSync RAPI to send information.
Use OpenNETCF. It's a useful compact framework for WindowsMobile (.NET).
Try this:
string fileToSendToDevice = #"C:\Text.txt";
OpenNETCF.Desktop.Communication.RAPI rApi = new OpenNETCF.Desktop.Communication.RAPI();
if (!rApi.DevicePresent) return; // no active sync
if (!rApi.Connected) rApi.Connect();
if (!File.Exists(fileToSendToDevice)) return; // file not found
rApi.CopyFileToDevice(fileToSendToDevice,
Path.Combine(#"\My Documents\", Path.GetFileName(fileToSendToDevice)));
The concept is really no different than PC-to-PC if you intend to use a Web Service for the transfer (assuming the Web Service is running on the PC). Find any example on the web that transfers a file to and from a Web Service and it will likely be applicable.
If you then have a question that is more specific to WinMo/CF or an implementation problem, feel free to update this question about what you've tried and what about it is not working.
One of these two examples will probably help. The code for the PC will be likely the same if not simpler.
Then you just need to write the web service.

Categories