Asked by:
SSH in Web Applications

Question
-
User-73514677 posted
Hi,
How to use SSH in an ASP.NET C# Web Application ? I am using ASP.NET 4.0.
How to connect to the remote server and execute unix commands from Web Application?
Thanks
Tuesday, July 24, 2018 8:40 AM
All replies
-
User475983607 posted
Have you tried typing "SSH C#" in a Google search? There's tons of examples.
Tuesday, July 24, 2018 11:39 AM -
User-73514677 posted
Hi,
I tried the below code :
using (SshClient ssh = new SshClient("IP address", port number, "username", "password")) { ssh.Connect(); var result = ssh.RunCommand("df -h"); ssh.Disconnect(); }
I am getting error as Renci.SshNet.Common.SshOperationTimeoutException: Socket read operation has timed out.
Wednesday, July 25, 2018 6:29 AM -
User446085077 posted
Hi,
I tried the below code :
using (SshClient ssh = new SshClient("IP address", port number, "username", "password")) { ssh.Connect(); var result = ssh.RunCommand("df -h"); ssh.Disconnect(); }
I am getting error as Renci.SshNet.Common.SshOperationTimeoutException: Socket read operation has timed out.
You gotta config Linux server.
Code is no problem.
Thursday, July 26, 2018 2:24 AM -
User-73514677 posted
Hi,
Thanks for the reply. What settings should be done on the server ?
Thanks
Thursday, July 26, 2018 3:27 AM -
User283571144 posted
Hi Rico,
According to your description and codes, I have created a test demo on my side, it works well.
I suggest you could firstly using cmd ping function to check you could access the unix server.
If the network is well, I suggest you could check your unix server has enable the remote access.
More details about how to enable the ssh remote access, you could refer to below article.
https://docs.oracle.com/cd/E18930_01/html/821-2426/gksja.html
Then I suggest you could directly use SSH to connect to the unix server.
More details about my test demo, you could refer to below codes:using Renci.SshNet; using System; namespace ConsoleApp { class Program { static void Main(string[] args) { try { using (var client = new SshClient("server ip address", "your user name", "your password")) { client.Connect(); Console.WriteLine("Connected"); string result = client.RunCommand("ifconfig -a").Execute(); Console.WriteLine(result); client.Disconnect(); Console.WriteLine("Disconnected"); } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } Console.ReadLine(); } } }
Result:
Best Regards,
BrandoThursday, July 26, 2018 3:37 AM