0

Is it possible to run multiple commands in putty or plink using c#? Currently I'm using below code to run a single command to reached particular folder in my server.

Once I reached that folder I wanted to delete all the files under that folder, but how to write multiple commands using below approach?

Requirement is to run below 2 commands one by one command 1 = CD /Dir1/dir2/NewFolder & command 2 = rm *.txt"

    string hostname = "hostname";
    string login = "login";
    string password = "password";
    string command = "CD /Dir1/dir2/NewFolder";  

    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();
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
Code's
  • 208
  • 2
  • 18
  • why cant you just add more commands to 'scriptFileName'? – tolanj Nov 06 '14 at 10:33
  • I added command 2 in my scriptFileName but the problem is I am not sure whether the command executed or not, its running in the background. is there any way to see the output screen when the command is running!! – Code's Nov 06 '14 at 10:38
  • well you could add a `-v` switch to your `rm` command, to see what is deleted. – MrPaulch Nov 06 '14 at 10:44
  • @Coder2014 See http://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output for getting the output of the process – tolanj Nov 06 '14 at 10:44
  • when i am using -- while (!proc.StandardOutput.EndOfStream) { string line = proc.StandardOutput.ReadLine(); // do something with line } its not executing the proc.StandardOutput.ReadLine(). – Code's Nov 06 '14 at 10:52
  • possible duplicate of [Putty - Delete files dynamically using C#](http://stackoverflow.com/questions/26773567/putty-delete-files-dynamically-using-c-sharp) – Benjamin Diele Nov 06 '14 at 11:00

0 Answers0