I need help with executing commands on an Unix server using a Visual Basic application. I am using Visual Basic Express 2010.
P.S. I can connect to the server using systems.net.sockets
but cannot send commands to execute.
I am open to trying any different approach, I just need to know how I can do this.
I can think of two ways to accomplish this:
Have a program like Putty installed on the system and make an SSH call to the server (outside of the app) using Putty and sending your commands this way. This will require some elevated privileges on your app since it's going to have to have access to the system directly. In my opinion, this is the "ugly" way to do this.
Find a VB SSH library. I have found this one but there has to be more. This will allow you to create an SSH connection to the Unix box and pass over the commands you want to run. This would be the "best"' way.
EDIT:
It doesn't really matter that it is CSHARP. You can download Visual C# 2010 Express and open Renci.SshNet and build it. Once it's built you'll get a dll that you can then add as a reference to your VB project. A little bit of a pain, but a small price to pay for using the Express editions. :)
Here's a screenshot of the ssh library open in Visual C# 2010 Express with the build run. You can see 'build succeeded' at the bottom left:
Once you have it as a reference then you can use the library to make an ssh connection to the server and run your command. Here is some sample code. I created a VB web application, added the dll as a reference and added a click event to a button that spit the results into a label control:
EDIT:
Just to make sure it worked in a non-web application, I did the example in a Windows Form app I created using Vb 2010 express. I added a button and a label to the form and added the code above to the button click event. Then I added a reference to the DLL (library) I created from C# 2010 Express.
This is a screenshot of adding the reference:
Here is a screenshot of the project properties showing the reference has been added:
Next, I ran the project and clicked the button. THe connection to unix box was made and the results of the command (in this case 'ls -lthr') were placed in the label. I logged in as root (not recommended) and ran the command from the /root/ directory. That is why there is not much there.