0
votes

I am making a desktop application in C# using Windows forms. The application connects to a Unix server (each user has a separate login credential and separate environment variables and .bash_profile) and runs scripts and sends back the output to the Windows form.

I am running a Perl script in Unix through ssh .net. Say abc.pl. This works fine, but inside the script I call another executable (cpp executable).

In my C# code I have given the entire path for the script and so it runs. Inside the script the entire path to the executable is given and so it also runs. But the executable fails because it doesn't get its corresponding required .so file.

The .so file is present in the library and the path to the library is set at .bash_profile.

I know that when I use ssh .net I am actually using a non interactive ssh session and so I would not have access to the environment variables or the .profile but I have tried many things as to putting ~/.bash_profile in my Perl script in the hope that the profile would be called and set in the ssh session and many other things but to no avail.

Can anybody tell me a workaround or how to run a cpp exec inside a Perl script using ssh .net from Windows platform

EDIT :- Importing the environment variable or rather setting the environment variable was the only way out but how to go about it was the real challenge. I chose to set the environment variable path for the Library for the duration of the script. In that way the SSH session, when it runs the script will have the path to the library set in the perl script for the sesssion and hence the executable will run smoothly. The code I used is :- $ENV{'LD_LIBRARY_PATH'}="$ENV{LD_LIBRARY_PATH}:/path/to/library";

EDIT2:- If by any chance you want to use all the environment variables present in your .profile of your interactive shell then you can run the entire .profile for your non interactive ssh session as well and then you wouldn't have to worry about setting the environment variables in your perl script. For example you CAN frame your command somewhat like this :- command = ". .bash_profile; actual command"; var cmd = ssh.RunCommand( command ); var result = cmd.Execute(); The .profile will run in your new non interactive shell and set the environment variables for the entire duration of the ssh connection.

2
You can add to the PATH environment variable in your Perl code. You can either hard code the path to the library or parse a .bash_profile to check what the value should be. Or there may be another way that your Perl program can discover the correct setting for PATH - Borodin
" I have tried many things..." You should edit your question to include the relevant parts of your perl script, including some of these things you tried. That would give people a much better chance of seeing the correct way to do what you want to do. - Kenster

2 Answers

0
votes

If the error is related to not finding *.so files, it is more than likely the LD_LIBRARY_PATH variable might need to be set. You can do this prior to the execution of the perl script when executing. Something like:

export LD_LIBRARY_PATH=/path/to/libs;/home/user/bin/myperlscript.pl

or just write a shell script wrapper where you set the environment variables explicitly, then call the perl script. Now, normally this isn't required and your admin might be able to setup the correct linkages, but this should work around the root issue.

Another option which depends on how cpp is being called is to set it directly in your perl script by setting the $ENV variable. Something like this

$ENV{LD_LIBARY_PATH} = "/path/to/libs";

This should work if you are doing a doing the call using the perl system method.

0
votes

Option 1:- Importing the environment variable or rather setting the environment variable was the only way out but how to go about it was the real challenge. I chose to set the environment variable path for the Library for the duration of the script. In that way the SSH session, when it runs the script will have the path to the library set in the perl script for the sesssion and hence the executable will run smoothly. The code I used is :- $ENV{'LD_LIBRARY_PATH'}="$ENV{LD_LIBRARY_PATH}:/path/to/library";

Option 2:- If by any chance you want to use all the environment variables present in your .profile of your interactive shell then you can run the entire .profile for your non interactive ssh session as well and then you wouldn't have to worry about setting the environment variables in your perl script. For example you CAN frame your command somewhat like this :-

command = ". .bash_profile; actual command"; 
var cmd = ssh.RunCommand( command ); 
var result = cmd.Execute(); 

The .profile will run in your new non interactive shell and set the environment variables for the entire duration of the ssh connection.

Though I have mentioned the very same thing in the edits as well. But for the convenience of all I am posting it as an answer since I have tried and tested both the above mentioned methods myself.