executing commands on unix server via visual basic application

前端 未结 1 1211
旧巷少年郎
旧巷少年郎 2021-01-22 02:00

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

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 02:17

    I can think of two ways to accomplish this:

    1. 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.

    2. 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: screenshot of build

    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:

    Protected Sub btnSSHTest_Click(sender As Object, e As EventArgs) Handles btnSSHTest.Click
    
        'Create the objects needed to make the connection'
        Dim connInfo As New Renci.SshNet.PasswordConnectionInfo("hostname", "username", "password")
        Dim sshClient As New Renci.SshNet.SshClient(connInfo)
    
        'Need to hold the command'
        Dim cmd As Renci.SshNet.SshCommand
    
    
        Using sshClient
            'connect to the server'
            sshClient.Connect()
    
            'Run the command and put the results into the cmd object. In this case'
            'I am just running a directory list'
            cmd = sshClient.RunCommand("ls -lthr")
    
            'my web page had a label control on it. I placed the results of the cmd into'
            'the label'
            lblResult.Text = cmd.Result
    
            'Close the connection.'
            sshClient.Disconnect()
        End Using
    
    End Sub
    

    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: how to add a reference

    Here is a screenshot of the project properties showing the reference has been added: screenhot showing the reference in the project

    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. application running

    0 讨论(0)
提交回复
热议问题