通过Visual Basic应用程序执行UNIX服务器上的命令 [英] executing commands on unix server via visual basic application

查看:477
本文介绍了通过Visual Basic应用程序执行UNIX服务器上的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Visual Basic应用程序中的Unix服务器上执行命令的帮助。我使用Visual Basic前preSS 2010。

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

P.S。我可以通过连接到服务器 systems.net.sockets ,但不能发送执行命令。

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:


  1. 有一个程序一样安装在系统上腻子,使一个SSH调用服务器使用腻子并发送你的命令这种方式(应用程序之外)。这将需要一些提升特权在你的应用程序,因为这将需要直接访问系统。在我看来,这就是丑的方式来做到这一点。

  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.

查找VB SSH文库。我发现这个,但必须有更多。这将允许您创建一个SSH连接到Unix框,并越过要运行的命令。这将是最好的的方式。

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.

编辑:

这其实并不重要,这是CSHARP。您可以下载的Visual C#2010前preSS和开放Renci.SshNet并构建它。一旦它的建成,你会得到一个dll,然后可以添加作为您的VB项目的引用。痛一点点,但小的代价使用防爆preSS版本。 :)

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

这里的SSH文库在Visual C#与构建运行2010前preSS开放的截图。你可以看到构建成功左下角:

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:

一旦你拥有了它作为参考,那么你可以使用该库进行ssh连接到服务器,并运行命令。下面是一些示例code。我创建了一个VB Web应用程序,添加该DLL作为参考,并添加一个click事件一个按钮,吐出的结果变成一个标签控件:

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

编辑:

只是为了确保它的工作在非Web应用程序,我做了一个Windows窗体应用程序我用VB 2010的前preSS创建的例子。我加了一个按钮和一个标签的形式,并且增加了code以上按钮单击事件。然后,我加入到DLL(库)的参考,我从C#2010前preSS创建。

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:

接下来,我跑了该项目,并单击按钮。到UNIX箱的连接被制成,该命令的结果(在此情况下,LS -lthr')被放置在标签。我以root身份登录(不推荐),并从/根/目录下运行命令。这就是为什么没有太多的存在。

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.

这篇关于通过Visual Basic应用程序执行UNIX服务器上的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆