Search code examples
javalinuxsshirc

SSH client output over IRC


I'm currently developing a small bot in Java that I run from Raspberry Pi's. These Raspberry Pi's are connected to DSL-routers in some shops that a friend of mine run. The purpose is to monitor different things in his shops, like uptime, client uptime etc., using nmap for one.

I've implemented the IRC client using PircBot.

Furthermore, I've used Jsch as my SSH client. I've followed one of the example SSH clients to get the shell output in the Java console.

Now, what I want to do is, to output this in JircBot's sendMessage() method instead. My Jsch client uses channel.setOutPutStream(System.out) as of now.

How do I get Jsch to output via JircBot's sendMessage() instead?

PS: The SSH client connects locally, there might even be smarter ways to execute commands on the Raspberry Pi, instead of using SSH locally, and output the shell via PircBot?

Kind regards


Solution

  • If you are just trying to run local commands, using SSH is not necessary. I'd recommend you take a look at the Apache commons-exec libraries, which will let you run commands on the Rasperry Pi without the overhead of authentication and establishing a secure connection.

    In your case, you could use it to relay messages to the PircBot API:

    Executor ex = new DefaultExecutor();
    ex.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
        @Override
        protected void processLine(String line, int level) {
            // Call PircBot's sendMessage() passing line
        }
    }));
    int exitValue = ex.execute("command to execute locally");