Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, May 13, 2015 3:11 PM
Hi,
Please can someone suggest an approach for the following scenario:
1. connect to LINUX server from .NET(C#) application.
2. Execute UNIX script which is on the LINUX server from the .NET application
Any code snippets for the same is appreciated.
It's very urgent.. Thanks in advance...
All replies (7)
Wednesday, May 13, 2015 3:38 PM ✅Answered
Probably, the only way you can do it is through a Web service. You mau wnat to lookup Mono and Linux too.
https://msdn.microsoft.com/en-us/magazine/dn342871.aspx
Wednesday, May 13, 2015 7:18 PM ✅Answered
You may want to find an implementation of the SSH protocol written in C# and use it to connect and interact with your linux/unix system.
Ex.:
https://github.com/net-ssh/net-ssh
Thursday, May 14, 2015 10:05 AM ✅Answered
Hi Grace,
I agree with David . Using open source code (SSH) Project through WCF service to UI.
You could look at ssh library to accomplish this. SSHwill make a connection to a remote machine and run arbitrary commands.
http://www.tamirgal.com/blog/page/SharpSSH.aspx
Best reagrds,
Kristin
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Thursday, May 14, 2015 1:16 PM ✅Answered
1) Create a user on that Linux server, set the user to run bash as shell. If the script needs sudo permission to some commands, also configure it here.
If your Linux server contains Web / FTP/ ... server daemon, please adjust configuration to exclude the account from using them.
2) At the user's home directory, create ".profile" plain text file that just contain the script you're going to run, then exit. Alternatively if you need not know the script's execute status and don't want to keep connection open, you can make it "source <script file name> &" and then exit
3) Write SSH / Telnet client in C# to login the server, the script should run automatically.
Whether using Telnet is acceptable depends on what your script is about. If it can be run at anytime and won't disclose sensitive information, it could be acceptable.
EDIT: Actually if there is Web server running, using PHP / CGI to invoke the script could be easier, but with it's own set of security implications. There are tons of WebClient examples on the web for you to refer to... so your call on what to choose from.
Thursday, May 14, 2015 1:19 PM ✅Answered
If you can't use external libraries but don't need encryption, telnet client is easiest to write.
Just ReadLine() for login prompt, WriteLine() your username, ReadLine() the password prompt, then WriteLine() your password will do.
Of course, writing with 3rd party library make your work easier, secure and more maintainable.
Tuesday, May 26, 2015 9:23 AM ✅Answered
Thank you all for the answers.
I used 'Renci SshNet' which was available as a nuget package in Visual Studio.
Using the inbuilt methods provided by this ssh, i was able to connect to the remote UNIX servre and execute the shell script.
Hope this will be helpful. Thanks again.
Thursday, May 14, 2015 12:37 PM
Thank you DA924, David & Kristin for the quick reply.
Is there any inbuilt .NET libraries to connect to remote servers and perform the same task? As i may be not allowed to use external libraries in my project.
Thanks in advance.