Putty - Delete files dynamically using C#

后端 未结 3 1906
渐次进展
渐次进展 2021-01-27 04:55

I am working on a Windows app with C# as a programming langugage.

Requirement is

  1. to login to putty dynamically
  2. delete old files from specific loca
相关标签:
3条回答
  • 2021-01-27 05:09

    Putty has the command line option -m {script_file} which allows you to specify a script file to be run against the remote server. After all the commands are run, putty will exit.

    You could save the command to be run in a script file, call Putty, and delete the script file when you're done.

    The following code works for me:

    string hostname = "hostname";
    string login = "login";
    string password = "password";
    string command = "rm ~/dir1/dir2/NewFolder/*.txt"; // modify this to suit your needs
    
    const string scriptFileName = @"remote_commands.sh";
    File.WriteAllText(scriptFileName, command);
    
    var startInfo = new ProcessStartInfo();
    startInfo.FileName = @"C:\Putty\putty.exe";
    startInfo.Arguments = string.Format("{0}@{1} -pw {2} -m {3}", 
                                         login, hostname, password, scriptFileName);
    
    var process = new Process {StartInfo = startInfo};
    process.Start();
    process.WaitForExit();
    
    File.Delete(scriptFileName);
    

    If all you want to do is send a single command to the server, this solution will do. If you need more advanced functionality, like reading the server response, you should check out Thomas' answer.

    Edit:

    Here's how to use plink to run commands and get their output:

    string hostname = "hostname";
    string login = "login";
    string password = "password";
    
    var startInfo = new ProcessStartInfo();
    startInfo.FileName = @"C:\Putty\plink.exe";
    startInfo.RedirectStandardInput = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.UseShellExecute = false;
    startInfo.Arguments = string.Format("{0}@{1} -pw {2}",
                                            login, hostname, password);
    
    using (var process = new Process {StartInfo = startInfo})
    {
        process.Start();
    
        process.StandardInput.WriteLine("ls");
        process.StandardInput.WriteLine("echo 'run more commands here...'");
        process.StandardInput.WriteLine("exit"); // make sure we exit at the end
    
        string output = process.StandardOutput.ReadToEnd();
        Console.WriteLine(output);
    }
    
    0 讨论(0)
  • 2021-01-27 05:19

    I don't know if this helps, because I can't try it right know, but it could lead you to the right direction. Try something like this:

    string hostname = "hostname";
    string login = "login";
    string password = "password";
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardInput = true;
    startInfo.FileName = @"C:\Putty\plink.exe";
    startInfo.Arguments = string.Format("{0}@{1} -pw {2}", login, hostname, password);
    
    Process p = new Process();
    p.StartInfo = startInfo;
    p.Start();
    
    p.StandardInput.WriteLine("cd /dir1/dir2/NewFolder");
    

    You should try 'plink', which is the command line version of Putty. With startInfo.RedirectStandardInput you specify, that the you can write to the stdin of the process with p.StandardInput.WriteLine(). This is also available for stdout, so you can read the output of the process.

    0 讨论(0)
  • 2021-01-27 05:26

    To answer your question we should first make some things clear:

    (1) You do not login to PuTTY. You use PuTTY to establish a connection to a remote server and to start a shell. Therefore you login to the server hostname

    (2) PuTTY is a program to provide an interactive shell to the user. It is not intended to be used as an programming interface

    What you look for is a SSH client library such as https://sshnet.codeplex.com/ (never used it, stated here as an example). Such libraries allow you to send commands using .NET using a predefined API

    0 讨论(0)
提交回复
热议问题