Running python script using SSH

Oct 16, 2013 at 5:14 PM
Hello
I am new in using SSH.NET library so correct me if I am wrong. I want to run a python command on remote machine using SSH.
                var cmd = client.CreateCommand("python2.7 /data/report.py x y z");
                //var command = client.CreateCommand("echo 'hello' ");
                //var command = client.CreateCommand("ls");
                result = cmd.Execute();
                Console.Write(result);

                var reader = new StreamReader(cmd.ExtendedOutputStream);
                Console.WriteLine("DEBUG:");
                Console.Write(reader.ReadToEnd());
1) Normal python script without using few specific setting in .bash_profile works well with above code snippet.
2) However, python code using few commands which are set in my .bash_profile (server on which I am connecting through SSH) doesn't work. Error is mostly because of not able to get settings from .bash_profile.

I read one post on this forum which says runCommand() or execute() everytime creates new shell with default environment and recommended to use shellStream.
using (var client = new SshClient(hostname, port, username,pwd))
            {
                client.Connect();

                using (var stream = client.CreateShellStream("xterm", 80, 24, 800, 600, 1024))
                {
                    var reader = new StreamReader(stream);
                    var writer = new StreamWriter(stream);
                    writer.AutoFlush = true;

                    Thread.Sleep(10000);
                    var text = reader.ReadToEnd();
                    Console.Write(text);
                    Thread.Sleep(10000);
                    writer.WriteLine("python2.7 /data/report.py x y z");
                    Thread.Sleep(10000);
                    text = reader.ReadToEnd();
                    Console.Write(text);
                }
client.Disconnect();
            }
I changed my code to work with shellstream but using sleep() or expect doesnt look good solution in my case.
I would prefer doing it with runCommand as it is more cleaner.
1) I would like to know how CreateShell is different than RunCommand() or Execute()?
2) How can I make sure Execute() uses my environment and not the default one?
3) Any other alternative to solve this problem?

Any help/advice is appreciated.
Thanks