简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM