-2

I searched everywhere but can't find a solution that works.

I have a Linux Debian machine in my network, which is running as a Mqtt Broker. I want to write a java programm to send sub and pub commands to the broker from another computer (Windows). Is there a way to send Linux commands from a Windows Computer?

If yes, is it possible to do it through java code and recieve the proper outputs?

I tried the following:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class AA
{
    public static void main(String[] args) throws Exception
    {
        ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c",
                "ssh 10.20.0.30 -l username"); // Ip of the Mqtt Broker
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
        String line;
        while (true)
        {
            line = r.readLine();
            if (line == null)
            {
                break;
            }
            System.out.println(line);
        }
    }
}

The output is:

Pseudo-terminal will not be allocated because stdin is not a terminal.

I feel like this might work, if the right commands would be added.

I have heard of libraries like "Eclipse Paho", but I want to know if my solution can work.

Thanks in advance!

Paul Erlenmeyer
  • 523
  • 2
  • 6
  • 29
  • try connecting two java programs using socket programming of Java and then run send messages which are your commands and on the receiver end run those locally. That will work. – Omkar Nath Singh Jul 26 '18 at 06:56

2 Answers2

0

Your solution can work if you fallow this approach Run a command over SSH with JSch

but you mention MQTT. therefore you dont need to use ssh. you can connect to mqtt and make run commands with it. here is mqtt connection example https://www.hivemq.com/blog/how-to-get-started-with-mqtt

ozkanpakdil
  • 3,199
  • 31
  • 48
  • Thanks a lot! I m looking into your first link. The second link points to "Eclipse Paho" implementation. I will use that in case everything else fails :) – Paul Erlenmeyer Jul 26 '18 at 07:34
-1

Try using ssh -tt in your code which might work for you.

From ssh manpage:

-t      Force pseudo-tty allocation.  This can be used to execute arbitrary 
        screen-based programs on a remote machine, which can be very useful,
        e.g. when implementing menu services.  Multiple -t options force tty
        allocation, even if ssh has no local tty.