1

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??

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
Code's
  • 208
  • 2
  • 18

3 Answers3

2

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);
}
Community
  • 1
  • 1
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
  • I am getting exception - "The Process object must have the UseShellExecute property set to false in order to use environment variables". [ while executing above code] -please help – Code's Nov 06 '14 at 08:28
  • @Coder2014 Don't know why; it worked perfectly for me. Try inserting `startInfo.UseShellExecute = false;` before the `new Process(...` line. – Cristian Lupascu Nov 06 '14 at 09:11
  • Thanks it worked!! Is there any way to validate whether the command executed or not. i know you have mentioned to check Thomas answer. But while using that i am not able to get p.StandardOutput. can you help!! – Code's Nov 06 '14 at 09:38
  • @Coder2014 just tried it and it worked. Please see the update to my answer. – Cristian Lupascu Nov 06 '14 at 10:49
1

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.

Thomas Lielacher
  • 1,037
  • 9
  • 20
  • can you please help to get the standard output after executing command. I tried http://stackoverflow.com/questions/1390559/how-to-get-the-output-of-a-system-diagnostics-process but it didnt helped – Code's Nov 06 '14 at 09:41
0

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

Sascha
  • 10,231
  • 4
  • 41
  • 65