1

I'm trying to develop an interface used to interact with some programs. I have a windows computer, which will hold the interface, and a Raspberry Pi whcich will launch programs.

I've to use them through SSH, but then to interact with :

  • From some text fields, i get parameters for programs, and launch it, connected to the SSH session. The program stay in the waiting state "enter a message :".
  • Then the user has to be able to enter a message on another text field from the computer, in order to use this message on the program. Here is the problem.

This is for a wireless communication between 2 raspberrys!

In fact, i've no difficulty to launch the program through any Expect library (expectJ, expect4J, expectIt) but i always fail to enter the message after.

The architecture is :

public SSH(String host, String user, String password) throws Exception {
    connection = new JSch();
    try {
        session = connection.getSession(user, host, 22);
        session.setPassword(password);
        connection.setConfig("StrictHostKeyChecking", "no");
        session.connect();
        channel = session.openChannel("shell");
        channel.connect();
        expect = new ExpectBuilder()
                .withOutput(channel.getOutputStream())
                .withInputs(channel.getInputStream(), channel.getExtInputStream())
                .withEchoInput(System.out)
                .withEchoOutput(System.out)
                .withExceptionOnFailure()
                .build();
    } catch (JSchException e) {
        System.out.println(e.getMessage());
        throw new Exception("Error on session");
    }
}

public void exec(String prog) {
    try {
        expect.sendLine(prog);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public String send(String cmd){
    try {
        expect.sendLine(cmd);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

The return "" should be the acknowlegdment when the programme whill be finished. But the send() always fail and enter the data as a linux command line...! So, i'm searching about a solution, like a "sub-expect" of the expect instance... Like this :

SSH --
      |-./udp xxxxx
        -|"hello"
        -|"132"
      |-close()

Anyone has an idea ?

Thanks !

Alexey Gavrilov
  • 10,593
  • 2
  • 38
  • 48
Aeldred
  • 314
  • 2
  • 15
  • please share your code of send, so we could help you – unique_ptr Aug 06 '15 at 10:10
  • @aeldred Please don't edit your question to include the answer or put "solved" in the title. You can answer a question, including your own question, by filling in the "answer" box at the bottom of the page. – Kenster Aug 07 '15 at 13:16

1 Answers1

0

Per @aeldred: I succeeded to do this ! If someone search for the same problem, it was the program execution's call which failed the communication, had to replace my channel with a ChannelExec, and my exec methods became :

 public void exec(String prog) {
        try {
            channelExec.setCommand(prog);
            channelExec.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Kenster
  • 23,465
  • 21
  • 80
  • 106