通过C#测试获取通过ssh在远程服务器上运行的脚本的输出

时间:2019-05-16 22:09:45

标签: ssh .net-core

我正在.net核心中编写自动化测试,它将访问运行我们正在测试的应用程序的远程服务器。我目前正在测试中连接到远程服务器上,以运行用于验证某些数据的脚本。我想从该脚本获取输出到本地计算机,以便我的测试可以知道该步骤是否通过。

我正在使用Renci.SshNet工具连接到远程服务器。

这是我在测试中运行的代码,用于连接到远程主机,然后以字符串形式发送命令。

string commandToRun = "/opt/scriptHome/scriptWithOutput";

using (var client = new SshClient(ipAddress, username, password))
{
    client.Connect();
    client.RunCommand(commandToRun);
    client.Disconnect();
}

1 个答案:

答案 0 :(得分:0)

RunCommand返回一个SshCommand类型的对象,您可以使用其属性来检查命令的结果,例如:

MenuBar menu = new MenuBar(true);

    for (int k = 0; k < 5; k++) {
            int ii=k;
            menu.addItem(tekst + k, new Command() {

                @Override
                public void execute() {
                    ClassMetodInterface metoda = (ClassMetodInterface) GWT.create(Method2.class);
                    vPanel.clear(); //clearing before I open a new widget.

/*I sent int (easier to tests), but I can sent string[i] too*/
/* logging - instance of main class to communicate with 'subclasses', in my project necessery*/

                    vPanel.add(metoda.defaultMethod(ii, logging));  
                }

            });
        }
(...)
/*-------------------------------------------*/
ClassMetodInterface, only for implementation class Method2 :
/*-------------------------------------------*/
    public interface ClassMetodInterface {
    Widget defaultMethod(int g, Logging logging);
}

/*-------------------------------------------*/
class Method2 with swich:
/*-------------------------------------------*/


public class Method2 implements ClassMetodInterface {

    public Widget zapisUsera(int g, Logging logging) {

        Logging logging;
        HorizontalPanel lPanel = new HorizontalPanel();

        switch(g) {
        case 1: {
            Window.alert("switch for test no:"+g);
            MenuWindow1(logging);

            break;

        }
        case 2:{
            Window.alert("switch for test no:"+g);
            break;
        }
        default:
            Window.alert("switch default - loop >2 && <1");
            break;
        }
        return lPanel;  //all methods will be void - they will be add widgets and Panels to lPanel

    }

    public Widget MenuWindow1(Logging logging) {
        this.logging = logging;

        lPanel.setBorderWidth(2);
        this.lPanel.setPixelSize(100, 50); 
        Button b1 = new Button("test button, not used");    
        lPanel.add(b1);

        Label lbl = new Label("label no "+logging.i);       

        lPanel.add(lbl);
        Button addGlobal = new Button("+");
        Button removeGlobal = new Button("-");
        lPanel.add(addGlobal);
        addGlobal.addClickHandler(new addGlobal());
        removeGlobal.addClickHandler(new removeGlobal());
        lPanel.add(removeGlobal);


        return lPanel;

    }

//for tests communication with class, where I have menu and global variable
    public class addGlobal implements ClickHandler {

        @Override
        public void onClick(ClickEvent event) {
            logging.i++;
        }

    }

    public class removeGlobal implements ClickHandler {

        @Override
        public void onClick(ClickEvent event) {
            logging.i--;
        }

    }
}