Running python script using SSH

Oct 16, 2013 at 5:14 PM
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/ x y z");
                //var command = client.CreateCommand("echo 'hello' ");
                //var command = client.CreateCommand("ls");
                result = cmd.Execute();

                var reader = new StreamReader(cmd.ExtendedOutputStream);
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))

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

                    var text = reader.ReadToEnd();
                    writer.WriteLine("python2.7 /data/ x y z");
                    text = reader.ReadToEnd();
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.