Putty - Delete files dynamically using C#

*爱你&永不变心* 提交于 2019-12-02 14:13:31

问题


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 location

I am currently using below code to login to putty, but how do i ran the delete command ??

string hostname = "hostname";
string login = "login";
string password = "password";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Putty\putty.exe";
startInfo.Arguments = string.Format("{0}@{1} -pw {2}", login, hostname, password);
startInfo.Start();

I want to pass something like this

rm myFile*.txt

But before that I have to navigate to particular location by

cd /dir1/dir2/NewFolder

I wanted to delete all the .txt files under NewFolder??


回答1:


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);
}



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/26773567/putty-delete-files-dynamically-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!