vb.net 通过 Visual Basic 应用程序在 unix 服务器上执行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14445004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:36:29  来源:igfitidea点击:

executing commands on unix server via visual basic application

vb.netbashshellunix

提问by Manny Balboa

I need help with executing commands on an Unix server using a Visual Basic application. I am using Visual Basic Express 2010.

我需要有关使用 Visual Basic 应用程序在 Unix 服务器上执行命令的帮助。我正在使用 Visual Basic Express 2010。

P.S. I can connect to the server using systems.net.socketsbut cannot send commands to execute.

PS 我可以使用连接到服务器,systems.net.sockets但不能发送命令来执行。

I am open to trying any different approach, I just need to know how I can do this.

我愿意尝试任何不同的方法,我只需要知道如何做到这一点。

回答by Alex Nelson

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

  1. 在系统上安装类似 Putty 的程序,并使用 Putty 对服务器(应用程序外部)进行 SSH 调用并以这种方式发送您的命令。这将需要对您的应用程序进行一些提升的权限,因为它必须直接访问系统。在我看来,这是做到这一点的“丑陋”方式。

  2. 找到一个 VB SSH 库。我找到了这个,但必须有更多。这将允许您创建到 Unix 框的 SSH 连接并传递您要运行的命令。这将是“最好”的方式。

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. :)

它是 CSHARP 并不重要。您可以下载 Visual C# 2010 Express 并打开 Renci.SshNet 并构建它。构建完成后,您将获得一个 dll,然后您可以将其添加为对 VB 项目的引用。有点痛苦,但使用 Express 版本需要付出很小的代价。:)

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

这是在 Visual C# 2010 Express 中打开并运行构建的 ssh 库的屏幕截图。您可以在左下角看到“构建成功”: 构建截图

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:

将其作为参考后,您就可以使用该库与服务器建立 ssh 连接并运行您的命令。这是一些示例代码。我创建了一个 VB Web 应用程序,添加了 dll 作为引用,并向按钮添加了一个单击事件,该按钮将结果吐到标签控件中:

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.

为了确保它在非 Web 应用程序中工作,我在使用 Vb 2010 express 创建的 Windows 窗体应用程序中做了这个示例。我在表单中添加了一个按钮和一个标签,并将上面的代码添加到按钮单击事件中。然后我添加了对我从 C# 2010 Express 创建的 DLL(库)的引用。

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

接下来,我运行了该项目并单击了按钮。连接到 unix box 并将命令的结果(在本例中为 'ls -lthr')放置在标签中。我以 root 身份登录(不推荐)并从 /root/ 目录运行命令。这就是为什么那里没有多少。 应用程序运行