1

I have a script in Perl which runs on a Unix server (Solaris to be precise). The script uses ftp utility to condition files in a mainframe server and then transfer them to Unix server.

Recently, I am trying to develop a Windows desktop application using Windows forms and C#. I am trying to use the SSH.NET library available from codeplex. The problem is, whenever I run a normal Unix command from the application it works, but when I try to run the script it does not work. I have checked the connection and the application is properly connecting to the server. I have used both the below methods but no luck :-

using (ssh client = new SshClient("Ip", "username", "password"))
{
    client.Connect();
    SshCommand cmd = client.RunCommand("script");
    client.Disconnect();
}

using (ssh client = new SshClient("hostnameOrIp", "username", "password")) 
{
    client.Connect();
    SshCommand cmd = client.createCommand("script");
    cmd.execute();
    client.Disconnect();
}

It would be a huge help if somebody points me towards a proper direction.

EDIT :- Running the script with the full path has worked as the non interactive shell that is invoked doesn't read the /etc/profile/ env variables when it loads. I will try out the other options suggested as well and edit the post if something works.

2
  • it does not work: What does that mean? It does not run? Or it fails when run? What's the output when you attempt to run it? Commented Nov 5, 2015 at 7:06
  • There is no output. The ouput should be the file in question to be transferred to the nix server from the mainframe server after some conditioning. If the script fails, the file is not transferred and there is no other output. Still, I can check the stderr and get some insight into it. Will do that first thing when I get a chance. Commented Nov 5, 2015 at 19:31

3 Answers 3

1

You need to debug your unix script. Modify the script to pipe stderr to a text file, and see what it says. For instance, the script might be relying on properties of the environment which change when you run it with ssh.net

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I will do that. If it does, what's the workaround ?
It depends on the script. I suggest you post a new question that includes details about how the script fails under ssh
Fair enough. But what I really wanted to ask is that, is there any inherent property or way to run user scripts which use environment variables from c# or rather through ssh .net lib?
I think the easiest way to get this right is to invoke your shell through the ssh command. Try replace client.createCommand("script"); with something more along the lines of client.createCommand("bash -l -c 'script'"); This should give you all your usual environment variables
Thanks. I will try that and let you know if its solved or what the exact issue was.
0

Your question is pretty vague.

But first, I'd try is to add a path to the script. Either absolute (/home/user/script) or relative (./script).

*nix shells by default do not look for executables in the current working directory (home directory in this case). They find them only, if you have . in your PATH.

Note that the RunCommand uses a non-interactive session. It may have a different environment (PATH) than the interactive session you use in the SSH terminal. Different set of start-up scripts is used for interactive and non-interactive sessions.

4 Comments

The scrpt is in my bin. It is accessible from any path I am in the nix server. So, ideally I should be able to run the script when I connect to the server with my credentials. Am I missing something here ?
Did you actually try the full path? The RunCommand uses non-interactive session. It may have a different environment (PATH) than the interactive session you use in the SSH terminal.
@KChow I am having the exact same issue can you show an example of what you did you get yours to work in regards to executing the script in non-interactive mode.. for example my path I am passing looks like the following /home/guest/src/test.sh I will ask the unix admin in my office what the full path is also does adding ./ find work load the environment variables..
You can do ./bash_profile in the ssh command and then the actual command like Ssh cmd=client.RunCommand("./bash_profile;/fullPath_from_your_home_directory_or_relative_path_from_the_landing_path_of_your_sshConnection/test.sh"); This should work fine for you. If it doesn't work then please elaborate on your actual problem and what you are trying to achieve.
0

Please make sure the default path for the user is correct and script is exist in the path. In order to debug the last command/script run/exit status you can again run shell command echo $? via C# ssh client to get the detail about the error. That may give some insight about the error

2 Comments

I did that and exit status was non zero. The script fails. I have not got the chance to check the stderr yet. I hope that gives me some clue.
with that exit code we can get a clue on the type of error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.